4 # initrd builder for network booting
6 # Utility function to determine whether or not a filesystem is usable for
7 # loopback mounts. Lifted verbatim from Erik Troan's mkinitrd script.
10 local parttype
= tmpname
=
12 [[ -d $dir ]] ||
return 1
13 [[ -w $dir ]] ||
return 1
14 [[ $dir == */ ]] && dir
=${dir%/}
15 parttype
=$
(awk "{if (\$2 == \""$dir"\") print \$3 }" /proc
/mounts
)
17 while tmpname
=${dir%/*} && [[ -z $parttype ]];do
18 [[ -z $tmpname ]] && tmpname
=/
19 parttype
=$
(awk "{if (\$2 == \""$tmpname"\") print \$3 }" /proc
/mounts
)
29 # Find a suitable temporary directory (i.e. not tmpfs or nfs)
30 if is_good_fs
$TMPDIR; then
32 elif is_good_fs
/tmp
; then
34 elif is_good_fs
/var
/tmp
; then
36 elif is_good_fs
/root
/tmp
; then
39 echo "Cannot use a tmp directory" >&2
43 # Default settings (some can be overridden by command-line options)
44 include_modules
=include-modules
45 initrd_skel
=/usr
/lib
/mkinitrd-net
/initrd-skel
49 output_dir
=/var
/lib
/tftpboot
53 # No need to use sudo if we are root
54 if [ $UID -eq 0 ]; then
58 USAGE
="Usage: $0 [-k|--kernel <kernel_ver>] [-n|--nolink] [-q|--quiet] [-l|--local] [--nosudo] [--keep] [--help] module_list ..."
60 # Parse command-line options
61 while [ $# -gt 0 ]; do
70 --nosudo) shift ; use_sudo
=n
;;
71 --keep) shift ; keep
=y
;;
73 shift ; make_link
=n
;;
74 -q|
--quiet) shift ; quiet
=-q ;;
75 --help) shift ; do_help
=y
;;
77 -*) echo "${0}: ${1}: invalid option" >&2
84 # Build list of requested modules
86 requested_modules
="$modules"
87 modules
="$modules nfs" # Always require nfs for nfs mount
88 modules
="$modules af_packet" # Always require af_packet for udhcpc
90 # --help => Print help message
91 if [ "$do_help" == "y" ]; then
93 echo " -k, --kernel Specify kernel version"
94 echo " -n, --nolink Do not create a matching symbolic link"
95 echo " -l, --local Run locally from CVS (for developers only)"
96 echo " --nosudo Do not use sudo (i.e. must run as root instead)"
97 echo " --keep Keep temporary files instead of deleting them"
101 # --local => we are running directly from CVS, rather than
102 # from an installed copy, so use local files and directories
103 if [ "$use_local" == "y" ]; then
104 include_modules
=.
/include-modules
105 initrd_skel
=initrd-skel
109 # If use_sudo is set, check that sudo exists
111 if [ "$use_sudo" == "y" ]; then
112 if [ ! -x $sudo ]; then
114 echo "WARNING: --nosudo not specified but $sudo not found"
117 if [ "$use_sudo" == "n" ]; then
121 # Create temporary working files
122 initrd
=`mktemp -d ${tmpdir}/initrd.XXXXXX`
123 initrdimg
=`mktemp ${tmpdir}/initrd.img.XXXXXX`
124 initrdmnt
=`mktemp -d ${tmpdir}/initrd.mnt.XXXXXX`
126 # Copy skeleton into temporary area
127 cp -a $initrd_skel/* $initrd/
128 mkdir
-p $initrd/lib
/modules
/$kernel_ver
129 $include_modules $quiet -k $kernel_ver -d $initrd/lib
/modules
/$kernel_ver $modules > $initrd/bin
/insert-modules ||
exit 1
130 chmod 755 $initrd/bin
/insert-modules
132 # Create empty ext2fs image file
133 dd if=/dev
/zero bs
=1k of
=$initrdimg count
=$
((`du -sk $initrd | cut -f1` * 7 / 6)) 2> /dev
/null
134 /sbin
/mke2fs
-q -F $initrdimg 2> /dev
/null
136 # Mount image file, copy files on, create /dev entries, display free space, umount
137 $sudo mount
-o loop
$initrdimg $initrdmnt
138 cp -a $initrd/* $initrdmnt/
139 $sudo mknod
$initrdmnt/dev
/console c
5 1
140 $sudo mknod
$initrdmnt/dev
/null c
1 3
141 $sudo mknod
$initrdmnt/dev
/ram b
1 1
142 $sudo mknod
$initrdmnt/dev
/systty c
4 0
143 for i
in 1 2 3 4; do $sudo mknod
$initrdmnt/dev
/tty
$i c
4 $i; done
144 if [ "$quiet" == "n" ]; then
147 $sudo umount
$initrdmnt
150 initrd_suffix
=`echo $requested_modules | tr " " .`
151 gzip -9 -n -c $initrdimg > $output_dir/initrd-
$initrd_suffix.
$kernel_ver.img
154 if [ "$make_link" == "y" ]; then
155 link
=$output_dir/initrd-
$initrd_suffix.img
156 [ -L $link ] && rm -f $link
157 ln -s initrd-
$initrd_suffix.
$kernel_ver.img
$link
160 # Remove temporary files
161 if [ "$keep" == "n" ]; then