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