388010bf4e937e1a2e6908cec6a86115f7ccb0cc
[bootcd.git] / initscripts / pl_sysinit
1 #!/bin/sh
2
3 ### xxx tmp debug
4
5 set -x
6 echo pl_sysinit on console > /dev/console
7 exec > /dev/console
8 echo pl_sysinit plain
9
10 ###
11 . /etc/init.d/pl_functions
12
13 echo ""
14 echo "PlanetLab BootCD - distro @PLDISTRO@ based on @FCDISTRO@"
15
16 echo ""
17 echo $(date "+%H:%M:%S") "pl_sysinit: bringing system online"
18
19 echo ""
20 echo $(date "+%H:%M:%S") "pl_sysinit: mounting file systems"
21 /bin/mount -v -a
22
23 echo ""
24 echo $(date "+%H:%M:%S") "pl_sysinit: starting udevd"
25 [ -x /sbin/start_udev ] && /sbin/start_udev
26
27 echo ""
28 echo $(date "+%H:%M:%S") "pl_sysinit: invoking hardware initialization script"
29 /etc/init.d/pl_hwinit
30
31 check_initrd()
32 {
33     _mounted=0
34     if [ -f /usr/isolinux/pl_version ] ; then
35         # it mounted, but we should probably make sure its our boot cd
36         # this can be done by making sure the /pl_version file (on initrd)
37         # matches /usr/isolinux/pl_version
38         initrd_version=$(/bin/cat /pl_version)
39         cd_version=$(/bin/cat /usr/isolinux/pl_version)
40         if [ "$initrd_version" == "$cd_version" ]; then
41             _mounted=1 
42         fi
43     fi
44     return $_mounted
45 }
46
47 check_block_devices()
48 {
49     _mounted=0
50     # so that * expands to empty string if there are no block devices
51     shopt -s nullglob
52
53     for device in /sys/block/*; do
54         device=$(/bin/basename $device)
55
56         # skipping any devices that start with md or ra (ram) or lo
57         # (loop) or fd (floppy)
58
59         start_device=${device:0:2}
60         if [ "$start_device" == "ra" ] || [ "$start_device" == "md" ] ||
61             [ "$start_device" == "lo" ] || [ "$start_device" == "fd" ]; then
62             continue
63         fi
64
65         # If this is a removable (e.g., USB flash) device, then try to
66         # look for an ISO image on each of its partitions.
67         if [ "$(cat /sys/block/$device/removable)" = "1" ] ; then
68             partitions=$(/bin/awk "\$4 ~ /${device}[0-9]*/ { print \$4 }" /proc/partitions)
69             for partition in $partitions ; do
70                 echo $(date "+%H:%M:%S") "pl_sysinit: checking $partition for iso image"
71                 mkdir -p /tmp/$partition
72                 if /bin/mount -o ro -t msdos,ext2 /dev/$partition /tmp/$partition 2>&1 > /dev/null ; then
73                 # Look for the first ISO image
74                     for iso in /tmp/$partition/*.iso ; do
75                         if /sbin/losetup /dev/loop0 $iso ; then
76                             echo $(date "+%H:%M:%S") "pl_sysinit: using $(basename $iso) on $partition"
77                             device="loop0"
78                             break
79                         fi
80                     done
81                     if [ "$device" != "loop0" ] ; then
82                         /bin/umount /tmp/$partition 2>&1 > /dev/null
83                     fi
84                 fi
85             done
86         fi
87         
88         echo $(date "+%H:%M:%S") "pl_sysinit: checking $device for /usr contents"
89         /bin/mount -o ro -t iso9660 /dev/$device /usr 2>&1 > /dev/null
90         if [ $? -eq 0 ]; then
91         # it mounted, but we should probably make sure its our boot cd
92         # this can be done by making sure the /pl_version file (on initrd)
93         # matches /usr/isolinux/pl_version
94             initrd_version=$(/bin/cat /pl_version)
95             cd_version=$(/bin/cat /usr/isolinux/pl_version)
96
97             if [ "$initrd_version" != "$cd_version" ]; then
98             # eh, wrong cd, keep trying
99                 /bin/umount /usr 2>&1 /dev/null
100             else
101                 echo $(date "+%H:%M:%S") "pl_sysinit: found cd and mounted on /usr"
102                 _mounted=1
103                 break
104             fi
105         fi
106     done
107     return $_mounted
108 }
109
110 echo $(date "+%H:%M:%S") "pl_sysinit: finding cd to mount on /usr"
111 mounted=0
112 check_initrd
113 if [ $? -eq 1 ]; then
114     mounted=1
115 else
116     [ ! -d /usr ] && mkdir /usr
117     check_block_devices
118     [ $? -eq 1 ] && mounted=1
119 fi
120
121 if [ $mounted -eq 0 ]; then
122     echo $(date "+%H:%M:%S") "pl_sysinit: unable to find boot cdrom, cannot continue."
123     # todo: maybe we can put a staticly linked sshd here
124     /sbin/shutdown -h now
125 fi
126
127
128 # parts of this were copied from devmap_mknod.sh from the device-mapper
129 # source. Since fedora decided not to include it in the rpm, it is 
130 # being copied here
131 echo $(date "+%H:%M:%S") "pl_sysinit: creating device mapper control node"
132
133 DM_DIR="mapper"
134 DM_NAME="device-mapper"
135 DIR="/dev/$DM_DIR"
136 CONTROL="$DIR/control"
137
138 MAJOR=$(sed -n 's/^ *\([0-9]\+\) \+misc$/\1/p' /proc/devices)
139 MINOR=$(sed -n "s/^ *\([0-9]\+\) \+$DM_NAME\$/\1/p" /proc/misc)
140
141 if [ -n "$MAJOR" ] && [ -n "$MINOR" ]; then
142     /bin/mkdir -p --mode=755 $DIR
143     /bin/rm -f $CONTROL
144     /bin/mknod --mode=600 $CONTROL c $MAJOR $MINOR
145 else
146     echo $(date "+%H:%M:%S") "pl_sysinit: unable to create device mapper control node, continuing"
147 fi
148
149 echo ""
150 echo $(date "+%H:%M:%S") "pl_sysinit: configuring kernel parameters"
151 /sbin/sysctl -e -p /etc/sysctl.conf
152
153 # startup rsyslog if available (we're *NOT* running the standard rc)
154 syslog=/etc/rc.d/init.d/rsyslog
155 [ -x $syslog ] && $syslog start
156
157 echo ""
158 echo $(date "+%H:%M:%S") "pl_sysinit: bringing network online"
159 /etc/init.d/pl_netinit
160
161 # just in case, sometimes we're seeing weird stuff already at this point
162 pl_network_sanity_checks
163
164 echo ""
165 echo $(date "+%H:%M:%S") "pl_sysinit: attempting to sync clock"
166 /usr/sbin/ntpdate -b -t 10 -u pool.ntp.org
167
168 # Handle /dev/rtc name change for newer kernels
169 # otherwise hwclock fails
170 baseMaj=`uname -r | cut -d "." -f1`
171 baseMin=`uname -r | cut -d "." -f2`
172 vers=`uname -r | cut -d "." -f3 | cut -d "-" -f1`
173 if [ $baseMaj -eq 2 ];then
174     if [ $baseMin -eq 6 ]; then
175         if [ $vers -ge 32 ];then
176             if [ "$(readlink /dev/rtc)" != "/dev/rtc0" ]; then
177                 rm -f /dev/rtc
178                 ln -s /dev/rtc0 /dev/rtc
179             fi
180         fi
181     fi
182 fi
183
184 # save ntp clock to hardware
185 /sbin/hwclock --systohc --utc