Release new version 1:20210208+grml.5
[live-boot-grml.git] / components / 9990-misc-helpers.sh
1 #!/bin/sh
2
3 #set -e
4
5 is_live_path()
6 {
7         DIRECTORY="${1}/${LIVE_MEDIA_PATH}"
8         for FILESYSTEM in squashfs ext2 ext3 ext4 xfs dir jffs
9         do
10                 if ls "${DIRECTORY}/"*.${FILESYSTEM} > /dev/null 2>&1
11                 then
12                         return 0
13                 fi
14         done
15         return 1
16 }
17
18 grml_match_bootid()
19 {
20         path="$1"
21
22         if [ -n "$IGNORE_BOOTID" ] ; then
23                 echo " * Ignoring verification of bootid.txt as requested via ignore_bootid.">>/boot.log
24                 return 0
25         fi
26
27         if [ -n "$BOOTID" ] && ! [ -r "${path}/conf/bootid.txt" ] ; then
28                 echo "  * Warning: bootid=... specified but no bootid.txt found on currently requested device.">>/boot.log
29                 return 1
30         fi
31
32         [ -r "${path}/conf/bootid.txt" ] || return 0
33
34         bootid_conf=$(cat "${path}/conf/bootid.txt")
35
36         if [ -z "$BOOTID" -a -z "$IGNORE_BOOTID" ]
37         then
38                 echo " * Warning: bootid.txt found but ignore_bootid / bootid=.. bootoption missing...">>/boot.log
39                 return 1
40         fi
41
42         if [ "$BOOTID" = "$bootid_conf" ]
43         then
44                 echo " * Successfully verified /conf/bootid.txt from ISO, continuing... ">>/boot.log
45         else
46                 echo " * Warning: BOOTID of ISO does not match. Retrying and continuing search...">>/boot.log
47                 return 1
48         fi
49
50         return 0
51 }
52
53
54 matches_uuid ()
55 {
56         if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
57         then
58                 return 0
59         fi
60
61         path="${1}"
62         uuid="$(cat /conf/uuid.conf)"
63
64         for try_uuid_file in "${path}/.disk/live-uuid"*
65         do
66                 [ -e "${try_uuid_file}" ] || continue
67
68                 try_uuid="$(cat "${try_uuid_file}")"
69
70                 if [ "${uuid}" = "${try_uuid}" ]
71                 then
72                         return 0
73                 fi
74         done
75
76         return 1
77 }
78
79 get_backing_device ()
80 {
81         case "${1}" in
82                 *.squashfs|*.ext2|*.ext3|*.ext4|*.jffs2)
83                         echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}" "${2}")
84                         ;;
85
86                 *.dir)
87                         echo "directory"
88                         ;;
89
90                 *)
91                         panic "Unrecognized live filesystem: ${1}"
92                         ;;
93         esac
94 }
95
96 mount_images_in_directory ()
97 {
98         directory="${1}"
99         rootmnt="${2}"
100         mac="${3}"
101
102         if is_live_path "${directory}"
103         then
104                 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
105                 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
106         else
107                 panic "No supported filesystem images found at /${LIVE_MEDIA_PATH}."
108         fi
109 }
110
111 is_nice_device ()
112 {
113         sysfs_path="${1#/sys}"
114
115         if udevadm info --query=all --path="${sysfs_path}" | egrep -q "DEVTYPE=disk"
116         then
117                 return 0
118         elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
119         then
120                 return 0
121         elif echo ${sysfs_path} | grep -q "^/block/dm-"
122         then
123                 return 0
124         elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
125         then
126                 return 0
127         fi
128
129         return 1
130 }
131
132 check_dev ()
133 {
134         local force fix
135         sysdev="${1}"
136         devname="${2}"
137         skip_uuid_check="${3}"
138
139         # support for fromiso=.../isofrom=....
140         local iso_device=''
141         if [ -n "$FROMISO" ]
142         then
143                 fs_type="${FROMISO%%:*}"
144                 fs_type_auto='1'
145                 iso_device="${FROMISO}"
146                 if echo "${fs_type}" | grep -q '[^[:alnum:]_-]'; then
147                         # Not a valid file system name. Treat as part of the
148                         # path, and, especially, use autodetection.
149                         fs_type=''
150                 else
151                         # Looks like a file system specification, treat it
152                         # like that.
153                         fs_type_auto='0'
154                         iso_device="${iso_device#*:}"
155                 fi
156                 iso_device=$(dirname "${iso_device}")
157                 if ! [ -b "${iso_device}" ]
158                 then
159                         # to support unusual device names like /dev/cciss/c0d0p1
160                         # as well we have to identify the block device name, let's
161                         # do that for up to 15 levels
162                         i='15'
163                         while [ -n "${iso_device}" ] && [ "${i}" -gt '0' ]
164                         do
165                                 iso_device=$(dirname "${iso_device}")
166                                 if [ -b "${iso_device}" ]; then
167                                         # Proper device, we're done.
168                                         break
169                                 else
170                                         # Otherwise, it *could* be a proper device
171                                         # that just hasn't been started yet - think
172                                         # lvm or RAID.
173                                         case "${iso_device}" in
174                                                 '/dev/mapper/'*|'/dev/md/'*)
175                                                         # We're looking for paths like
176                                                         # /dev/{mapper,md}/foo here.
177                                                         if printf '%s\n' "${iso_device}" | \
178                                                            grep -Eqs '^/dev/(mapper|md)/[^/]+$'; then
179                                                                 # Okay, looks like a device we'll
180                                                                 # have to start later on, done here.
181                                                                 break
182                                                         fi
183                                                         ;;
184                                                 '/dev/md'*)
185                                                         # This is different from the path above.
186                                                         # Here, we're looking for something like
187                                                         # /dev/md0, /dev/md126 and the like.
188                                                         if printf '%s\n' "${iso_device}" | \
189                                                            grep -Eqs '^/dev/md[[:digit:]]+$'; then
190                                                                 # Syntax matches, keep as device
191                                                                 # name for later when RAIDs were
192                                                                 # started.
193                                                                 break
194                                                         fi
195                                                         ;;
196                                         esac
197                                 fi
198                                 i="$(($i - 1))"
199                         done
200                 fi
201
202                 if [ "${iso_device}" = '/' ]
203                 then
204                         # not a block device, check if it's an iso file, for
205                         # example an ISO when booting on an ONIE system
206                         if echo "${FROMISO}" | grep -q "\.iso$"
207                         then
208                                 if [ '0' -ne "${fs_type_auto}" ]; then
209                                         # Autodetect fs type if not overridden.
210                                         fs_type=$(get_fstype "${FROMISO}")
211                                 fi
212                                 if is_supported_fs ${fs_type}
213                                 then
214                                         mkdir /run/live/fromiso
215                                         mount -t "${fs_type}" -o 'ro' "${FROMISO}" '/run/live/fromiso'
216                                         if [ "$?" != 0 ]
217                                         then
218                                                 echo "Warning: unable to mount ${FROMISO} (type ${fs_type})." >>/boot.log
219                                         fi
220                                         devname="/run/live/fromiso"
221                                 fi
222                         else
223                                 echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
224                         fi
225                 else
226                         # Otherwise, it must be a block device, so record that as the devname.
227                         devname="${iso_device}"
228                 fi
229         else
230                 # Usual handling in the non-fromiso case.
231                 if [ -z "${devname}" ]
232                 then
233                         devname=$(sys2dev "${sysdev}")
234                 fi
235         fi
236
237         # Common code path again: if devname is a directory, we can just go ahead
238         # and bind-mount it to our target mount point.
239         if [ -d "${devname}" ]
240         then
241                 mount -o bind "${devname}" $mountpoint || continue
242
243                 if is_live_path $mountpoint
244                 then
245                         echo $mountpoint
246                         return 0
247                 else
248                         umount $mountpoint
249                 fi
250         fi
251
252         # Check if we have to start logical volumes or RAID arrays.
253         # Do this in the common code path so it's only executed once.
254         IFS=","
255         local device=''
256         for device in ${devname}
257         do
258                 case "$device" in
259                         *mapper*)
260                                 # Adding lvm support
261                                 if [ -x /scripts/local-top/lvm2 ]
262                                 then
263                                         ROOT="$device" resume="" /scripts/local-top/lvm2 >>/boot.log
264                                 fi
265                                 ;;
266
267                         /dev/md*)
268                                 # Adding raid support
269                                 if [ -x /scripts/local-block/mdadm ]
270                                 then
271                                         # Before actually doing anything, make sure that no auto-generated
272                                         # mdadm.conf is around in the initramfs.
273                                         # Short explanation of why this is needed:
274                                         # /usr/share/initramfs-tools/hooks/mdadm is executed as part of
275                                         # mkinitramfs, which generates an /etc/mdadm/mdadm.conf file if one
276                                         # doesn't exist yet (which should always be the case in our new
277                                         # chroot) based on the host system configuration. Eventually, this
278                                         # is copied into the generated initramfs and then causes issues on
279                                         # other systems.
280                                         # It's safe to delete the file within the initramfs. The actual
281                                         # squashfs root will not be affected by that (and, additionally,
282                                         # a FAI script makes sure that the mdadm.conf in there gets wiped
283                                         # of any auto-generated arrays).
284                                         rm -rf '/etc/mdadm/mdadm.conf'
285
286                                         # Back in the day, when there was still a local-top mdadm script, we
287                                         # used to select specific devices to be auto-assembled.
288                                         # This functionality was dropped in the local-block script, so just
289                                         # scan and assemble all RAID devices.
290                                         /scripts/local-block/mdadm >>/boot.log
291                                 fi
292                                 ;;
293                 esac
294         done
295         unset IFS
296
297         # Back to fromiso handling, if necessary.
298         # We should now have the necessary lv/RAID devices available, so try to use them.
299         if [ -n "$FROMISO" ]
300         then
301                 if [ "${iso_device}" != '/' ]
302                 then
303                         # Need to extract actual ISO file path later on,
304                         # initialize first.
305                         iso_name="${FROMISO}"
306
307                         if [ '0' -ne "${fs_type_auto}" ]; then
308                                 # Try to auto-detect file system if not
309                                 # explicitly provided.
310                                 fs_type=$(get_fstype "${iso_device}")
311                         else
312                                 # Delete file system type override.
313                                 iso_name="${iso_name#*:}"
314                         fi
315                         # At this point, the backing device should always be
316                         # at the very front, so remove that - leaving only the
317                         # ISO file path.
318                         iso_name="$(echo "${iso_name}" | sed "s|^${iso_device}||")"
319                         if is_supported_fs ${fs_type}
320                         then
321                                 mkdir /run/live/fromiso
322                                 mount -t "${fs_type}" -o 'ro' "${iso_device}" '/run/live/fromiso'
323                                 loopdevname=$(setup_loop "/run/live/fromiso/${iso_name}" "loop" "/sys/block/loop*" "" '')
324                                 devname="${loopdevname}"
325
326                                 # Reset device variable, we won't need it.
327                                 device=''
328                         else
329                                 echo "Warning: unable to mount ${iso_device} (type ${fs_type})." >>/boot.log
330                         fi
331                 fi
332         fi
333
334         [ -n "$device" ] && devname="$device"
335
336         [ -e "$devname" ] || continue
337
338         if [ -n "${LIVE_MEDIA_OFFSET}" ]
339         then
340                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
341                 devname="${loopdevname}"
342         fi
343
344         fstype=$(get_fstype "${devname}")
345
346         if is_supported_fs ${fstype}
347         then
348                 devuid=$(blkid -o value -s UUID "$devname")
349                 [ -n "$devuid" ] && grep -qs "\<$devuid\>" /var/lib/live/boot/devices-already-tried-to-mount && continue
350
351                 for _PARAMETER in ${LIVE_BOOT_CMDLINE}
352                 do
353                         case "${_PARAMETER}" in
354                                 forcefsck)
355                                         FORCEFSCK="true"
356                                         ;;
357                         esac
358                 done
359
360                 if [ "${PERSISTENCE_FSCK}" = "true" ] ||  [ "${PERSISTENCE_FSCK}" = "yes" ] || [ "${FORCEFSCK}" = "true" ]
361                 then
362                         force=""
363                         if [ "$FORCEFSCK" = "true" ]
364                         then
365                                 force="-f"
366                         fi
367
368                         fix="-a"
369                         if [ "$FSCKFIX" = "true" ] || [ "$FSCKFIX" = "yes" ]
370                         then
371                                 fix="-y"
372                         fi
373
374                         fsck $fix $force ${devname} >> fsck.log 2>&1
375                 fi
376
377                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
378                 [ -n "$devuid" ] && echo "$devuid" >> /var/lib/live/boot/devices-already-tried-to-mount
379
380                 if [ -n "${FINDISO}" ]
381                 then
382                         if [ -f ${mountpoint}/${FINDISO} ]
383                         then
384                                 umount ${mountpoint}
385                                 mkdir -p /run/live/findiso
386                                 mount -t ${fstype} -o ro,noatime "${devname}" /run/live/findiso
387                                 loopdevname=$(setup_loop "/run/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
388                                 devname="${loopdevname}"
389                                 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
390                         else
391                                 umount ${mountpoint}
392                         fi
393                 fi
394
395                 if is_live_path ${mountpoint} && \
396                         ([ "${skip_uuid_check}" ] || grml_match_bootid ${mountpoint})
397                 then
398                         echo ${mountpoint}
399                         return 0
400                 else
401                         umount ${mountpoint} 2>/dev/null
402                 fi
403         fi
404
405         if [ -n "${LIVE_MEDIA_OFFSET}" ]
406         then
407                 losetup -d "${loopdevname}"
408         fi
409
410         return 1
411 }
412
413 find_livefs ()
414 {
415         timeout="${1}"
416
417         # don't start autodetection before timeout has expired
418         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
419         then
420                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
421                 then
422                         return 1
423                 fi
424         fi
425
426         # first look at the one specified in the command line
427         case "${LIVE_MEDIA}" in
428                 removable-usb)
429                         for sysblock in $(removable_usb_dev "sys")
430                         do
431                                 for dev in $(subdevices "${sysblock}")
432                                 do
433                                         if check_dev "${dev}"
434                                         then
435                                                 return 0
436                                         fi
437                                 done
438                         done
439                         return 1
440                         ;;
441
442                 removable)
443                         for sysblock in $(removable_dev "sys")
444                         do
445                                 for dev in $(subdevices "${sysblock}")
446                                 do
447                                         if check_dev "${dev}"
448                                         then
449                                                 return 0
450                                         fi
451                                 done
452                         done
453                         return 1
454                         ;;
455
456                 *)
457                         if [ ! -z "${LIVE_MEDIA}" ]
458                         then
459                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
460                                 then
461                                         return 0
462                                 fi
463                         fi
464                         ;;
465         esac
466
467         # or do the scan of block devices
468         # prefer removable devices over non-removable devices, so scan them first
469         devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
470
471         for sysblock in $devices_to_scan
472         do
473                 devname=$(sys2dev "${sysblock}")
474                 [ -e "$devname" ] || continue
475                 fstype=$(get_fstype "${devname}")
476
477                 if /lib/udev/cdrom_id ${devname} > /dev/null
478                 then
479                         if check_dev "null" "${devname}"
480                         then
481                                 return 0
482                         fi
483                 elif is_nice_device "${sysblock}"
484                 then
485                         for dev in $(subdevices "${sysblock}")
486                         do
487                                 if check_dev "${dev}"
488                                 then
489                                         return 0
490                                 fi
491                         done
492                 fi
493         done
494
495         return 1
496 }
497
498 is_in_list_separator_helper ()
499 {
500         local sep element list
501         sep=${1}
502         shift
503         element=${1}
504         shift
505         list=${*}
506         echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
507 }
508
509 is_in_space_sep_list ()
510 {
511         local element
512         element=${1}
513         shift
514         is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
515 }
516
517 is_in_comma_sep_list ()
518 {
519         local element
520         element=${1}
521         shift
522         is_in_list_separator_helper "," "${element}" "${*}"
523 }
524
525 sys2dev ()
526 {
527         sysdev=${1#/sys}
528         echo "/dev/$(udevadm info -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
529 }
530
531 subdevices ()
532 {
533         sysblock=${1}
534         r=""
535
536         for dev in "${sysblock}"/* "${sysblock}"
537         do
538                 if [ -e "${dev}/dev" ]
539                 then
540                         r="${r} ${dev}"
541                 fi
542         done
543
544         echo ${r}
545 }
546
547 storage_devices()
548 {
549         black_listed_devices="${1}"
550         white_listed_devices="${2}"
551
552         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
553         do
554                 fulldevname=$(sys2dev "${sysblock}")
555
556                 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
557                         [ -n "${white_listed_devices}" ] && \
558                         ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
559                 then
560                         # skip this device entirely
561                         continue
562                 fi
563
564                 for dev in $(subdevices "${sysblock}")
565                 do
566                         devname=$(sys2dev "${dev}")
567
568                         if is_in_space_sep_list ${devname} ${black_listed_devices}
569                         then
570                                 # skip this subdevice
571                                 continue
572                         else
573                                 echo "${devname}"
574                         fi
575                 done
576         done
577 }
578
579 is_supported_fs ()
580 {
581         fstype="${1}"
582
583         # Validate input first
584         if [ -z "${fstype}" ]
585         then
586                 return 1
587         fi
588
589         # get_fstype might report "unknown" or "swap", ignore it as no such kernel module exists
590         if [ "${fstype}" = "unknown" ] || [ "${fstype}" = "swap" ]
591         then
592                 return 1
593         fi
594
595         # Try to look if it is already supported by the kernel
596         # For ntfs, since user space program ntfs-3g will be used. Check ntfs-3g instead of kernel module.
597         if [ "${fstype}" = "ntfs" ]; then
598                 if type ntfs-3g >/dev/null 2>&1; then
599                         return 0
600                 else
601                         return 1
602                 fi
603         fi
604         if grep -q ${fstype} /proc/filesystems
605         then
606                 return 0
607         else
608                 # Then try to add support for it the gentle way using the initramfs capabilities
609                 modprobe -q -b ${fstype}
610                 if grep -q ${fstype} /proc/filesystems
611                 then
612                         return 0
613                 # Then try the hard way if /root is already reachable
614                 else
615                         kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
616                         if [ -e "${kmodule}" ]
617                         then
618                                 insmod "${kmodule}"
619                                 if grep -q ${fstype} /proc/filesystems
620                                 then
621                                         return 0
622                                 fi
623                         fi
624                 fi
625         fi
626
627         return 1
628 }
629
630 get_fstype ()
631 {
632         blkid -s TYPE -o value $1 2>/dev/null
633 }
634
635 where_is_mounted ()
636 {
637         device=${1}
638         # return first found
639         grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
640 }
641
642 trim_path ()
643 {
644         # remove all unnecessary /:s in the path, including last one (except
645         # if path is just "/")
646         echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
647 }
648
649 what_is_mounted_on ()
650 {
651         local dir
652         dir="$(trim_path ${1})"
653         grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
654 }
655
656 chown_ref ()
657 {
658         local reference targets owner
659         reference="${1}"
660         shift
661         targets=${@}
662         owner=$(stat -c %u:%g "${reference}")
663         chown -h ${owner} ${targets}
664 }
665
666 chmod_ref ()
667 {
668         local reference targets rights
669         reference="${1}"
670         shift
671         targets=${@}
672         rights=$(stat -c %a "${reference}")
673         chmod ${rights} ${targets}
674 }
675
676 lastline ()
677 {
678         while read lines
679         do
680                 line=${lines}
681         done
682
683         echo "${line}"
684 }
685
686 base_path ()
687 {
688         testpath="${1}"
689         mounts="$(awk '{print $2}' /proc/mounts)"
690         testpath="$(realpath ${testpath})"
691
692         while true
693         do
694                 if echo "${mounts}" | grep -qs "^${testpath}"
695                 then
696                         set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
697                         echo ${1}
698                         break
699                 else
700                         testpath=$(dirname $testpath)
701                 fi
702         done
703 }
704
705 fs_size ()
706 {
707         # Returns used/free fs kbytes + 5% more
708         # You could pass a block device as ${1} or the mount point as ${2}
709
710         dev="${1}"
711         mountp="${2}"
712         used="${3}"
713
714         if [ -z "${mountp}" ]
715         then
716                 mountp="$(where_is_mounted ${dev})"
717
718                 if [ -z "${mountp}" ]
719                 then
720                         mountp="/mnt/tmp_fs_size"
721
722                         mkdir -p "${mountp}"
723                         mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
724
725                         doumount=1
726                 fi
727         fi
728
729         if [ "${used}" = "used" ]
730         then
731                 size=$(du -ks ${mountp} | cut -f1)
732                 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
733         else
734                 # free space
735                 size="$(df -kP | grep -s ${mountp} | awk '{print $4}')"
736         fi
737
738         if [ -n "${doumount}" ]
739         then
740                 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
741                 rmdir "${mountp}"
742         fi
743
744         echo "${size}"
745 }
746
747 load_keymap ()
748 {
749         # Load custom keymap
750         if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
751         then
752                 loadkeys --quiet /etc/boottime.kmap.gz
753         fi
754 }
755
756 setup_loop ()
757 {
758         local fspath module pattern offset encryption readonly
759         fspath=${1}
760         module=${2}
761         pattern=${3}
762         offset=${4}
763         encryption=${5}
764         readonly=${6}
765
766         # the output of setup_loop is evaluated in other functions,
767         # modprobe leaks kernel options like "libata.dma=0"
768         # as "options libata dma=0" on stdout, causing serious
769         # problems therefor, so instead always avoid output to stdout
770         modprobe -q -b "${module}" 1>/dev/null
771
772         udevadm settle
773
774         for loopdev in ${pattern}
775         do
776                 if [ "$(cat ${loopdev}/size)" -eq 0 ]
777                 then
778                         dev=$(sys2dev "${loopdev}")
779                         options=''
780
781                         if [ -n "${readonly}" ]
782                         then
783                                 if losetup --help 2>&1 | grep -q -- "-r\b"
784                                 then
785                                         options="${options} -r"
786                                 fi
787                         fi
788
789                         if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
790                         then
791                                 options="${options} -o ${offset}"
792                         fi
793
794                         if [ -z "${encryption}" ]
795                         then
796                                 losetup ${options} "${dev}" "${fspath}"
797                         else
798                                 # Loop AES encryption
799                                 while true
800                                 do
801                                         load_keymap
802
803                                         echo -n "Enter passphrase for root filesystem: " >&6
804                                         read -s passphrase
805                                         echo "${passphrase}" > /tmp/passphrase
806                                         unset passphrase
807                                         exec 9</tmp/passphrase
808                                         losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
809                                         error=${?}
810                                         exec 9<&-
811                                         rm -f /tmp/passphrase
812
813                                         if [ 0 -eq ${error} ]
814                                         then
815                                                 unset error
816                                                 break
817                                         fi
818
819                                         echo
820                                         echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
821                                         read answer
822
823                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
824                                         then
825                                                 unset answer
826                                                 break
827                                         fi
828                                 done
829                         fi
830
831                         echo "${dev}"
832                         return 0
833                 fi
834         done
835
836         panic "No loop devices available"
837 }
838
839 try_mount ()
840 {
841         dev="${1}"
842         mountp="${2}"
843         opts="${3}"
844         fstype="${4}"
845
846         old_mountp="$(where_is_mounted ${dev})"
847
848         if [ -n "${old_mountp}" ]
849         then
850                 if [ "${opts}" != "ro" ]
851                 then
852                         mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
853                 fi
854
855                 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
856         else
857                 if [ -z "${fstype}" ]
858                 then
859                         fstype=$(get_fstype "${dev}")
860                 fi
861                 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
862                 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" >> boot.log && return 0 )
863         fi
864 }
865
866 # Try to mount $device to the place expected by live-boot. If $device
867 # is already mounted somewhere, move it to the expected place. If $device
868 # ends with a "/" this is a directory path.
869 # If we're only probing $device (to check if it has custom persistence)
870 # $probe should be set, which suppresses warnings upon failure. On
871 # success, print the mount point for $device.
872 mount_persistence_media ()
873 {
874         local device probe backing old_backing fstype mount_opts
875         device=${1}
876         probe=${2}
877
878         # get_custom_mounts() might call this with a directory path instead
879         # of a block device path. This means we have found sub-directory path
880         # underneath /run/live/persistence, so we're done
881         if [ -d "${device}" ]
882         then
883                 echo "${device}"
884                 return 0
885         fi
886
887         if [ ! -b "${device}" ]
888         then
889                 return 1
890         fi
891
892         backing="/run/live/persistence/$(basename ${device})"
893
894         mkdir -p "${backing}"
895         old_backing="$(where_is_mounted ${device})"
896         if [ -z "${old_backing}" ]
897         then
898                 fstype="$(get_fstype ${device})"
899                 mount_opts="rw,noatime"
900                 if [ -n "${PERSISTENCE_READONLY}" ]
901                 then
902                         mount_opts="ro,noatime"
903                 fi
904                 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null 2>&1
905                 then
906                         echo ${backing}
907                         return 0
908                 else
909                         [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
910                         rmdir "${backing}"
911                         return 1
912                 fi
913         elif [ "${backing}" != "${old_backing}" ]
914         then
915                 if ! mount -o move ${old_backing} ${backing} >/dev/null
916                 then
917                         [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
918                         rmdir "${backing}"
919                         return 1
920                 fi
921                 mount_opts="rw,noatime"
922                 if [ -n "${PERSISTENCE_READONLY}" ]
923                 then
924                         mount_opts="ro,noatime"
925                 fi
926                 if ! mount -o "remount,${mount_opts}" "${backing}" >/dev/null
927                 then
928                         log_warning_msg "Failed to remount persistence media ${device} writable"
929                         # Don't unmount or rmdir the new mountpoint in this case
930                 fi
931                 echo ${backing}
932                 return 0
933         else
934                 # This means that $device has already been mounted on
935                 # the place expected by live-boot, so we're done.
936                 echo ${backing}
937                 return 0
938         fi
939 }
940
941 close_persistence_media ()
942 {
943         local device backing
944         device=${1}
945         backing="$(where_is_mounted ${device})"
946
947         if [ -d "${backing}" ]
948         then
949                 umount "${backing}" >/dev/null 2>&1
950                 rmdir "${backing}" >/dev/null 2>&1
951         fi
952
953         if is_active_luks_mapping ${device}
954         then
955                 cryptsetup luksClose ${device}
956         fi
957 }
958
959 open_luks_device ()
960 {
961         dev="${1}"
962         name="$(basename ${dev})"
963         opts="--key-file=-"
964         if [ -n "${PERSISTENCE_READONLY}" ]
965         then
966                 opts="${opts} --readonly"
967         fi
968
969         if cryptsetup status "${name}" >/dev/null 2>&1
970         then
971                 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
972                 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
973                 if [ "${opened_dev}" = "${dev}" ]
974                 then
975                         luks_device="/dev/mapper/${name}"
976                         echo ${luks_device}
977                         return 0
978                 else
979                         log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
980                         return 1
981                 fi
982         fi
983
984         load_keymap
985
986         # check for plymouth
987         if [ -x /bin/plymouth ]
988         then
989                 _PLYMOUTH="true"
990         fi
991
992         case "${_PLYMOUTH}" in
993                 true)
994                         plymouth --ping
995
996                         cryptkeyscript="plymouth ask-for-password --prompt"
997                         # Plymouth will add a : if it is a non-graphical prompt
998                         cryptkeyprompt="Please unlock disk ${dev}"
999                         ;;
1000
1001                 *)
1002                         cryptkeyscript="/lib/cryptsetup/askpass"
1003                         cryptkeyprompt="Please unlock disk ${dev}: "
1004                         ;;
1005         esac
1006
1007         while true
1008         do
1009                 $cryptkeyscript "$cryptkeyprompt" | \
1010                         cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
1011
1012                 if [ 0 -eq ${?} ]
1013                 then
1014                         luks_device="/dev/mapper/${name}"
1015                         echo ${luks_device}
1016                         return 0
1017                 fi
1018
1019                 echo >&6
1020                 retryprompt="There was an error decrypting ${dev} ... Retry? [Y/n]"
1021
1022                 case "${_PLYMOUTH}" in
1023                         true)
1024                                 plymouth display-message --text "${retryprompt}"
1025                                 answer=$(plymouth watch-keystroke --keys="YNyn")
1026                                 ;;
1027
1028                         *)
1029                                 echo -n "${retryprompt} " >&6
1030                                 read answer
1031                                 ;;
1032                 esac
1033
1034                 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
1035                 then
1036                         case "${_PLYMOUTH}" in
1037                                 true)
1038                                         plymouth display-message --text ""
1039                                         ;;
1040                         esac
1041
1042                         return 2
1043                 fi
1044         done
1045 }
1046
1047 get_gpt_name ()
1048 {
1049     local dev
1050     dev="${1}"
1051     blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
1052 }
1053
1054 is_gpt_device ()
1055 {
1056     local dev
1057     dev="${1}"
1058     [ "$(blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
1059 }
1060
1061 probe_for_gpt_name ()
1062 {
1063         local overlays dev gpt_dev gpt_name
1064         overlays="${1}"
1065         dev="${2}"
1066
1067         gpt_dev="${dev}"
1068         if is_active_luks_mapping ${dev}
1069         then
1070                 # if $dev is an opened luks device, we need to check
1071                 # GPT stuff on the backing device
1072                 gpt_dev=$(get_luks_backing_device "${dev}")
1073         fi
1074
1075         if ! is_gpt_device ${gpt_dev}
1076         then
1077                 return
1078         fi
1079
1080         gpt_name=$(get_gpt_name ${gpt_dev})
1081         for label in ${overlays}
1082         do
1083                 if [ "${gpt_name}" = "${label}" ]
1084                 then
1085                         echo "${label}=${dev}"
1086                 fi
1087         done
1088 }
1089
1090 probe_for_fs_label ()
1091 {
1092         local overlays dev
1093         overlays="${1}"
1094         dev="${2}"
1095
1096         for label in ${overlays}
1097         do
1098                 if [ "$(blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
1099                 then
1100                         echo "${label}=${dev}"
1101                 fi
1102         done
1103 }
1104
1105 probe_for_file_name ()
1106 {
1107         local overlays dev ret backing
1108         overlays="${1}"
1109         dev="${2}"
1110
1111         ret=""
1112         backing="$(mount_persistence_media ${dev} probe)"
1113         if [ -z "${backing}" ]
1114         then
1115             return
1116         fi
1117
1118         for label in ${overlays}
1119         do
1120                 path=${backing}/${PERSISTENCE_PATH}/${label}
1121                 if [ -f "${path}" ]
1122                 then
1123                         local loopdev
1124                         loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
1125                         ret="${ret} ${label}=${loopdev}"
1126                 fi
1127         done
1128
1129         if [ -n "${ret}" ]
1130         then
1131                 echo ${ret}
1132         else
1133                 # unmount and remove mountpoint
1134                 umount ${backing} > /dev/null 2>&1 || true
1135                 rmdir ${backing} > /dev/null 2>&1 || true
1136         fi
1137 }
1138
1139 probe_for_directory_name ()
1140 {
1141         local overlays dev ret backing
1142         overlays="${1}"
1143         dev="${2}"
1144
1145         ret=""
1146         backing="$(mount_persistence_media ${dev} probe)"
1147         if [ -z "${backing}" ]
1148         then
1149             return
1150         fi
1151
1152         for label in ${overlays}
1153         do
1154                 path=${backing}/${PERSISTENCE_PATH}/${label}
1155                 if [ -d "${path}" ]
1156                 then
1157                         # in this case the "device" ends with a "/"
1158                         ret="${ret} ${label}=${backing}/${PERSISTENCE_PATH}/${label%%/}/"
1159                 fi
1160         done
1161
1162         if [ -n "${ret}" ]
1163         then
1164                 echo ${ret}
1165         else
1166                 # unmount and remove mountpoint
1167                 umount ${backing} > /dev/null 2>&1 || true
1168                 rmdir ${backing} > /dev/null 2>&1 || true
1169         fi
1170 }
1171
1172 find_persistence_media ()
1173 {
1174         # Scans devices for overlays, and returns a whitespace
1175         # separated list of how to use them. Only overlays with a partition
1176         # label or file name in ${overlays} are returned.
1177         #
1178         # When scanning a LUKS device, the user will be asked to enter the
1179         # passphrase; on failure to enter it, or if no persistence partitions
1180         # or files were found, the LUKS device is closed.
1181         #
1182         # For all other cases (overlay partition and overlay file) the
1183         # return value is "${label}=${device}", where ${device} a device that
1184         # can mount the content. In the case of an overlay file, the device
1185         # containing the file will remain mounted as a side-effect.
1186         #
1187         # No devices in ${black_listed_devices} will be scanned, and if
1188         # ${white_list_devices} is non-empty, only devices in it will be
1189         # scanned.
1190
1191         local overlays white_listed_devices ret black_listed_devices
1192         overlays="${1}"
1193         white_listed_devices="${2}"
1194         ret=""
1195
1196         #
1197         # The devices that are hosting the actual live rootfs should not be
1198         # used for persistence storage since otherwise you might mount a
1199         # parent directory on top of a sub-directory of the same filesystem
1200         # in one union together.
1201         #
1202         black_listed_devices=""
1203         for d in /run/live/rootfs/* /run/live/findiso /run/live/fromiso
1204         do
1205                 black_listed_devices="${black_listed_devices} $(what_is_mounted_on d)"
1206         done
1207
1208         for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
1209         do
1210                 local result luks_device
1211                 result=""
1212
1213                 luks_device=""
1214                 # Check if it's a luks device; we'll have to open the device
1215                 # in order to probe any filesystem it contains, like we do
1216                 # below. activate_custom_mounts() also depends on that any luks
1217                 # device already has been opened.
1218                 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${dev}
1219                 then
1220                         if luks_device=$(open_luks_device "${dev}")
1221                         then
1222                                 dev="${luks_device}"
1223                         else
1224                                 # skip $dev since we failed/chose not to open it
1225                                 continue
1226                         fi
1227                 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
1228                 then
1229                         # skip $dev since we don't allow unencrypted storage
1230                         continue
1231                 fi
1232
1233                 # Probe for matching GPT partition names or filesystem labels
1234                 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
1235                 then
1236                         result=$(probe_for_gpt_name "${overlays}" ${dev})
1237                         if [ -n "${result}" ]
1238                         then
1239                                 ret="${ret} ${result}"
1240                                 continue
1241                         fi
1242
1243                         result=$(probe_for_fs_label "${overlays}" ${dev})
1244                         if [ -n "${result}" ]
1245                         then
1246                                 ret="${ret} ${result}"
1247                                 continue
1248                         fi
1249                 fi
1250
1251                 # Probe for files with matching name on mounted partition
1252                 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
1253                 then
1254                         result=$(probe_for_file_name "${overlays}" ${dev})
1255                         if [ -n "${result}" ]
1256                         then
1257                                 local loopdevice
1258                                 loopdevice=${result##*=}
1259                                 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && is_luks_partition ${loopdevice}
1260                                 then
1261                                         local luksfile
1262                                         luksfile=""
1263                                         if luksfile=$(open_luks_device "${loopdevice}")
1264                                         then
1265                                                 result=${result%%=*}
1266                                                 result="${result}=${luksfile}"
1267                                         else
1268                                                 losetup -d $loopdevice
1269                                                 result=""
1270                                         fi
1271                                 fi
1272                                 ret="${ret} ${result}"
1273                                 continue
1274                         fi
1275                 fi
1276
1277                 # Probe for directory with matching name on mounted partition
1278                 if is_in_comma_sep_list directory ${PERSISTENCE_STORAGE}
1279                 then
1280                         result=$(probe_for_directory_name "${overlays}" ${dev})
1281                         if [ -n "${result}" ]
1282                         then
1283                                 ret="${ret} ${result}"
1284                                 continue
1285                         fi
1286                 fi
1287
1288                 # Close luks device if it isn't used
1289                 if [ -z "${result}" ] && [ -n "${luks_device}" ] && is_active_luks_mapping "${luks_device}"
1290                 then
1291                         cryptsetup luksClose "${luks_device}"
1292                 fi
1293         done
1294
1295         if [ -n "${ret}" ]
1296         then
1297                 echo ${ret}
1298         fi
1299 }
1300
1301 get_mac ()
1302 {
1303         mac=""
1304
1305         for adaptor in /sys/class/net/*
1306         do
1307                 status="$(cat ${adaptor}/iflink)"
1308
1309                 if [ "${status}" -eq 2 ]
1310                 then
1311                         mac="$(cat ${adaptor}/address)"
1312                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1313                 fi
1314         done
1315
1316         echo ${mac}
1317 }
1318
1319 is_luks_partition ()
1320 {
1321         device="${1}"
1322         cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1323 }
1324
1325 is_active_luks_mapping ()
1326 {
1327         device="${1}"
1328         cryptsetup status "${device}" 1>/dev/null 2>&1
1329 }
1330
1331 get_luks_backing_device ()
1332 {
1333         device=${1}
1334         cryptsetup status ${device} 2> /dev/null | \
1335                 awk '{if ($1 == "device:") print $2}'
1336 }
1337
1338 removable_dev ()
1339 {
1340         output_format="${1}"
1341         want_usb="${2}"
1342         ret=
1343
1344         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1345         do
1346                 if [ ! -d "${sysblock}" ]; then
1347                         continue
1348                 fi
1349
1350                 dev_ok=
1351                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1352                 then
1353                         if [ -z "${want_usb}" ]
1354                         then
1355                                 dev_ok="true"
1356                         else
1357                                 if readlink ${sysblock} | grep -q usb
1358                                 then
1359                                         dev_ok="true"
1360                                 fi
1361                         fi
1362                 fi
1363
1364                 if [ "${dev_ok}" = "true" ]
1365                 then
1366                         case "${output_format}" in
1367                                 sys)
1368                                         ret="${ret} ${sysblock}"
1369                                         ;;
1370                                 *)
1371                                         devname=$(sys2dev "${sysblock}")
1372                                         ret="${ret} ${devname}"
1373                                         ;;
1374                         esac
1375                 fi
1376         done
1377
1378         echo "${ret}"
1379 }
1380
1381 removable_usb_dev ()
1382 {
1383         output_format="${1}"
1384
1385         removable_dev "${output_format}" "want_usb"
1386 }
1387
1388 non_removable_dev ()
1389 {
1390         output_format="${1}"
1391         ret=
1392
1393         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1394         do
1395                 if [ ! -d "${sysblock}" ]; then
1396                         continue
1397                 fi
1398
1399                 if [ "$(cat ${sysblock}/removable)" = "0" ]
1400                 then
1401                         case "${output_format}" in
1402                                 sys)
1403                                         ret="${ret} ${sysblock}"
1404                                         ;;
1405                                 *)
1406                                         devname=$(sys2dev "${sysblock}")
1407                                         ret="${ret} ${devname}"
1408                                         ;;
1409                         esac
1410                 fi
1411         done
1412
1413         echo "${ret}"
1414 }
1415
1416 link_files ()
1417 {
1418         # create source's directory structure in dest, and recursively
1419         # create symlinks in dest to to all files in source. if mask
1420         # is non-empty, remove mask from all source paths when
1421         # creating links (will be necessary if we change root, which
1422         # live-boot normally does (into $rootmnt)).
1423         local src_dir dest_dir src_transform
1424
1425         # remove multiple /:s and ensure ending on /
1426         src_dir="$(trim_path ${1})/"
1427         dest_dir="$(trim_path ${2})/"
1428         src_transform="${3}"
1429
1430         # This check can only trigger on the inital, non-recursive call since
1431         # we create the destination before recursive calls
1432         if [ ! -d "${dest_dir}" ]
1433         then
1434                 log_warning_msg "Must link_files into a directory"
1435                 return
1436         fi
1437
1438         find "${src_dir}" -mindepth 1 -maxdepth 1 | \
1439         while read src
1440         do
1441                 local dest final_src
1442                 dest="${dest_dir}$(basename "${src}")"
1443                 if [ -d "${src}" ]
1444                 then
1445                         if [ -z "$(ls -A "${src}")" ]
1446                         then
1447                                 continue
1448                         fi
1449                         if [ ! -d "${dest}" ]
1450                         then
1451                                 mkdir -p "${dest}"
1452                                 chown_ref "${src}" "${dest}"
1453                                 chmod_ref "${src}" "${dest}"
1454                         fi
1455                         link_files "${src}" "${dest}" "${src_transform}"
1456                 else
1457                         final_src=${src}
1458                         if [ -n "${src_transform}" ]
1459                         then
1460                                 final_src="$(echo ${final_src} | sed "${src_transform}")"
1461                         fi
1462                         rm -rf "${dest}" 2> /dev/null
1463                         ln -s "${final_src}" "${dest}"
1464                         chown_ref "${src}" "${dest}"
1465                 fi
1466         done
1467 }
1468
1469 do_union ()
1470 {
1471         local unionmountpoint unionrw unionro
1472         unionmountpoint="${1}"  # directory where the union is mounted
1473         shift
1474         unionrw="${1}"          # branch where the union changes are stored
1475         shift
1476         unionro="${*}"          # space separated list of read-only branches (optional)
1477
1478         case "${UNIONTYPE}" in
1479                 aufs)
1480                         rw_opt="rw"
1481                         ro_opt="rr+wh"
1482                         noxino_opt="noxino"
1483
1484                         unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1485                         if [ -n "${unionro}" ]
1486                         then
1487                                 for rofs in ${unionro}
1488                                 do
1489                                         unionmountopts="${unionmountopts}:${rofs}=${ro_opt}"
1490                                 done
1491                         fi
1492                         ;;
1493
1494                 overlay)
1495                         # XXX: can unionro be optional? i.e. can overlay skip lowerdir?
1496                         if [ -z "${unionro}" ]
1497                         then
1498                                 panic "overlay needs at least one lower filesystem (read-only branch)."
1499                         fi
1500                         # Multiple lower layers can now be given using the the colon (":") as a
1501                         # separator character between the directory names.
1502                         unionro="$(echo ${unionro} | sed -e 's| |:|g')"
1503                         # overlayfs requires:
1504                         # + a workdir to become mounted
1505                         # + workdir and upperdir to reside under the same mount
1506                         # + workdir and upperdir to be in separate directories
1507                         mkdir -p "${unionrw}/rw"
1508                         mkdir -p "${unionrw}/work"
1509                         unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}/rw,workdir=${unionrw}/work"
1510                         ;;
1511         esac
1512
1513         mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1514 }
1515
1516 get_custom_mounts ()
1517 {
1518         # Side-effect: leaves $devices with persistence.conf mounted in /run/live/persistence
1519         # Side-effect: prints info to file $custom_mounts
1520
1521         local custom_mounts devices bindings links
1522         custom_mounts=${1}
1523         shift
1524         devices=${@}
1525
1526         bindings="/tmp/bindings.list"
1527         links="/tmp/links.list"
1528         rm -rf ${bindings} ${links} 2> /dev/null
1529
1530         for device in ${devices}
1531         do
1532                 local device_name backing include_list
1533                 device_name="$(basename ${device})"
1534                 backing=$(mount_persistence_media ${device})
1535                 if [ -z "${backing}" ]
1536                 then
1537                         continue
1538                 fi
1539
1540                 if [ -r "${backing}/${persistence_list}" ]
1541                 then
1542                         include_list="${backing}/${persistence_list}"
1543                 else
1544                         continue
1545                 fi
1546
1547                 if [ -n "${LIVE_BOOT_DEBUG}" ] && [ -e "${include_list}" ]
1548                 then
1549                         cp ${include_list} /run/live/persistence/${persistence_list}.${device_name}
1550                 fi
1551
1552                 while read dir options # < ${include_list}
1553                 do
1554                         if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1555                         then
1556                                 # skipping empty or commented lines
1557                                 continue
1558                         fi
1559
1560                         if trim_path ${dir} | grep -q -e "^[^/]" -e "^/lib" -e "^/run/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1561                         then
1562                                 log_warning_msg "Skipping unsafe custom mount ${dir}: must be an absolute path containing neither the \".\" nor \"..\" special dirs, and cannot be \"/lib\", or \"/run/live\" or any of its sub-directories."
1563                                 continue
1564                         fi
1565
1566                         local opt_source opt_link source full_source full_dest
1567                         opt_source=""
1568                         opt_link=""
1569                         for opt in $(echo ${options} | tr ',' ' ');
1570                         do
1571                                 case "${opt}" in
1572                                         source=*)
1573                                                 opt_source=${opt#source=}
1574                                                 ;;
1575                                         link)
1576                                                 opt_link="true"
1577                                                 ;;
1578                                         union|bind)
1579                                                 ;;
1580                                         *)
1581                                                 log_warning_msg "Skipping custom mount with unknown option: ${opt}"
1582                                                 continue 2
1583                                                 ;;
1584                                 esac
1585                         done
1586
1587                         source="${dir}"
1588                         if [ -n "${opt_source}" ]
1589                         then
1590                                 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${opt_source}" != "." ]
1591                                 then
1592                                         log_warning_msg "Skipping unsafe custom mount with option source=${opt_source}: must be either \".\" (the media root) or a relative path w.r.t. the media root that contains neither comas, nor the special \".\" and \"..\" path components"
1593                                         continue
1594                                 else
1595                                         source="${opt_source}"
1596                                 fi
1597                         fi
1598
1599                         full_source="$(trim_path ${backing}/${source})"
1600                         full_dest="$(trim_path ${rootmnt}/${dir})"
1601                         if [ -n "${opt_link}" ]
1602                         then
1603                                 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1604                         else
1605                                 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1606                         fi
1607                 done < ${include_list}
1608         done
1609
1610         # We sort the list according to destination so we're sure that
1611         # we won't hide a previous mount. We also ignore duplicate
1612         # destinations in a more or less arbitrary way.
1613         [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1614
1615         # After all mounts are considered we add symlinks so they
1616         # won't be hidden by some mount.
1617         [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1618
1619         # We need to make sure that no two custom mounts have the same sources
1620         # or are nested; if that is the case, too much weird stuff can happen.
1621         local prev_source prev_dest
1622         prev_source="impossible source" # first iteration must not match
1623         prev_dest=""
1624         # This sort will ensure that a source /a comes right before a source
1625         # /a/b so we only need to look at the previous source
1626         [ -e ${custom_mounts} ] && sort -k2 -b ${custom_mounts} |
1627         while read device source dest options
1628         do
1629                 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1630                 then
1631                         panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1632                 fi
1633                 prev_source=${source}
1634                 prev_dest=${dest}
1635         done
1636 }
1637
1638 activate_custom_mounts ()
1639 {
1640         local custom_mounts used_devices
1641         custom_mounts="${1}" # the ouput from get_custom_mounts()
1642         used_devices=""
1643
1644         while read device source dest options # < ${custom_mounts}
1645         do
1646                 local opt_bind opt_link opt_union
1647                 opt_bind="true"
1648                 opt_link=""
1649                 opt_union=""
1650                 for opt in $(echo ${options} | tr ',' ' ');
1651                 do
1652                         case "${opt}" in
1653                                 bind)
1654                                         opt_bind="true"
1655                                         unset opt_link opt_union
1656                                         ;;
1657                                 link)
1658                                         opt_link="true"
1659                                         unset opt_bind opt_union
1660                                         ;;
1661                                 union)
1662                                         opt_union="true"
1663                                         unset opt_bind opt_link
1664                                         ;;
1665                         esac
1666                 done
1667
1668                 if [ -n "$(what_is_mounted_on "${dest}")" ]
1669                 then
1670                         if [ "${dest}" = "${rootmnt}" ]
1671                         then
1672                                 umount "${dest}"
1673                         else
1674                                 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1675                                 continue
1676                         fi
1677                 fi
1678
1679                 if [ ! -d "${dest}" ]
1680                 then
1681                         # create the destination and delete existing files in
1682                         # its path that are in the way
1683                         path="/"
1684                         for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1685                         do
1686                                 path=$(trim_path ${path}/${dir})
1687                                 if [ -f ${path} ]
1688                                 then
1689                                         rm -f ${path}
1690                                 fi
1691                                 if [ ! -e ${path} ]
1692                                 then
1693                                         mkdir -p ${path}
1694                                         if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1695                                         then
1696                                                 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1697                                                 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1698                                                 chown 1000:1000 ${path}
1699                                         fi
1700                                 fi
1701                         done
1702                 fi
1703
1704                 # if ${source} doesn't exist on our persistence media
1705                 # we bootstrap it with $dest from the live filesystem.
1706                 # this both makes sense and is critical if we're
1707                 # dealing with /etc or other system dir.
1708                 if [ ! -d "${source}" ]
1709                 then
1710                         if [ -n "${PERSISTENCE_READONLY}" ]
1711                         then
1712                                 continue
1713                         elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1714                         then
1715                                 # unions and don't need to be bootstrapped
1716                                 # link dirs can't be bootstrapped in a sensible way
1717                                 mkdir -p "${source}"
1718                                 chown_ref "${dest}" "${source}"
1719                                 chmod_ref "${dest}" "${source}"
1720                         elif [ -n "${opt_bind}" ]
1721                         then
1722                                 # ensure that $dest is not copied *into* $source
1723                                 mkdir -p "$(dirname ${source})"
1724                                 cp -a "${dest}" "${source}"
1725                         fi
1726                 fi
1727
1728                 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1729                 # ignore the loop below and set rootfs_dest_backing=$dest
1730                 local rootfs_dest_backing
1731                 rootfs_dest_backing=""
1732                 if [ -n "${opt_link}" ] || [ -n "${opt_union}" ]
1733                 then
1734                         for d in /run/live/rootfs/*
1735                         do
1736                                 if [ -n "${rootmnt}" ]
1737                                 then
1738                                         fs="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1739                                 else
1740                                         fs="${d}/${dest}"
1741                                 fi
1742                                 if [ -d "${fs}" ]
1743                                 then
1744                                         rootfs_dest_backing="${rootfs_dest_backing} ${fs}"
1745                                 fi
1746                         done
1747                 fi
1748
1749                 local cow_dir links_source
1750                 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1751                 then
1752                         link_files ${source} ${dest} ""
1753                 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1754                 then
1755                         mkdir -p /run/live/persistence
1756                         links_source=$(mktemp -d /run/live/persistence/links-source-XXXXXX)
1757                         chown_ref ${source} ${links_source}
1758                         chmod_ref ${source} ${links_source}
1759                         # We put the cow dir in the below strange place to
1760                         # make it absolutely certain that the link source
1761                         # has its own directory and isn't nested with some
1762                         # other custom mount (if so that mount's files would
1763                         # be linked, causing breakage.
1764                         cow_dir="/run/live/overlay/run/live/persistence/$(basename ${links_source})"
1765                         mkdir -p ${cow_dir}
1766                         chown_ref "${source}" "${cow_dir}"
1767                         chmod_ref "${source}" "${cow_dir}"
1768                         do_union ${links_source} ${cow_dir} ${source} ${rootfs_dest_backing}
1769                         link_files ${links_source} ${dest} "s|^${rootmnt}||"
1770                 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1771                 then
1772                         do_union ${dest} ${source} ${rootfs_dest_backing}
1773                 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1774                 then
1775                         mount -o bind "${source}" "${dest}"
1776                 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1777                 then
1778                         # bind-mount and union mount are handled the same
1779                         # in read-only mode, but note that rootfs_dest_backing
1780                         # is non-empty (and necessary) only for unions
1781                         cow_dir="/run/live/overlay/${dest}"
1782                         if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1783                         then
1784                                 # If an earlier custom mount has files here
1785                                 # it will "block" the current mount's files
1786                                 # which is undesirable
1787                                 rm -rf "${cow_dir}"
1788                         fi
1789                         mkdir -p ${cow_dir}
1790                         chown_ref "${source}" "${cow_dir}"
1791                         chmod_ref "${source}" "${cow_dir}"
1792                         if [ "${UNIONTYPE}" = "overlay" ]
1793                         then
1794                                 # When we use overlay we add the "/rw" postfix to our source when using it
1795                                 # as upper layer. Therefore we also have to add it here when using it as
1796                                 # the lower layer.
1797                                 source="${source}/rw"
1798                         fi
1799                         do_union ${dest} ${cow_dir} ${source} ${rootfs_dest_backing}
1800                 fi
1801
1802                 PERSISTENCE_IS_ON="1"
1803                 export PERSISTENCE_IS_ON
1804
1805                 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1806                 then
1807                         used_devices="${used_devices} ${device}"
1808                 fi
1809         done < ${custom_mounts}
1810
1811         echo ${used_devices}
1812 }
1813
1814 is_mountpoint ()
1815 {
1816         directory="$1"
1817
1818         [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]
1819 }