Patch by Steven Shiau <steven@nchc.org.tw>. The only changes so far are:
[live-boot-grml.git] / components / 9990-main.sh
1 #!/bin/sh
2
3 # set -e
4
5 Live ()
6 {
7         if [ -x /scripts/local-top/cryptroot ]
8         then
9                 /scripts/local-top/cryptroot
10         fi
11
12         exec 6>&1
13         exec 7>&2
14         exec > boot.log
15         exec 2>&1
16         tail -f boot.log >&7 &
17         tailpid="${!}"
18
19         LIVE_BOOT_CMDLINE="${LIVE_BOOT_CMDLINE:-$(cat /proc/cmdline)}"
20         Cmdline_old
21
22         Debug
23
24         Read_only
25
26         Select_eth_device
27
28         if [ -e /conf/param.conf ]
29         then
30                 . /conf/param.conf
31         fi
32
33         # Needed here too because some things (*cough* udev *cough*)
34         # changes the timeout
35
36         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
37         then
38                 if do_netmount
39                 then
40                         livefs_root="${mountpoint}"
41                 else
42                         panic "Unable to find a live file system on the network"
43                 fi
44         else
45                 if [ -n "${ISCSI_PORTAL}" ]
46                 then
47                         do_iscsi && livefs_root="${mountpoint}"
48                 elif [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
49                 then
50                         # Do a local boot from hd
51                         livefs_root=${ROOT}
52                 else
53                         if [ -x /usr/bin/memdiskfind ]
54                         then
55                                 MEMDISK=$(/usr/bin/memdiskfind)
56
57                                 if [ $? -eq 0 ]
58                                 then
59                                         # We found a memdisk, set up phram
60                                         modprobe phram phram=memdisk,${MEMDISK}
61
62                                         # Load mtdblock, the memdisk will be /dev/mtdblock0
63                                         modprobe mtdblock
64                                 fi
65                         fi
66
67                         # Scan local devices for the image
68                         i=0
69                         while [ "$i" -lt 60 ]
70                         do
71                                 livefs_root=$(find_livefs ${i})
72
73                                 if [ -n "${livefs_root}" ]
74                                 then
75                                         break
76                                 fi
77
78                                 sleep 1
79                                 i="$(($i + 1))"
80                         done
81                 fi
82         fi
83
84         if [ -z "${livefs_root}" ]
85         then
86                 panic "Unable to find a medium containing a live file system"
87         fi
88
89         Verify_checksums "${livefs_root}"
90
91         if [ "${TORAM}" ]
92         then
93                 live_dest="ram"
94         elif [ "${TODISK}" ]
95         then
96                 live_dest="${TODISK}"
97         fi
98
99         if [ "${live_dest}" ]
100         then
101                 log_begin_msg "Copying live media to ${live_dest}"
102                 copy_live_to "${livefs_root}" "${live_dest}"
103                 log_end_msg
104         fi
105
106         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
107         # because the mountpoint is left behind in /proc/mounts, so let's get
108         # rid of it when running from RAM
109         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
110         then
111                 losetup -d /dev/loop0
112
113                 if is_mountpoint /live/fromiso
114                 then
115                         umount /live/fromiso
116                         rmdir --ignore-fail-on-non-empty /live/fromiso \
117                                 >/dev/null 2>&1 || true
118                 fi
119         fi
120
121         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
122         then
123                 setup_unionfs "${livefs_root}" "${rootmnt}"
124         else
125                 mac="$(get_mac)"
126                 mac="$(echo ${mac} | sed 's/-//g')"
127                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
128         fi
129
130         # At this point /root should contain the final root filesystem.
131         # Move all mountpoints below /live into /root/lib/live/mount.
132         # This has to be done after mounting the root filesystem to /
133         # otherwise these mount points won't be accessible from the running system.
134         for _MOUNT in $(cat /proc/mounts | cut -f 2 -d " " | grep -e "^/live/")
135         do
136                 local newmount
137                 newmount="${rootmnt}/lib/live/mount/${_MOUNT#/live/}"
138                 mkdir -p "${newmount}"
139                 mount -o move "${_MOUNT}" "${newmount}" > /dev/null 2>&1 || \
140                 mount -o bind "${_MOUNT}" "${newmount}" > /dev/null || \
141                 log_warning_msg "W: failed to move or bindmount ${_MOUNT} to ${newmount}"
142         done
143
144         if [ -n "${ROOT_PID}" ]
145         then
146                 echo "${ROOT_PID}" > "${rootmnt}"/lib/live/root.pid
147         fi
148
149         log_end_msg
150
151         # aufs2 in kernel versions around 2.6.33 has a regression:
152         # directories can't be accessed when read for the first the time,
153         # causing a failure for example when accessing /var/lib/fai
154         # when booting FAI, this simple workaround solves it
155         ls /root/* >/dev/null 2>&1
156
157         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
158         # because the mountpoint is left behind in /proc/mounts, so let's get
159         # rid of it when running from RAM
160         if [ -n "$FINDISO" ] && [ "${TORAM}" ]
161         then
162                 losetup -d /dev/loop0
163
164                 if is_mountpoint /root/lib/live/mount/findiso
165                 then
166                         umount /root/lib/live/mount/findiso
167                         rmdir --ignore-fail-on-non-empty /root/lib/live/mount/findiso \
168                                 >/dev/null 2>&1 || true
169                 fi
170         fi
171
172         if [ -L /root/etc/resolv.conf ] ; then
173                 # assume we have resolvconf
174                 DNSFILE="${rootmnt}/etc/resolvconf/resolv.conf.d/base"
175         else
176                 DNSFILE="${rootmnt}/etc/resolv.conf"
177         fi
178         if [ -f /etc/resolv.conf ] && [ ! -s ${DNSFILE} ]
179         then
180                 log_begin_msg "Copying /etc/resolv.conf to ${DNSFILE}"
181                 cp -v /etc/resolv.conf ${DNSFILE}
182                 log_end_msg
183         fi
184
185         if ! [ -d "/lib/live/boot" ]
186         then
187                 panic "A wrong rootfs was mounted."
188         fi
189
190         Fstab
191         Netbase
192
193         Swap
194
195         exec 1>&6 6>&-
196         exec 2>&7 7>&-
197         kill ${tailpid}
198         [ -w "${rootmnt}/var/log/" ] && mkdir -p "${rootmnt}/var/log/live" && cp boot.log "${rootmnt}/var/log/live" 2>/dev/null
199 }