3ad6a7260e5396167efa03769fc7f01e44b44587
[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
23         Cmdline_old
24
25         case "${LIVE_DEBUG}" in
26                 true)
27                         set -x
28                         ;;
29         esac
30
31         case "${LIVE_READ_ONLY}" in
32                 true)
33                         Read_only
34                         ;;
35         esac
36
37         Select_eth_device
38
39         # Needed here too because some things (*cough* udev *cough*)
40         # changes the timeout
41
42         if [ ! -z "${NETBOOT}" ] || [ ! -z "${FETCH}" ] || [ ! -z "${HTTPFS}" ] || [ ! -z "${FTPFS}" ]
43         then
44                 if do_netmount
45                 then
46                         livefs_root="${mountpoint}"
47                 else
48                         panic "Unable to find a live file system on the network"
49                 fi
50         else
51                 if [ -n "${ISCSI_PORTAL}" ]
52                 then
53                         do_iscsi && livefs_root="${mountpoint}"
54                 elif [ -n "${PLAIN_ROOT}" ] && [ -n "${ROOT}" ]
55                 then
56                         # Do a local boot from hd
57                         livefs_root=${ROOT}
58                 else
59                         if [ -x /usr/bin/memdiskfind ]
60                         then
61                                 MEMDISK=$(/usr/bin/memdiskfind)
62
63                                 if [ $? -eq 0 ]
64                                 then
65                                         # We found a memdisk, set up phram
66                                         modprobe phram phram=memdisk,${MEMDISK}
67
68                                         # Load mtdblock, the memdisk will be /dev/mtdblock0
69                                         modprobe mtdblock
70                                 fi
71                         fi
72
73                         # Scan local devices for the image
74                         i=0
75                         while [ "$i" -lt 60 ]
76                         do
77                                 livefs_root=$(find_livefs ${i})
78
79                                 if [ -n "${livefs_root}" ]
80                                 then
81                                         break
82                                 fi
83
84                                 sleep 1
85                                 i="$(($i + 1))"
86                         done
87                 fi
88         fi
89
90         if [ -z "${livefs_root}" ]
91         then
92                 panic "Unable to find a medium containing a live file system"
93         fi
94
95         case "${LIVE_VERIFY_CHECKSUMS}" in
96                 true)
97                         Verify_checksums "${livefs_root}"
98                         ;;
99         esac
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 /live/fromiso
124                 then
125                         umount /live/fromiso
126                         rmdir --ignore-fail-on-non-empty /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
141         if [ -n "${ROOT_PID}" ]
142         then
143                 echo "${ROOT_PID}" > "${rootmnt}"/live/root.pid
144         fi
145
146         log_end_msg
147
148         # unionfs-fuse needs /dev to be bind-mounted for the duration of
149         # live-bottom; udev's init script will take care of things after that
150         case "${UNIONTYPE}" in
151                 unionfs-fuse)
152                         mount -n -o bind /dev "${rootmnt}/dev"
153                         ;;
154         esac
155
156         # Move to the new root filesystem so that programs there can get at it.
157         if [ ! -d /root/live/image ]
158         then
159                 mkdir -p /root/live/image
160                 mount --move /live/image /root/live/image
161         fi
162
163         # aufs2 in kernel versions around 2.6.33 has a regression:
164         # directories can't be accessed when read for the first the time,
165         # causing a failure for example when accessing /var/lib/fai
166         # when booting FAI, this simple workaround solves it
167         ls /root/* >/dev/null 2>&1
168
169         # Move findiso directory to the new root filesystem so that programs there can get at it.
170         if [ -d /live/findiso ] && [ ! -d /root/live/findiso ]
171         then
172                 mkdir -p /root/live/findiso
173                 mount -n --move /live/findiso /root/live/findiso
174         fi
175
176         # if we do not unmount the ISO we can't run "fsck /dev/ice" later on
177         # because the mountpoint is left behind in /proc/mounts, so let's get
178         # rid of it when running from RAM
179         if [ -n "$FINDISO" ] && [ "${TORAM}" ]
180         then
181                 losetup -d /dev/loop0
182
183                 if is_mountpoint /root/live/findiso
184                 then
185                         umount /root/live/findiso
186                         rmdir --ignore-fail-on-non-empty /root/live/findiso \
187                                 >/dev/null 2>&1 || true
188                 fi
189         fi
190
191         if [ -f /etc/resolv.conf ] && [ ! -s ${rootmnt}/etc/resolv.conf ]
192         then
193                 log_begin_msg "Copying /etc/resolv.conf to ${rootmnt}/etc/resolv.conf"
194                 cp -v /etc/resolv.conf ${rootmnt}/etc/resolv.conf
195                 log_end_msg
196         fi
197
198         if ! [ -d "/lib/live/boot" ]
199         then
200                 panic "A wrong rootfs was mounted."
201         fi
202
203         Fstab
204         Netbase
205
206         case "${LIVE_SWAPON}" in
207                 true)
208                         Swapon
209                         ;;
210         esac
211
212         case "${UNIONFS}" in
213                 unionfs-fuse)
214                         umount "${rootmnt}/dev"
215                         ;;
216         esac
217
218         exec 1>&6 6>&-
219         exec 2>&7 7>&-
220         kill ${tailpid}
221         [ -w "${rootmnt}/var/log/" ] && mkdir -p "${rootmnt}/var/log/live" && cp boot.log "${rootmnt}/var/log/live" 2>/dev/null
222 }