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