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                         # Scan local devices for the image
84                         i=0
85                         while [ "$i" -lt 60 ]
86                         do
87                                 livefs_root=$(find_livefs ${i})
88
89                                 if [ -n "${livefs_root}" ]
90                                 then
91                                         break
92                                 fi
93
94                                 sleep 1
95                                 i=$((i + 1))
96                         done
97                 fi
98         fi
99
100         if [ -z "${livefs_root}" ]
101         then
102                 panic "Unable to find a medium containing a live file system"
103         fi
104
105         Verify_checksums "${livefs_root}"
106
107         if [ "${TORAM}" ]
108         then
109                 live_dest="ram"
110         elif [ "${TODISK}" ]
111         then
112                 live_dest="${TODISK}"
113         fi
114
115         if [ "${live_dest}" ]
116         then
117                 log_begin_msg "Copying live media to ${live_dest}"
118                 copy_live_to "${livefs_root}" "${live_dest}"
119                 log_end_msg
120         fi
121
122         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
123         # because the mountpoint is left behind in /proc/mounts, so let's get
124         # rid of it when running from RAM
125         if [ -n "$FROMISO" ] && [ "${TORAM}" ]
126         then
127                 losetup -d /dev/loop0
128
129                 if is_mountpoint /run/live/fromiso
130                 then
131                         umount /run/live/fromiso
132                         rmdir --ignore-fail-on-non-empty /run/live/fromiso \
133                                 >/dev/null 2>&1 || true
134                 fi
135         fi
136
137         if [ -n "${MODULETORAMFILE}" ] || [ -n "${PLAIN_ROOT}" ]
138         then
139                 setup_unionfs "${livefs_root}" "${rootmnt?}"
140         else
141                 mac="$(get_mac)"
142                 mac="$(echo "${mac}" | sed 's/-//g')"
143                 mount_images_in_directory "${livefs_root}" "${rootmnt}" "${mac}"
144         fi
145
146         if [ -n "${ROOT_PID}" ]
147         then
148                 echo "${ROOT_PID}" > "${rootmnt}"/lib/live/root.pid
149         fi
150
151         log_end_msg
152
153         # aufs2 in kernel versions around 2.6.33 has a regression:
154         # directories can't be accessed when read for the first the time,
155         # causing a failure for example when accessing /var/lib/fai
156         # when booting FAI, this simple workaround solves it
157         ls /root/* >/dev/null 2>&1
158
159         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
160         # because the mountpoint is left behind in /proc/mounts, so let's get
161         # rid of it when running from RAM
162         if [ -n "$FINDISO" ] && [ "${TORAM}" ]
163         then
164                 losetup -d /dev/loop0
165
166                 if is_mountpoint /run/live/findiso
167                 then
168                         umount /run/live/findiso
169                         rmdir --ignore-fail-on-non-empty /run/live/findiso \
170                                 >/dev/null 2>&1 || true
171                 fi
172         fi
173
174         if [ -f /etc/hostname ] && ! grep -E -q -v '^[[:space:]]*(#|$)' "${rootmnt}/etc/hostname"
175         then
176                 log_begin_msg "Copying /etc/hostname to ${rootmnt}/etc/hostname"
177                 cp -v /etc/hostname "${rootmnt}/etc/hostname"
178                 log_end_msg
179         fi
180
181         if [ -f /etc/hosts ] && ! grep -E -q -v '^[[:space:]]*(#|$|(127.0.0.1|::1|ff02::[12])[[:space:]])' "${rootmnt}/etc/hosts"
182         then
183                 log_begin_msg "Copying /etc/hosts to ${rootmnt}/etc/hosts"
184                 cp -v /etc/hosts "${rootmnt}/etc/hosts"
185                 log_end_msg
186         fi
187
188         if [ -L /root/etc/resolv.conf ] ; then
189                 # assume we have resolvconf
190                 DNSFILE="${rootmnt}/etc/resolvconf/resolv.conf.d/base"
191         else
192                 DNSFILE="${rootmnt}/etc/resolv.conf"
193         fi
194         if [ -f /etc/resolv.conf ] && ! grep -E -q -v '^[[:space:]]*(#|$)' "${DNSFILE}"
195         then
196                 log_begin_msg "Copying /etc/resolv.conf to ${DNSFILE}"
197                 cp -v /etc/resolv.conf "${DNSFILE}"
198                 log_end_msg
199         fi
200
201         if ! [ -d "/lib/live/boot" ]
202         then
203                 panic "A wrong rootfs was mounted."
204         fi
205
206         # avoid breaking existing user scripts that rely on the old path
207         # this includes code that checks what is mounted on /lib/live/mount/*
208         # (eg: grep /lib/live /proc/mount)
209         # XXX: to be removed before the bullseye release
210         mkdir -p "${rootmnt}/lib/live/mount"
211         mount --rbind /run/live "${rootmnt}/lib/live/mount"
212
213         Fstab
214
215         Swap
216
217         if grep -q debian_networking /proc/cmdline ; then
218                 Netbase
219         else
220                 Grml_Networking
221         fi
222
223         exec 1>&6 6>&-
224         exec 2>&7 7>&-
225         kill ${tailpid}
226         [ -w "${rootmnt}/var/log/" ] && mkdir -p "${rootmnt}/var/log/live" && ( \
227                                 cp boot.log "${rootmnt}/var/log/live" 2>/dev/null; \
228                                 cp fsck.log "${rootmnt}/var/log/live" 2>/dev/null )
229 }