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