This patch enables support for verifying the ISO through bootid=.... bootoption.
[live-boot-grml.git] / components / 9990-overlay.sh
1 #!/bin/sh
2
3 #set -e
4
5 setup_unionfs ()
6 {
7         image_directory="${1}"
8         rootmnt="${2}"
9         addimage_directory="${3}"
10
11         modprobe -q -b ${UNIONTYPE}
12
13         if ! cut -f2 /proc/filesystems | grep -q "^${UNIONTYPE}\$"
14         then
15                 panic "${UNIONTYPE} not available."
16         fi
17
18         # run-init can't deal with images in a subdir, but we're going to
19         # move all of these away before it runs anyway.  No, we're not,
20         # put them in / since move-mounting them into / breaks mono and
21         # some other apps.
22
23         croot="/"
24
25         # Let's just mount the read-only file systems first
26         rootfslist=""
27
28         if [ -z "${PLAIN_ROOT}" ]
29         then
30                 # Read image names from ${MODULE}.module if it exists
31                 if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
32                 then
33                         for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
34                         do
35                                 image_string="${image_string} ${image_directory}/${IMAGE}"
36                         done
37                 elif [ -e "${image_directory}/${MODULE}.module" ]
38                 then
39                         for IMAGE in $(cat ${image_directory}/${MODULE}.module)
40                         do
41                                 image_string="${image_string} ${image_directory}/${IMAGE}"
42                         done
43                 else
44                         # ${MODULE}.module does not exist, create a list of images
45                         for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
46                         do
47                                 for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
48                                 do
49                                         if [ -e "${IMAGE}" ]
50                                         then
51                                                 image_string="${image_string} ${IMAGE}"
52                                         fi
53                                 done
54                         done
55
56                         if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
57                         then
58                                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
59                                 do
60                                         for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
61                                         do
62                                                 if [ -e "${IMAGE}" ]
63                                                 then
64                                                         image_string="${image_string} ${IMAGE}"
65                                                 fi
66                                         done
67                                 done
68                         fi
69
70                         # Now sort the list
71                         image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
72                 fi
73
74                 [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
75
76                 mkdir -p "${croot}"
77
78                 for image in ${image_string}
79                 do
80                         imagename=$(basename "${image}")
81
82                         export image devname
83                         maybe_break live-realpremount
84                         log_begin_msg "Running /scripts/live-realpremount"
85                         run_scripts /scripts/live-realpremount
86                         log_end_msg
87
88                         if [ -d "${image}" ]
89                         then
90                                 # it is a plain directory: do nothing
91                                 rootfslist="${image} ${rootfslist}"
92                         elif [ -f "${image}" ]
93                         then
94                                 if losetup --help 2>&1 | grep -q -- "-r\b"
95                                 then
96                                         backdev=$(get_backing_device "${image}" "-r")
97                                 else
98                                         backdev=$(get_backing_device "${image}")
99                                 fi
100                                 fstype=$(get_fstype "${backdev}")
101
102                                 case "${fstype}" in
103                                         unknown)
104                                                 panic "Unknown file system type on ${backdev} (${image})"
105                                                 ;;
106
107                                         "")
108                                                 fstype="${imagename##*.}"
109                                                 log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
110                                                 ;;
111                                 esac
112
113                                 mpoint="${croot}/${imagename}"
114                                 rootfslist="${mpoint} ${rootfslist}"
115
116                                 mkdir -p "${mpoint}"
117                                 log_begin_msg "Mounting \"${image}\" on \"${mpoint}\" via \"${backdev}\""
118                                 mount -t "${fstype}" -o ro,noatime "${backdev}" "${mpoint}" || panic "Can not mount ${backdev} (${image}) on ${mpoint}"
119                                 log_end_msg
120                         else
121                                 log_warning_msg "Could not find image '${image}'. Most likely it is listed in a .module file, perhaps by mistake."
122                         fi
123                 done
124         else
125                 # we have a plain root system
126                 mkdir -p "${croot}/filesystem"
127                 log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
128                 mount -t $(get_fstype "${image_directory}") -o ro,noatime "${image_directory}" "${croot}/filesystem" || \
129                         panic "Can not mount ${image_directory} on ${croot}/filesystem" && \
130                         rootfslist="${croot}/filesystem ${rootfslist}"
131                 # probably broken:
132                 mount -o bind ${croot}/filesystem $mountpoint
133                 log_end_msg
134         fi
135
136         # tmpfs file systems
137         touch /etc/fstab
138         mkdir -p /live/overlay
139         mount -t tmpfs tmpfs /live/overlay
140
141         # Looking for persistence devices or files
142         if [ -n "${PERSISTENCE}" ] && [ -z "${NOPERSISTENCE}" ]
143         then
144
145                 if [ -z "${QUICKUSBMODULES}" ]
146                 then
147                         # Load USB modules
148                         num_block=$(ls -l /sys/block | wc -l)
149                         for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
150                         do
151                                 modprobe -q -b ${module}
152                         done
153
154                         udevadm trigger
155                         udevadm settle
156
157                         # For some reason, udevsettle does not block in this scenario,
158                         # so we sleep for a little while.
159                         #
160                         # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
161                         for timeout in 5 4 3 2 1
162                         do
163                                 sleep 1
164
165                                 if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
166                                 then
167                                         break
168                                 fi
169                         done
170                 fi
171
172                 local whitelistdev
173                 whitelistdev=""
174                 if [ -n "${PERSISTENCE_MEDIA}" ]
175                 then
176                         case "${PERSISTENCE_MEDIA}" in
177                                 removable)
178                                         whitelistdev="$(removable_dev)"
179                                         ;;
180
181                                 removable-usb)
182                                         whitelistdev="$(removable_usb_dev)"
183                                         ;;
184                         esac
185                         if [ -z "${whitelistdev}" ]
186                         then
187                                 whitelistdev="ignore_all_devices"
188                         fi
189                 fi
190
191                 if is_in_comma_sep_list overlay ${PERSISTENCE_METHOD}
192                 then
193                         overlays="${custom_overlay_label}"
194                 fi
195
196                 local overlay_devices
197                 overlay_devices=""
198                 if [ "${whitelistdev}" != "ignore_all_devices" ]
199                 then
200                         for media in $(find_persistence_media "${overlays}" "${whitelistdev}")
201                         do
202                                 media="$(echo ${media} | tr ":" " ")"
203
204                                 case ${media} in
205                                         ${custom_overlay_label}=*)
206                                                 device="${media#*=}"
207                                                 overlay_devices="${overlay_devices} ${device}"
208                                                 ;;
209                                  esac
210                         done
211                 fi
212         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENCE}" ]
213         then
214                 # check if there are any nfs options
215                 if echo ${NFS_COW} | grep -q ','
216                 then
217                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
218                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
219                 else
220                         nfs_cow_opts="-o nolock"
221                         nfs_cow=${NFS_COW}
222                 fi
223
224                 if [ -n "${PERSISTENCE_READONLY}" ]
225                 then
226                         nfs_cow_opts="${nfs_cow_opts},nocto,ro"
227                 fi
228
229                 mac="$(get_mac)"
230                 if [ -n "${mac}" ]
231                 then
232                         cowdevice=$(echo ${nfs_cow} | sed "s/client_mac_address/${mac}/")
233                         cow_fstype="nfs"
234                 else
235                         panic "unable to determine mac address"
236                 fi
237         fi
238
239         if [ -z "${cowdevice}" ]
240         then
241                 cowdevice="tmpfs"
242                 cow_fstype="tmpfs"
243                 cow_mountopt="rw,noatime,mode=755"
244         fi
245
246         if [ -n "${PERSISTENCE_READONLY}" ] && [ "${cowdevice}" != "tmpfs" ]
247         then
248                 mount -t tmpfs -o rw,noatime,mode=755 tmpfs "/live/overlay"
249                 root_backing="/live/persistence/$(basename ${cowdevice})-root"
250                 mkdir -p ${root_backing}
251         else
252                 root_backing="/live/overlay"
253         fi
254
255         if [ "${cow_fstype}" = "nfs" ]
256         then
257                 log_begin_msg \
258                         "Trying nfsmount ${nfs_cow_opts} ${cowdevice} ${root_backing}"
259                 nfsmount ${nfs_cow_opts} ${cowdevice} ${root_backing} || \
260                         panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on ${root_backing}"
261         else
262                 mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} ${root_backing} || \
263                         panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on ${root_backing}"
264         fi
265
266         rootfscount=$(echo ${rootfslist} |wc -w)
267
268         rootfs=${rootfslist%% }
269
270         if [ -n "${EXPOSED_ROOT}" ]
271         then
272                 if [ ${rootfscount} -ne 1 ]
273                 then
274                         panic "only one RO file system supported with exposedroot: ${rootfslist}"
275                 fi
276
277                 mount --bind ${rootfs} ${rootmnt} || \
278                         panic "bind mount of ${rootfs} failed"
279
280                 if [ -z "${SKIP_UNION_MOUNTS}" ]
281                 then
282                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
283                 else
284                         cow_dirs=''
285                 fi
286         else
287                 cow_dirs="/"
288         fi
289
290         for dir in ${cow_dirs}; do
291                 unionmountpoint="${rootmnt}${dir}"
292                 mkdir -p ${unionmountpoint}
293                 cow_dir="/live/overlay${dir}"
294                 rootfs_dir="${rootfs}${dir}"
295                 mkdir -p ${cow_dir}
296                 if [ -n "${PERSISTENCE_READONLY}" ] && [ "${cowdevice}" != "tmpfs" ]
297                 then
298                         do_union ${unionmountpoint} ${cow_dir} ${root_backing} ${rootfs_dir}
299                 else
300                         do_union ${unionmountpoint} ${cow_dir} ${rootfs_dir}
301                 fi || panic "mount ${UNIONTYPE} on ${unionmountpoint} failed with option ${unionmountopts}"
302         done
303
304         # Remove persistence depending on boot parameter
305         Remove_persistence
306
307         # Correct the permissions of /:
308         chmod 0755 "${rootmnt}"
309
310         # Correct the permission of /tmp:
311         if [ -d "${rootmnt}/tmp" ]
312         then
313                 chmod 1777 "${rootmnt}"/tmp
314         fi
315
316         live_rootfs_list=""
317         for d in ${rootfslist}
318         do
319                 live_rootfs="/live/rootfs/${d##*/}"
320                 live_rootfs_list="${live_rootfs_list} ${live_rootfs}"
321                 mkdir -p "${live_rootfs}"
322                 case "${d}" in
323                         *.dir)
324                                 # do nothing # mount -o bind "${d}" "${live_rootfs}"
325                                 ;;
326                         *)
327                                 mount -o move "${d}" "${live_rootfs}"
328                                 ;;
329                 esac
330         done
331
332         # Adding custom persistence
333         if [ -n "${PERSISTENCE}" ] && [ -z "${NOPERSISTENCE}" ]
334         then
335                 local custom_mounts
336                 custom_mounts="/tmp/custom_mounts.list"
337                 rm -f ${custom_mounts}
338
339                 # Gather information about custom mounts from devies detected as overlays
340                 get_custom_mounts ${custom_mounts} ${overlay_devices}
341
342                 [ -n "${LIVE_BOOT_DEBUG}" ] && cp ${custom_mounts} "/lib/live/mount/persistence"
343
344                 # Now we do the actual mounting (and symlinking)
345                 local used_overlays
346                 used_overlays=""
347                 used_overlays=$(activate_custom_mounts ${custom_mounts})
348                 rm -f ${custom_mounts}
349
350                 # Close unused overlays (e.g. due to missing $persistence_list)
351                 for overlay in ${overlay_devices}
352                 do
353                         if echo ${used_overlays} | grep -qve "^\(.* \)\?${overlay}\( .*\)\?$"
354                         then
355                                 close_persistence_media ${overlay}
356                         fi
357                 done
358         fi
359
360         # ensure that a potentially stray tmpfs gets removed
361         # otherways, initramfs-tools is unable to remove /live
362         # and fails to boot
363         umount /live/overlay > /dev/null 2>&1 || true
364 }