untabified all init scripts
[bootcd.git] / initscripts / pl_sysinit
1 #!/bin/bash
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 verbose-message "PlanetLab BootCD - distro @PLDISTRO@ based on @FCDISTRO@"
12
13 verbose-message "pl_sysinit: bringing system online"
14
15 verbose-message "pl_sysinit: mounting file systems"
16 verbose-run /bin/mount -v -a
17
18 # on old systems that use init, we totally take control and replace
19 # /etc/inittab with just our stuff, so nothing is going to run udev
20 # for us and we need to start it ourselves
21 if [ -x /sbin/start_udev ]; then
22     verbose-message "pl_sysinit: starting udev daemon"
23     verbose-run /sbin/start_udev
24 else
25     # on systemd-based systems, udev is triggered by systemd
26     # but just in case we issue a warning
27     verbose-message "pl_sysinit: WARNING cannot start udev daemon (probably safe under systemd)"
28 fi
29
30 verbose-message "pl_sysinit: invoking hardware initialization script"
31 verbose-run /etc/init.d/pl_hwinit
32
33 function check-initrd() {
34     _mounted=0
35     if [ -f /usr/isolinux/pl_version ] ; then
36         # it is 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         else
44             verbose-message "pl_sysinit: WARNING pl_version mismatch"
45         fi
46     fi
47     return $_mounted
48 }
49
50 function check-block-devices() {
51     _mounted=0
52     # so that * expands to empty string if there are no block devices
53     shopt -s nullglob
54
55     for device in /sys/block/*; do
56         device=$(/bin/basename $device)
57
58         # skipping any devices that start with md or ra (ram) or lo
59         # (loop) or fd (floppy)
60
61         start_device=${device:0:2}
62         if [ "$start_device" == "ra" ] || [ "$start_device" == "md" ] ||
63             [ "$start_device" == "lo" ] || [ "$start_device" == "fd" ]; then
64             continue
65         fi
66
67         # If this is a removable (e.g., USB flash) device, then try to
68         # look for an ISO image on each of its partitions.
69         if [ "$(cat /sys/block/$device/removable)" = "1" ] ; then
70             partitions=$(/bin/awk "\$4 ~ /${device}[0-9]*/ { print \$4 }" /proc/partitions)
71             for partition in $partitions ; do
72                 echo $(date "+%H:%M:%S") "pl_sysinit: checking $partition for iso image"
73                 mkdir -p /tmp/$partition
74                 if /bin/mount -o ro -t msdos,ext2 /dev/$partition /tmp/$partition 2>&1 > /dev/null ; then
75                 # Look for the first ISO image
76                     for iso in /tmp/$partition/*.iso ; do
77                         if /sbin/losetup /dev/loop0 $iso ; then
78                             echo $(date "+%H:%M:%S") "pl_sysinit: using $(basename $iso) on $partition"
79                             device="loop0"
80                             break
81                         fi
82                     done
83                     if [ "$device" != "loop0" ] ; then
84                         /bin/umount /tmp/$partition 2>&1 > /dev/null
85                     fi
86                 fi
87             done
88         fi
89         
90         verbose-message "pl_sysinit: checking $device for /usr contents"
91         /bin/mount -o ro -t iso9660 /dev/$device /usr 2>&1 > /dev/null
92         if [ $? -eq 0 ]; then
93         # it mounted, but we should probably make sure its our boot cd
94         # this can be done by making sure the /pl_version file (on initrd)
95         # matches /usr/isolinux/pl_version
96             initrd_version=$(/bin/cat /pl_version)
97             cd_version=$(/bin/cat /usr/isolinux/pl_version)
98
99             if [ "$initrd_version" != "$cd_version" ]; then
100             # eh, wrong cd, keep trying
101                 /bin/umount /usr 2>&1 /dev/null
102             else
103                 verbose-message "pl_sysinit: found CD and mounted on /usr"
104                 _mounted=1
105                 break
106             fi
107         fi
108     done
109     return $_mounted
110 }
111
112 verbose-message "pl_sysinit: finding CD to mount on /usr"
113 mounted=0
114 check-initrd
115 if [ $? -eq 1 ]; then
116     mounted=1
117 else
118     [ ! -d /usr ] && mkdir /usr
119     check-block-devices
120     [ $? -eq 1 ] && mounted=1
121 fi
122
123 if [ $mounted -eq 0 ]; then
124     hang-and-shutdown  "pl_sysinit: ERROR - unable to find boot CD"
125     exit 1
126 fi
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 function create-device-mapper-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         verbose-message "pl_sysinit: unable to create device mapper control node, continuing"
147     fi
148 }
149
150 verbose-message "pl_sysinit: creating device mapper control node"
151 create-device-mapper-node
152
153 verbose-message "pl_sysinit: configuring kernel parameters"
154 verbose-run  /sbin/sysctl -e -p /etc/sysctl.conf
155
156 # startup rsyslog if available (we're *NOT* running the standard rc)
157 syslog=/etc/rc.d/init.d/rsyslog
158 if [ -x $syslog ]; then
159     verbose-message "pl_sysinit: starting rsyslog"
160     verbose-run  $syslog start
161 else
162     verbose-message "pl_sysinit: WARNING cannot start rsyslog"
163 fi
164
165 verbose-message "pl_sysinit: bringing network online"
166 verbose-run  /etc/init.d/pl_netinit
167
168 function sync-clock() {
169     /usr/sbin/ntpdate -b -t 10 -u pool.ntp.org
170
171     # Handle /dev/rtc name change for newer kernels
172     # otherwise hwclock fails
173     baseMaj=`uname -r | cut -d "." -f1`
174     baseMin=`uname -r | cut -d "." -f2`
175     vers=`uname -r | cut -d "." -f3 | cut -d "-" -f1`
176     if [ $baseMaj -eq 2 ];then
177         if [ $baseMin -eq 6 ]; then
178             if [ $vers -ge 32 ];then
179                 if [ "$(readlink /dev/rtc)" != "/dev/rtc0" ]; then
180                     rm -f /dev/rtc
181                     ln -s /dev/rtc0 /dev/rtc
182                 fi
183             fi
184         fi
185     fi
186     # save ntp clock to hardware
187     /sbin/hwclock --systohc --utc
188 }
189
190 verbose-message "pl_sysinit: attempting to sync clock"
191 sync-clock