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