2 # Copyright (C) Martin Schlemmer <azarah@nosferatu.za.org>
3 # Released under the terms of the GNU GPL
5 # Generate a newline separated list of entries from the file/directory
6 # supplied as an argument.
8 # If a file/directory is not supplied then generate a small dummy file.
10 # The output is suitable for gen_init_cpio built from usr/gen_init_cpio.c.
15 # This is a very simple, default initramfs
18 nod /dev/console 0600 0 0 c 5 1
26 # symlink test must come before file test
27 if [ -L "${argv1}" ]; then
29 elif [ -f "${argv1}" ]; then
31 elif [ -d "${argv1}" ]; then
33 elif [ -b "${argv1}" -o -c "${argv1}" ]; then
35 elif [ -p "${argv1}" ]; then
37 elif [ -S "${argv1}" ]; then
49 if [ -e "${argv1}" ]; then
50 my_mtime=$(find "${argv1}" -printf "%T@\n" | sort -r | head -n 1)
53 echo "# Last modified: ${my_mtime}"
59 local name="${location/${srcdir}//}"
60 # change '//' into '/'
61 name="${name//\/\///}"
65 local ftype=$(filetype "${location}")
66 # remap uid/gid to 0 if necessary
67 [ "$uid" -eq "$root_uid" ] && uid=0
68 [ "$gid" -eq "$root_gid" ] && gid=0
69 local str="${mode} ${uid} ${gid}"
71 [ "${ftype}" == "invalid" ] && return 0
72 [ "${location}" == "${srcdir}" ] && return 0
76 str="${ftype} ${name} ${location} ${str}"
80 local maj=$(LC_ALL=C ls -l "${location}" | \
81 gawk '{sub(/,/, "", $5); print $5}')
82 local min=$(LC_ALL=C ls -l "${location}" | \
85 if [ -b "${location}" ]; then
90 str="${ftype} ${name} ${str} ${dev_type} ${maj} ${min}"
93 local target=$(LC_ALL=C ls -l "${location}" | \
95 str="${ftype} ${name} ${target} ${str}"
98 str="${ftype} ${name} ${str}"
109 printf "$0 [ [-u <root_uid>] [-g <root_gid>] [-d | <cpio_source>] ] . . .\n"
111 printf -- "-u <root_uid> User ID to map to user ID 0 (root).\n"
112 printf " <root_uid> is only meaningful if <cpio_source>\n"
113 printf " is a directory.\n"
114 printf -- "-g <root_gid> Group ID to map to group ID 0 (root).\n"
115 printf " <root_gid> is only meaningful if <cpio_source>\n"
116 printf " is a directory.\n"
117 printf "<cpio_source> File list or directory for cpio archive.\n"
118 printf " If <cpio_source> is not provided then a\n"
119 printf " a default list will be output.\n"
120 printf -- "-d Output the default cpio list. If no <cpio_source>\n"
121 printf " is given then the default cpio list will be output.\n"
123 printf "All options may be repeated and are interpreted sequentially\n"
124 printf "and immediately. -u and -g states are preserved across\n"
125 printf "<cpio_source> options so an explicit \"-u 0 -g 0\" is required\n"
126 printf "to reset the root/group mapping.\n"
130 printf "\n#####################\n# $cpio_source\n"
132 if [ -f "$cpio_source" ]; then
133 print_mtime "$cpio_source"
135 elif [ -d "$cpio_source" ]; then
136 srcdir=$(echo "$cpio_source" | sed -e 's://*:/:g')
137 dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" 2>/dev/null)
139 # If $dirlist is only one line, then the directory is empty
140 if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
141 print_mtime "$cpio_source"
143 echo "${dirlist}" | \
148 # Failsafe in case directory is empty
152 echo " $0: Cannot open '$cpio_source'" >&2
161 while [ $# -gt 0 ]; do
184 printf "ERROR: unknown option \"$arg\"\n" >&2
185 printf "If the filename validly begins with '-', then it must be prefixed\n" >&2
186 printf "by './' so that it won't be interpreted as an option." >&2
200 # spit out the default cpio list if a source hasn't been specified
201 [ -z "$cpio_source" -a -z "$default_list" ] && default_initramfs