[e1000] Use PCI_BASE_ADDRESS_* symbols instead of integers
[gpxe.git] / contrib / initrd / mkinitrd-net
blob0c95ebd1bc49d231b45b2dcfcf6a979ecddbc6e0
1 #!/bin/sh
3 # $Id$
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.
9 is_good_fs() {
10 local parttype= tmpname=
11 local dir=$1
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)
20 dir=$tmpname
21 done
23 case $parttype in
24 nfs|tmpfs) return 1;;
25 *) return 0;
26 esac
29 # Find a suitable temporary directory (i.e. not tmpfs or nfs)
30 if is_good_fs $TMPDIR; then
31 tmpdir=$TMPDIR
32 elif is_good_fs /tmp; then
33 tmpdir=/tmp
34 elif is_good_fs /var/tmp; then
35 tmpdir=/var/tmp
36 elif is_good_fs /root/tmp; then
37 tmpdir=/root/tmp
38 else
39 echo "Cannot use a tmp directory" >&2
40 exit 1
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
46 kernel_ver=`uname -r`
47 use_sudo=y
48 keep=n
49 output_dir=/var/lib/tftpboot
50 make_link=y
51 quiet=
53 # No need to use sudo if we are root
54 if [ $UID -eq 0 ]; then
55 use_sudo=n
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
62 case "$1" in
63 -l|--local)
64 shift
65 use_local=y ;;
66 -k|--kernel)
67 shift
68 kernel_ver=$1
69 shift ;;
70 --nosudo) shift ; use_sudo=n ;;
71 --keep) shift ; keep=y ;;
72 --n|--nolink)
73 shift ; make_link=n ;;
74 -q|--quiet) shift ; quiet=-q ;;
75 --help) shift ; do_help=y ;;
76 --) shift ; break ;;
77 -*) echo "${0}: ${1}: invalid option" >&2
78 echo $USAGE >& 2
79 exit 2 ;;
80 *) break ;;
81 esac
82 done
84 # Build list of requested modules
85 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
92 echo $USAGE
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"
98 exit 0;
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
106 output_dir=tftpboot
109 # If use_sudo is set, check that sudo exists
110 sudo=/usr/bin/sudo
111 if [ "$use_sudo" == "y" ]; then
112 if [ ! -x $sudo ]; then
113 use_sudo=n
114 echo "WARNING: --nosudo not specified but $sudo not found"
117 if [ "$use_sudo" == "n" ]; then
118 sudo=
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
145 df -h $initrdmnt
147 $sudo umount $initrdmnt
149 # Create output file
150 initrd_suffix=`echo $requested_modules | tr " " .`
151 gzip -9 -n -c $initrdimg > $output_dir/initrd-$initrd_suffix.$kernel_ver.img
153 # Create symlink
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
162 rm -rf $initrd
163 rm -f $initrdimg
164 rmdir $initrdmnt