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