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