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