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