Dropping unionfs support, long superseeded with aufs.
[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                                 case "${UNIONTYPE}" in
118                                         unionmount)
119                                                 mpoint="${rootmnt}"
120                                                 rootfslist="${rootmnt} ${rootfslist}"
121                                                 ;;
122
123                                         *)
124                                                 mpoint="${croot}/${imagename}"
125                                                 rootfslist="${mpoint} ${rootfslist}"
126                                                 ;;
127                                 esac
128
129                                 mkdir -p "${mpoint}"
130                                 log_begin_msg "Mounting \"${image}\" on \"${mpoint}\" via \"${backdev}\""
131                                 mount -t "${fstype}" -o ro,noatime "${backdev}" "${mpoint}" || panic "Can not mount ${backdev} (${image}) on ${mpoint}"
132                                 log_end_msg
133                         fi
134                 done
135         else
136                 # we have a plain root system
137                 mkdir -p "${croot}/filesystem"
138                 log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
139                 mount -t $(get_fstype "${image_directory}") -o ro,noatime "${image_directory}" "${croot}/filesystem" || \
140                         panic "Can not mount ${image_directory} on ${croot}/filesystem" && \
141                         rootfslist="${croot}/filesystem ${rootfslist}"
142                 # probably broken:
143                 mount -o bind ${croot}/filesystem $mountpoint
144                 log_end_msg
145         fi
146
147         # tmpfs file systems
148         touch /etc/fstab
149         mkdir -p /live/overlay
150         mount -t tmpfs tmpfs /live/overlay
151
152         # Looking for persistence devices or files
153         if [ -n "${PERSISTENCE}" ] && [ -z "${NOPERSISTENCE}" ]
154         then
155
156                 if [ -z "${QUICKUSBMODULES}" ]
157                 then
158                         # Load USB modules
159                         num_block=$(ls -l /sys/block | wc -l)
160                         for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
161                         do
162                                 modprobe -q -b ${module}
163                         done
164
165                         udevadm trigger
166                         udevadm settle
167
168                         # For some reason, udevsettle does not block in this scenario,
169                         # so we sleep for a little while.
170                         #
171                         # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
172                         for timeout in 5 4 3 2 1
173                         do
174                                 sleep 1
175
176                                 if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
177                                 then
178                                         break
179                                 fi
180                         done
181                 fi
182
183                 local whitelistdev
184                 whitelistdev=""
185                 if [ -n "${PERSISTENCE_MEDIA}" ]
186                 then
187                         case "${PERSISTENCE_MEDIA}" in
188                                 removable)
189                                         whitelistdev="$(removable_dev)"
190                                         ;;
191
192                                 removable-usb)
193                                         whitelistdev="$(removable_usb_dev)"
194                                         ;;
195                         esac
196                         if [ -z "${whitelistdev}" ]
197                         then
198                                 whitelistdev="ignore_all_devices"
199                         fi
200                 fi
201
202                 if is_in_comma_sep_list overlay ${PERSISTENCE_METHOD}
203                 then
204                         overlays="${custom_overlay_label}"
205                 fi
206
207                 local overlay_devices
208                 overlay_devices=""
209                 if [ "${whitelistdev}" != "ignore_all_devices" ]
210                 then
211                         for media in $(find_persistence_media "${overlays}" "${whitelistdev}")
212                         do
213                                 media="$(echo ${media} | tr ":" " ")"
214
215                                 case ${media} in
216                                         ${custom_overlay_label}=*)
217                                                 device="${media#*=}"
218                                                 overlay_devices="${overlay_devices} ${device}"
219                                                 ;;
220                                  esac
221                         done
222                 fi
223         elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENCE}" ]
224         then
225                 # check if there are any nfs options
226                 if echo ${NFS_COW} | grep -q ','
227                 then
228                         nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
229                         nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
230                 else
231                         nfs_cow_opts="-o nolock"
232                         nfs_cow=${NFS_COW}
233                 fi
234
235                 if [ -n "${PERSISTENCE_READONLY}" ]
236                 then
237                         nfs_cow_opts="${nfs_cow_opts},nocto,ro"
238                 fi
239
240                 mac="$(get_mac)"
241                 if [ -n "${mac}" ]
242                 then
243                         cowdevice=$(echo ${nfs_cow} | sed "s/client_mac_address/${mac}/")
244                         cow_fstype="nfs"
245                 else
246                         panic "unable to determine mac address"
247                 fi
248         fi
249
250         if [ -z "${cowdevice}" ]
251         then
252                 cowdevice="tmpfs"
253                 cow_fstype="tmpfs"
254                 cow_mountopt="rw,noatime,mode=755"
255         fi
256
257         if [ "${UNIONTYPE}" != "unionmount" ]
258         then
259                 if [ -n "${PERSISTENCE_READONLY}" ] && [ "${cowdevice}" != "tmpfs" ]
260                 then
261                         mount -t tmpfs -o rw,noatime,mode=755 tmpfs "/live/overlay"
262                         root_backing="/live/persistence/$(basename ${cowdevice})-root"
263                         mkdir -p ${root_backing}
264                 else
265                         root_backing="/live/overlay"
266                 fi
267
268                 if [ "${cow_fstype}" = "nfs" ]
269                 then
270                         log_begin_msg \
271                                 "Trying nfsmount ${nfs_cow_opts} ${cowdevice} ${root_backing}"
272                         nfsmount ${nfs_cow_opts} ${cowdevice} ${root_backing} || \
273                                 panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on ${root_backing}"
274                 else
275                         mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} ${root_backing} || \
276                                 panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on ${root_backing}"
277                 fi
278         fi
279
280         rootfscount=$(echo ${rootfslist} |wc -w)
281
282         rootfs=${rootfslist%% }
283
284         if [ -n "${EXPOSED_ROOT}" ]
285         then
286                 if [ ${rootfscount} -ne 1 ]
287                 then
288                         panic "only one RO file system supported with exposedroot: ${rootfslist}"
289                 fi
290
291                 mount --bind ${rootfs} ${rootmnt} || \
292                         panic "bind mount of ${rootfs} failed"
293
294                 if [ -z "${SKIP_UNION_MOUNTS}" ]
295                 then
296                         cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
297                 else
298                         cow_dirs=''
299                 fi
300         else
301                 cow_dirs="/"
302         fi
303
304         if [ "${cow_fstype}" != "tmpfs" ] && [ "${cow_dirs}" != "/" ] && [ "${UNIONTYPE}" = "unionmount" ]
305         then
306                 true # FIXME: Maybe it does, I don't really know.
307                 #panic "unionmount does not support subunions (${cow_dirs})."
308         fi
309
310         for dir in ${cow_dirs}; do
311                 unionmountpoint="${rootmnt}${dir}"
312                 mkdir -p ${unionmountpoint}
313                 if [ "${UNIONTYPE}" = "unionmount" ]
314                 then
315                         # FIXME: handle PERSISTENCE_READONLY
316                         unionmountopts="-t ${cow_fstype} -o noatime,union,${cow_mountopt} ${cowdevice}"
317                         # unionmount only works with util-linux mount
318                         mount.util-linux $unionmountopts "${unionmountpoint}"
319                 else
320                         cow_dir="/live/overlay${dir}"
321                         rootfs_dir="${rootfs}${dir}"
322                         mkdir -p ${cow_dir}
323                         if [ -n "${PERSISTENCE_READONLY}" ] && [ "${cowdevice}" != "tmpfs" ]
324                         then
325                                 do_union ${unionmountpoint} ${cow_dir} ${root_backing} ${rootfs_dir}
326                         else
327                                 do_union ${unionmountpoint} ${cow_dir} ${rootfs_dir}
328                         fi
329                 fi || panic "mount ${UNIONTYPE} on ${unionmountpoint} failed with option ${unionmountopts}"
330         done
331
332         # Correct the permissions of /:
333         chmod 0755 "${rootmnt}"
334
335         # Correct the permission of /tmp:
336         if [ -d "${rootmnt}/tmp" ]
337         then
338                 chmod 1777 "${rootmnt}"/tmp
339         fi
340
341         live_rootfs_list=""
342         for d in ${rootfslist}
343         do
344                 live_rootfs="/live/rootfs/${d##*/}"
345                 live_rootfs_list="${live_rootfs_list} ${live_rootfs}"
346                 mkdir -p "${live_rootfs}"
347                 case "${d}" in
348                         *.dir)
349                                 # do nothing # mount -o bind "${d}" "${live_rootfs}"
350                                 ;;
351                         *)
352                                 mount -o move "${d}" "${live_rootfs}"
353                                 ;;
354                 esac
355         done
356
357         # Adding custom persistence
358         if [ -n "${PERSISTENCE}" ] && [ -z "${NOPERSISTENCE}" ]
359         then
360                 local custom_mounts
361                 custom_mounts="/tmp/custom_mounts.list"
362                 rm -f ${custom_mounts}
363
364                 # Gather information about custom mounts from devies detected as overlays
365                 get_custom_mounts ${custom_mounts} ${overlay_devices}
366
367                 [ -n "${LIVE_BOOT_DEBUG}" ] && cp ${custom_mounts} "/lib/live/mount/persistence"
368
369                 # Now we do the actual mounting (and symlinking)
370                 local used_overlays
371                 used_overlays=""
372                 used_overlays=$(activate_custom_mounts ${custom_mounts})
373                 rm -f ${custom_mounts}
374
375                 # Close unused overlays (e.g. due to missing $persistence_list)
376                 for overlay in ${overlay_devices}
377                 do
378                         if echo ${used_overlays} | grep -qve "^\(.* \)\?${overlay}\( .*\)\?$"
379                         then
380                                 close_persistence_media ${overlay}
381                         fi
382                 done
383         fi
384
385         # ensure that a potentially stray tmpfs gets removed
386         # otherways, initramfs-tools is unable to remove /live
387         # and fails to boot
388         umount /live/overlay > /dev/null 2>&1 || true
389 }