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