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