Release new version 1:20210208+grml.5
[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 [ -n "${NETBOOT}" ] || [ -n "${FETCH}" ] || [ -n "${HTTPFS}" ] || [ -n "${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                                 if ! dd if=/dev/mem of=/dev/zero bs=1 count=1 >/dev/null 2>&1
56                                 then
57                                         log_begin_msg "access to /dev/mem is restriced, skipping memdiskfind"
58                                         log_end_msg
59                                 else
60                                         if MEMDISK=$(/usr/bin/memdiskfind)
61                                         then
62                                                 # We found a memdisk, set up phram
63                                                 # Sometimes "modprobe phram" can not successfully create /dev/mtd0.
64                                                 # Have to try several times.
65                                                 max_try=20
66                                                 while [ ! -c /dev/mtd0 ] && [ "$max_try" -gt 0 ]; do
67                                                   modprobe phram "phram=memdisk,${MEMDISK}"
68                                                   sleep 0.2
69                                                   if [ -c /dev/mtd0 ]; then
70                                                         break
71                                                   else
72                                                         rmmod phram
73                                                   fi
74                                                   max_try=$((max_try - 1))
75                                                 done
76
77                                                 # Load mtdblock, the memdisk will be /dev/mtdblock0
78                                                 modprobe mtdblock
79                                         fi
80                                 fi
81                         fi
82
83                         # If the live media location is given via command line and access to it
84                         # involves LVM volumes, the corresponding volumes need to be activated.
85                         IFS=','
86                         for dev in $(echo "$LIVE_MEDIA")
87                         do
88                                 case "$dev" in
89                                 /dev/mapper/*)
90                                         eval $(dmsetup splitname --nameprefixes --noheadings --rows "${dev#/dev/mapper/}")
91                                         if [ "$DM_VG_NAME" ] && [ "$DM_LV_NAME" ]
92                                         then
93                                                 lvm lvchange -aay -y --sysinit --ignoreskippedcluster "$DM_VG_NAME/$DM_LV_NAME"
94                                         fi
95                                         ;;
96                                 /dev/*/*)
97                                         # Could be /dev/VG/LV; use lvs to check
98                                         if lvm lvs -- "$dev" >/dev/null 2>&1
99                                         then
100                                                 lvm lvchange -aay -y --sysinit --ignoreskippedcluster "$dev"
101                                         fi
102                                         ;;
103                                 esac
104                         done
105                         unset IFS
106
107                         # Scan local devices for the image
108                         i=0
109                         while [ "$i" -lt 60 ]
110                         do
111                                 livefs_root=$(find_livefs ${i})
112
113                                 if [ -n "${livefs_root}" ]
114                                 then
115                                         break
116                                 fi
117
118                                 sleep 1
119                                 i=$((i + 1))
120                         done
121                 fi
122         fi
123
124         if [ -z "${livefs_root}" ]
125         then
126                 panic "Unable to find a medium containing a live file system"
127         fi
128
129         Verify_checksums "${livefs_root}"
130
131         if [ "${TORAM}" ]
132         then
133                 live_dest="ram"
134         elif [ "${TODISK}" ]
135         then
136                 live_dest="${TODISK}"
137         fi
138
139         if [ "${live_dest}" ]
140         then
141                 log_begin_msg "Copying live media to ${live_dest}"
142                 copy_live_to "${livefs_root}" "${live_dest}"
143                 log_end_msg
144         fi
145
146         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
147         # because the mountpoint is left behind in /proc/mounts, so let's get
148         # rid of it when running from RAM
149         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
150         then
151                 losetup -d /dev/loop0
152
153                 if is_mountpoint /run/live/fromiso
154                 then
155                         umount /run/live/fromiso
156                         rmdir --ignore-fail-on-non-empty /run/live/fromiso \
157                                 >/dev/null 2>&1 || true
158                 fi
159         fi
160
161         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
162         then
163                 setup_unionfs "${livefs_root}" "${rootmnt?}"
164         else
165                 mac="$(get_mac)"
166                 mac="$(echo "${mac}" | sed 's/-//g')"
167                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
168         fi
169
170         if [ -n "${ROOT_PID}" ]
171         then
172                 echo "${ROOT_PID}" > "${rootmnt}"/lib/live/root.pid
173         fi
174
175         log_end_msg
176
177         # aufs2 in kernel versions around 2.6.33 has a regression:
178         # directories can't be accessed when read for the first the time,
179         # causing a failure for example when accessing /var/lib/fai
180         # when booting FAI, this simple workaround solves it
181         ls /root/* >/dev/null 2>&1
182
183         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
184         # because the mountpoint is left behind in /proc/mounts, so let's get
185         # rid of it when running from RAM
186         if [ -n "$FINDISO" ] && [ "${TORAM}" ]
187         then
188                 losetup -d /dev/loop0
189
190                 if is_mountpoint /run/live/findiso
191                 then
192                         umount /run/live/findiso
193                         rmdir --ignore-fail-on-non-empty /run/live/findiso \
194                                 >/dev/null 2>&1 || true
195                 fi
196         fi
197
198         if [ -f /etc/hostname ] && ! grep -E -q -v '^[[:space:]]*(#|$)' "${rootmnt}/etc/hostname"
199         then
200                 log_begin_msg "Copying /etc/hostname to ${rootmnt}/etc/hostname"
201                 cp -v /etc/hostname "${rootmnt}/etc/hostname"
202                 log_end_msg
203         fi
204
205         if [ -f /etc/hosts ] && ! grep -E -q -v '^[[:space:]]*(#|$|(127.0.0.1|::1|ff02::[12])[[:space:]])' "${rootmnt}/etc/hosts"
206         then
207                 log_begin_msg "Copying /etc/hosts to ${rootmnt}/etc/hosts"
208                 cp -v /etc/hosts "${rootmnt}/etc/hosts"
209                 log_end_msg
210         fi
211
212         if [ -L /root/etc/resolv.conf ] ; then
213                 # assume we have resolvconf
214                 DNSFILE="${rootmnt}/etc/resolvconf/resolv.conf.d/base"
215         else
216                 DNSFILE="${rootmnt}/etc/resolv.conf"
217         fi
218         if [ -f /etc/resolv.conf ] && ! grep -E -q -v '^[[:space:]]*(#|$)' "${DNSFILE}"
219         then
220                 log_begin_msg "Copying /etc/resolv.conf to ${DNSFILE}"
221                 cp -v /etc/resolv.conf "${DNSFILE}"
222                 log_end_msg
223         fi
224
225         if ! [ -d "/lib/live/boot" ]
226         then
227                 panic "A wrong rootfs was mounted."
228         fi
229
230         # avoid breaking existing user scripts that rely on the old path
231         # this includes code that checks what is mounted on /lib/live/mount/*
232         # (eg: grep /lib/live /proc/mount)
233         # XXX: to be removed before the bullseye release
234         mkdir -p "${rootmnt}/lib/live/mount"
235         mount --rbind /run/live "${rootmnt}/lib/live/mount"
236
237         Fstab
238
239         Swap
240
241         if grep -q debian_networking /proc/cmdline ; then
242                 Netbase
243         else
244                 Grml_Networking
245         fi
246
247         exec 1>&6 6>&-
248         exec 2>&7 7>&-
249         kill ${tailpid}
250         [ -w "${rootmnt}/var/log/" ] && mkdir -p "${rootmnt}/var/log/live" && ( \
251                                 cp boot.log "${rootmnt}/var/log/live" 2>/dev/null; \
252                                 cp fsck.log "${rootmnt}/var/log/live" 2>/dev/null )
253 }