reviewed to ensure maximal coverage of forensics traces
[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 if [ -x /sbin/start_udev ]; then
19     verbose-message "pl_sysinit: starting udev daemon"
20     verbose-run /sbin/start_udev
21 else
22     verbose-message "pl_sysinit: WARNING cannot start udev daemon - /sbin/start_udev NOT FOUND"
23 fi
24
25 verbose-message "pl_sysinit: invoking hardware initialization script"
26 verbose-run /etc/init.d/pl_hwinit
27
28 function check-initrd() {
29     _mounted=0
30     if [ -f /usr/isolinux/pl_version ] ; then
31         # it is 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         else
39             verbose-message "pl_sysinit: WARNING pl_version mismatch"
40         fi
41     fi
42     return $_mounted
43 }
44
45 function check-block-devices() {
46     _mounted=0
47     # so that * expands to empty string if there are no block devices
48     shopt -s nullglob
49
50     for device in /sys/block/*; do
51         device=$(/bin/basename $device)
52
53         # skipping any devices that start with md or ra (ram) or lo
54         # (loop) or fd (floppy)
55
56         start_device=${device:0:2}
57         if [ "$start_device" == "ra" ] || [ "$start_device" == "md" ] ||
58             [ "$start_device" == "lo" ] || [ "$start_device" == "fd" ]; then
59             continue
60         fi
61
62         # If this is a removable (e.g., USB flash) device, then try to
63         # look for an ISO image on each of its partitions.
64         if [ "$(cat /sys/block/$device/removable)" = "1" ] ; then
65             partitions=$(/bin/awk "\$4 ~ /${device}[0-9]*/ { print \$4 }" /proc/partitions)
66             for partition in $partitions ; do
67                 echo $(date "+%H:%M:%S") "pl_sysinit: checking $partition for iso image"
68                 mkdir -p /tmp/$partition
69                 if /bin/mount -o ro -t msdos,ext2 /dev/$partition /tmp/$partition 2>&1 > /dev/null ; then
70                 # Look for the first ISO image
71                     for iso in /tmp/$partition/*.iso ; do
72                         if /sbin/losetup /dev/loop0 $iso ; then
73                             echo $(date "+%H:%M:%S") "pl_sysinit: using $(basename $iso) on $partition"
74                             device="loop0"
75                             break
76                         fi
77                     done
78                     if [ "$device" != "loop0" ] ; then
79                         /bin/umount /tmp/$partition 2>&1 > /dev/null
80                     fi
81                 fi
82             done
83         fi
84         
85         verbose-message "pl_sysinit: checking $device for /usr contents"
86         /bin/mount -o ro -t iso9660 /dev/$device /usr 2>&1 > /dev/null
87         if [ $? -eq 0 ]; then
88         # it mounted, but we should probably make sure its our boot cd
89         # this can be done by making sure the /pl_version file (on initrd)
90         # matches /usr/isolinux/pl_version
91             initrd_version=$(/bin/cat /pl_version)
92             cd_version=$(/bin/cat /usr/isolinux/pl_version)
93
94             if [ "$initrd_version" != "$cd_version" ]; then
95             # eh, wrong cd, keep trying
96                 /bin/umount /usr 2>&1 /dev/null
97             else
98                 verbose-message "pl_sysinit: found CD and mounted on /usr"
99                 _mounted=1
100                 break
101             fi
102         fi
103     done
104     return $_mounted
105 }
106
107 verbose-message "pl_sysinit: finding CD to mount on /usr"
108 mounted=0
109 check-initrd
110 if [ $? -eq 1 ]; then
111     mounted=1
112 else
113     [ ! -d /usr ] && mkdir /usr
114     check-block-devices
115     [ $? -eq 1 ] && mounted=1
116 fi
117
118 if [ $mounted -eq 0 ]; then
119     hang-and-shutdown  "pl_sysinit: ERROR - unable to find boot CD"
120     exit 1
121 fi
122
123 # parts of this were copied from devmap_mknod.sh from the device-mapper
124 # source. Since fedora decided not to include it in the rpm, it is 
125 # being copied here
126 function create-device-mapper-node() {
127
128     DM_DIR="mapper"
129     DM_NAME="device-mapper"
130     DIR="/dev/$DM_DIR"
131     CONTROL="$DIR/control"
132
133     MAJOR=$(sed -n 's/^ *\([0-9]\+\) \+misc$/\1/p' /proc/devices)
134     MINOR=$(sed -n "s/^ *\([0-9]\+\) \+$DM_NAME\$/\1/p" /proc/misc)
135     
136     if [ -n "$MAJOR" ] && [ -n "$MINOR" ]; then
137         /bin/mkdir -p --mode=755 $DIR
138         /bin/rm -f $CONTROL
139         /bin/mknod --mode=600 $CONTROL c $MAJOR $MINOR
140     else
141         verbose-message "pl_sysinit: unable to create device mapper control node, continuing"
142     fi
143 }
144
145 verbose-message "pl_sysinit: creating device mapper control node"
146 create-device-mapper-node
147
148 verbose-message "pl_sysinit: configuring kernel parameters"
149 verbose-run  /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 if [ -x $syslog ]; then
154     verbose-message "pl_sysinit: starting rsyslog"
155     verbose-run  $syslog start
156 else
157     verbose-message "pl_sysinit: WARNING cannot start rsyslog"
158 fi
159
160 verbose-message "pl_sysinit: bringing network online"
161 verbose-run  /etc/init.d/pl_netinit
162
163 function 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     # save ntp clock to hardware
182     /sbin/hwclock --systohc --utc
183 }
184
185 verbose-message "pl_sysinit: attempting to sync clock"
186 sync-clock