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