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