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