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