897032752a86eb5d6163cc215c9d73c529825f87
[live-boot-grml.git] / scripts / boot / misc-helpers.sh
1 #!/bin/sh
2
3 #set -e
4
5 is_live_path ()
6 {
7         DIRECTORY="${1}"
8
9         if [ -d "${DIRECTORY}"/"${LIVE_MEDIA_PATH}" ]
10         then
11                 for FILESYSTEM in squashfs ext2 ext3 ext4 xfs dir jffs2
12                 do
13                         if [ "$(echo ${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM})" != "${DIRECTORY}/${LIVE_MEDIA_PATH}/*.${FILESYSTEM}" ]
14                         then
15                                 return 0
16                         fi
17                 done
18         fi
19
20         return 1
21 }
22
23 matches_uuid ()
24 {
25         if [ "${IGNORE_UUID}" ] || [ ! -e /conf/uuid.conf ]
26         then
27                 return 0
28         fi
29
30         path="${1}"
31         uuid="$(cat /conf/uuid.conf)"
32
33         for try_uuid_file in "${path}/.disk/live-uuid"*
34         do
35                 [ -e "${try_uuid_file}" ] || continue
36
37                 try_uuid="$(cat "${try_uuid_file}")"
38
39                 if [ "${uuid}" = "${try_uuid}" ]
40                 then
41                         return 0
42                 fi
43         done
44
45         return 1
46 }
47
48 get_backing_device ()
49 {
50         case "${1}" in
51                 *.squashfs|*.ext2|*.ext3|*.ext4|*.jffs2)
52                         echo $(setup_loop "${1}" "loop" "/sys/block/loop*" '0' "${LIVE_MEDIA_ENCRYPTION}" "${2}")
53                         ;;
54
55                 *.dir)
56                         echo "directory"
57                         ;;
58
59                 *)
60                         panic "Unrecognized live filesystem: ${1}"
61                         ;;
62         esac
63 }
64
65 match_files_in_dir ()
66 {
67         # Does any files match pattern ${1} ?
68         local pattern="${1}"
69
70         if [ "$(echo ${pattern})" != "${pattern}" ]
71         then
72                 return 0
73         fi
74
75         return 1
76 }
77
78 mount_images_in_directory ()
79 {
80         directory="${1}"
81         rootmnt="${2}"
82         mac="${3}"
83
84
85         if match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.squashfs" ||
86                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext2" ||
87                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext3" ||
88                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.ext4" ||
89                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.jffs2" ||
90                 match_files_in_dir "${directory}/${LIVE_MEDIA_PATH}/*.dir"
91         then
92                 [ -n "${mac}" ] && adddirectory="${directory}/${LIVE_MEDIA_PATH}/${mac}"
93                 setup_unionfs "${directory}/${LIVE_MEDIA_PATH}" "${rootmnt}" "${adddirectory}"
94         else
95                 panic "No supported filesystem images found at /${LIVE_MEDIA_PATH}."
96         fi
97 }
98
99 is_nice_device ()
100 {
101         sysfs_path="${1#/sys}"
102
103         if [ -e /lib/udev/path_id ]
104         then
105                 # squeeze
106                 PATH_ID="/lib/udev/path_id"
107         else
108                 # wheezy/sid (udev >= 174)
109                 PATH_ID="/sbin/udevadm test-builtin path_id"
110         fi
111
112         if ${PATH_ID} "${sysfs_path}" | egrep -q "ID_PATH=(usb|pci-[^-]*-(ide|sas|scsi|usb|virtio)|platform-sata_mv|platform-orion-ehci|platform-mmc|platform-mxsdhci)"
113         then
114                 return 0
115         elif echo "${sysfs_path}" | grep -q '^/block/vd[a-z]$'
116         then
117                 return 0
118         elif echo ${sysfs_path} | grep -q "^/block/dm-"
119         then
120                 return 0
121         elif echo ${sysfs_path} | grep -q "^/block/mtdblock"
122         then
123                 return 0
124         fi
125
126         return 1
127 }
128
129 check_dev ()
130 {
131         sysdev="${1}"
132         devname="${2}"
133         skip_uuid_check="${3}"
134
135         # support for fromiso=.../isofrom=....
136         if [ -n "$FROMISO" ]
137         then
138                 ISO_DEVICE=$(dirname $FROMISO)
139                 if ! [ -b $ISO_DEVICE ]
140                 then
141                         # to support unusual device names like /dev/cciss/c0d0p1
142                         # as well we have to identify the block device name, let's
143                         # do that for up to 15 levels
144                         i=15
145                         while [ -n "$ISO_DEVICE" ] && [ "$i" -gt 0 ]
146                         do
147                                 ISO_DEVICE=$(dirname ${ISO_DEVICE})
148                                 [ -b "$ISO_DEVICE" ] && break
149                                 i=$(($i -1))
150                         done
151                 fi
152
153                 if [ "$ISO_DEVICE" = "/" ]
154                 then
155                         echo "Warning: device for bootoption fromiso= ($FROMISO) not found.">>/boot.log
156                 else
157                         fs_type=$(get_fstype "${ISO_DEVICE}")
158                         if is_supported_fs ${fs_type}
159                         then
160                                 mkdir /live/fromiso
161                                 mount -t $fs_type "$ISO_DEVICE" /live/fromiso
162                                 ISO_NAME="$(echo $FROMISO | sed "s|$ISO_DEVICE||")"
163                                 loopdevname=$(setup_loop "/live/fromiso/${ISO_NAME}" "loop" "/sys/block/loop*" "" '')
164                                 devname="${loopdevname}"
165                         else
166                                 echo "Warning: unable to mount $ISO_DEVICE." >>/boot.log
167                         fi
168                 fi
169         fi
170
171         if [ -z "${devname}" ]
172         then
173                 devname=$(sys2dev "${sysdev}")
174         fi
175
176         if [ -d "${devname}" ]
177         then
178                 mount -o bind "${devname}" $mountpoint || continue
179
180                 if is_live_path $mountpoint
181                 then
182                         echo $mountpoint
183                         return 0
184                 else
185                         umount $mountpoint
186                 fi
187         fi
188
189         IFS=","
190         for device in ${devname}
191         do
192                 case "$device" in
193                         *mapper*)
194                                 # Adding lvm support
195                                 if [ -x /scripts/local-top/lvm2 ]
196                                 then
197                                         ROOT="$device" resume="" /scripts/local-top/lvm2
198                                 fi
199                                 ;;
200
201                         /dev/md*)
202                                 # Adding raid support
203                                 if [ -x /scripts/local-top/mdadm ]
204                                 then
205                                         cp /conf/conf.d/md /conf/conf.d/md.orig
206                                         echo "MD_DEVS=$device " >> /conf/conf.d/md
207                                         /scripts/local-top/mdadm
208                                         mv /conf/conf.d/md.orig /conf/conf.d/md
209                                 fi
210                                 ;;
211                 esac
212         done
213         unset IFS
214
215         [ -n "$device" ] && devname="$device"
216
217         [ -e "$devname" ] || continue
218
219         if [ -n "${LIVE_MEDIA_OFFSET}" ]
220         then
221                 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVE_MEDIA_OFFSET}" '')
222                 devname="${loopdevname}"
223         fi
224
225         fstype=$(get_fstype "${devname}")
226
227         if is_supported_fs ${fstype}
228         then
229                 devuid=$(blkid -o value -s UUID "$devname")
230                 [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
231                 mount -t ${fstype} -o ro,noatime "${devname}" ${mountpoint} || continue
232                 [ -n "$devuid" ] && echo "$devuid" >> $tried
233
234                 if [ -n "${FINDISO}" ]
235                 then
236                         if [ -f ${mountpoint}/${FINDISO} ]
237                         then
238                                 umount ${mountpoint}
239                                 mkdir -p /live/findiso
240                                 mount -t ${fstype} -o ro,noatime "${devname}" /live/findiso
241                                 loopdevname=$(setup_loop "/live/findiso/${FINDISO}" "loop" "/sys/block/loop*" 0 "")
242                                 devname="${loopdevname}"
243                                 mount -t iso9660 -o ro,noatime "${devname}" ${mountpoint}
244                         else
245                                 umount ${mountpoint}
246                         fi
247                 fi
248
249                 if is_live_path ${mountpoint} && \
250                         ([ "${skip_uuid_check}" ] || matches_uuid ${mountpoint})
251                 then
252                         echo ${mountpoint}
253                         return 0
254                 else
255                         umount ${mountpoint} 2>/dev/null
256                 fi
257         fi
258
259         if [ -n "${LIVE_MEDIA_OFFSET}" ]
260         then
261                 losetup -d "${loopdevname}"
262         fi
263
264         return 1
265 }
266
267 find_livefs ()
268 {
269         timeout="${1}"
270
271         # don't start autodetection before timeout has expired
272         if [ -n "${LIVE_MEDIA_TIMEOUT}" ]
273         then
274                 if [ "${timeout}" -lt "${LIVE_MEDIA_TIMEOUT}" ]
275                 then
276                         return 1
277                 fi
278         fi
279
280         # first look at the one specified in the command line
281         case "${LIVE_MEDIA}" in
282                 removable-usb)
283                         for sysblock in $(removable_usb_dev "sys")
284                         do
285                                 for dev in $(subdevices "${sysblock}")
286                                 do
287                                         if check_dev "${dev}"
288                                         then
289                                                 return 0
290                                         fi
291                                 done
292                         done
293                         return 1
294                         ;;
295
296                 removable)
297                         for sysblock in $(removable_dev "sys")
298                         do
299                                 for dev in $(subdevices "${sysblock}")
300                                 do
301                                         if check_dev "${dev}"
302                                         then
303                                                 return 0
304                                         fi
305                                 done
306                         done
307                         return 1
308                         ;;
309
310                 *)
311                         if [ ! -z "${LIVE_MEDIA}" ]
312                         then
313                                 if check_dev "null" "${LIVE_MEDIA}" "skip_uuid_check"
314                                 then
315                                         return 0
316                                 fi
317                         fi
318                         ;;
319         esac
320
321         # or do the scan of block devices
322         # prefer removable devices over non-removable devices, so scan them first
323         devices_to_scan="$(removable_dev 'sys') $(non_removable_dev 'sys')"
324
325         for sysblock in $devices_to_scan
326         do
327                 devname=$(sys2dev "${sysblock}")
328                 [ -e "$devname" ] || continue
329                 fstype=$(get_fstype "${devname}")
330
331                 if /lib/udev/cdrom_id ${devname} > /dev/null
332                 then
333                         if check_dev "null" "${devname}"
334                         then
335                                 return 0
336                         fi
337                 elif is_nice_device "${sysblock}"
338                 then
339                         for dev in $(subdevices "${sysblock}")
340                         do
341                                 if check_dev "${dev}"
342                                 then
343                                         return 0
344                                 fi
345                         done
346                 elif [ "${fstype}" = "squashfs" -o \
347                         "${fstype}" = "btrfs" -o \
348                         "${fstype}" = "ext2" -o \
349                         "${fstype}" = "ext3" -o \
350                         "${fstype}" = "ext4" -o \
351                         "${fstype}" = "jffs2" ]
352                 then
353                         # This is an ugly hack situation, the block device has
354                         # an image directly on it.  It's hopefully
355                         # live-boot, so take it and run with it.
356                         ln -s "${devname}" "${devname}.${fstype}"
357                         echo "${devname}.${fstype}"
358                         return 0
359                 fi
360         done
361
362         return 1
363 }
364
365 really_export ()
366 {
367         STRING="${1}"
368         VALUE="$(eval echo -n \${$STRING})"
369
370         if [ -f /live.vars ] && grep -sq "export ${STRING}" /live.vars
371         then
372                 sed -i -e 's/\('${STRING}'=\).*$/\1'${VALUE}'/' /live.vars
373         else
374                 echo "export ${STRING}=\"${VALUE}\"" >> /live.vars
375         fi
376
377         eval export "${STRING}"="${VALUE}"
378 }
379
380 is_in_list_separator_helper () {
381         local sep=${1}
382         shift
383         local element=${1}
384         shift
385         local list=${*}
386         echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
387 }
388
389 is_in_space_sep_list () {
390         local element=${1}
391         shift
392         is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
393 }
394
395 is_in_comma_sep_list () {
396         local element=${1}
397         shift
398         is_in_list_separator_helper "," "${element}" "${*}"
399 }
400
401 sys2dev ()
402 {
403         sysdev=${1#/sys}
404         echo "/dev/$($udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
405 }
406
407 subdevices ()
408 {
409         sysblock=${1}
410         r=""
411
412         for dev in "${sysblock}"/* "${sysblock}"
413         do
414                 if [ -e "${dev}/dev" ]
415                 then
416                         r="${r} ${dev}"
417                 fi
418         done
419
420         echo ${r}
421 }
422
423 storage_devices()
424 {
425         black_listed_devices="${1}"
426         white_listed_devices="${2}"
427
428         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
429         do
430                 fulldevname=$(sys2dev "${sysblock}")
431
432                 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
433                         [ -n "${white_listed_devices}" ] && \
434                         ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
435                 then
436                         # skip this device entirely
437                         continue
438                 fi
439
440                 for dev in $(subdevices "${sysblock}")
441                 do
442                         devname=$(sys2dev "${dev}")
443
444                         if is_in_space_sep_list ${devname} ${black_listed_devices}
445                         then
446                                 # skip this subdevice
447                                 continue
448                         else
449                                 echo "${devname}"
450                         fi
451                 done
452         done
453 }
454
455 is_supported_fs ()
456 {
457         fstype="${1}"
458
459         # Validate input first
460         if [ -z "${fstype}" ]
461         then
462                 return 1
463         fi
464
465         # Try to look if it is already supported by the kernel
466         if grep -q ${fstype} /proc/filesystems
467         then
468                 return 0
469         else
470                 # Then try to add support for it the gentle way using the initramfs capabilities
471                 modprobe ${fstype}
472                 if grep -q ${fstype} /proc/filesystems
473                 then
474                         return 0
475                 # Then try the hard way if /root is already reachable
476                 else
477                         kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
478                         if [ -e "${kmodule}" ]
479                         then
480                                 insmod "${kmodule}"
481                                 if grep -q ${fstype} /proc/filesystems
482                                 then
483                                         return 0
484                                 fi
485                         fi
486                 fi
487         fi
488
489         return 1
490 }
491
492 get_fstype ()
493 {
494         /sbin/blkid -s TYPE -o value $1 2>/dev/null
495 }
496
497 where_is_mounted ()
498 {
499         device=${1}
500         # return first found
501         grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
502 }
503
504 trim_path () {
505     # remove all unnecessary /:s in the path, including last one (except
506     # if path is just "/")
507     echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
508 }
509
510 what_is_mounted_on ()
511 {
512         local dir="$(trim_path ${1})"
513         grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
514 }
515
516 chown_ref ()
517 {
518         local reference="${1}"
519         shift
520         local targets=${@}
521         local owner=$(stat -c %u:%g "${reference}")
522         chown -h ${owner} ${targets}
523 }
524
525 chmod_ref ()
526 {
527         local reference="${1}"
528         shift
529         local targets=${@}
530         local rights=$(stat -c %a "${reference}")
531         chmod ${rights} ${targets}
532 }
533
534 lastline ()
535 {
536         while read lines
537         do
538                 line=${lines}
539         done
540
541         echo "${line}"
542 }
543
544 base_path ()
545 {
546         testpath="${1}"
547         mounts="$(awk '{print $2}' /proc/mounts)"
548         testpath="$(busybox realpath ${testpath})"
549
550         while true
551         do
552                 if echo "${mounts}" | grep -qs "^${testpath}"
553                 then
554                         set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
555                         echo ${1}
556                         break
557                 else
558                         testpath=$(dirname $testpath)
559                 fi
560         done
561 }
562
563 fs_size ()
564 {
565         # Returns used/free fs kbytes + 5% more
566         # You could pass a block device as ${1} or the mount point as ${2}
567
568         dev="${1}"
569         mountp="${2}"
570         used="${3}"
571
572         if [ -z "${mountp}" ]
573         then
574                 mountp="$(where_is_mounted ${dev})"
575
576                 if [ -z "${mountp}" ]
577                 then
578                         mountp="/mnt/tmp_fs_size"
579
580                         mkdir -p "${mountp}"
581                         mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
582
583                         doumount=1
584                 fi
585         fi
586
587         if [ "${used}" = "used" ]
588         then
589                 size=$(du -ks ${mountp} | cut -f1)
590                 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
591         else
592                 # free space
593                 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
594         fi
595
596         if [ -n "${doumount}" ]
597         then
598                 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
599                 rmdir "${mountp}"
600         fi
601
602         echo "${size}"
603 }
604
605 load_keymap ()
606 {
607         # Load custom keymap
608         if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
609         then
610                 loadkeys /etc/boottime.kmap.gz
611         fi
612 }
613
614 setup_loop ()
615 {
616         local fspath=${1}
617         local module=${2}
618         local pattern=${3}
619         local offset=${4}
620         local encryption=${5}
621         local readonly=${6}
622
623         # the output of setup_loop is evaluated in other functions,
624         # modprobe leaks kernel options like "libata.dma=0"
625         # as "options libata dma=0" on stdout, causing serious
626         # problems therefor, so instead always avoid output to stdout
627         modprobe -q -b "${module}" 1>/dev/null
628
629         udevadm settle
630
631         for loopdev in ${pattern}
632         do
633                 if [ "$(cat ${loopdev}/size)" -eq 0 ]
634                 then
635                         dev=$(sys2dev "${loopdev}")
636                         options=''
637
638                         if [ -n "${readonly}" ]
639                         then
640                                 if losetup --help 2>&1 | grep -q -- "-r\b"
641                                 then
642                                         options="${options} -r"
643                                 fi
644                         fi
645
646                         if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
647                         then
648                                 options="${options} -o ${offset}"
649                         fi
650
651                         if [ -z "${encryption}" ]
652                         then
653                                 losetup ${options} "${dev}" "${fspath}"
654                         else
655                                 # Loop AES encryption
656                                 while true
657                                 do
658                                         load_keymap
659
660                                         echo -n "Enter passphrase for root filesystem: " >&6
661                                         read -s passphrase
662                                         echo "${passphrase}" > /tmp/passphrase
663                                         unset passphrase
664                                         exec 9</tmp/passphrase
665                                         /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
666                                         error=${?}
667                                         exec 9<&-
668                                         rm -f /tmp/passphrase
669
670                                         if [ 0 -eq ${error} ]
671                                         then
672                                                 unset error
673                                                 break
674                                         fi
675
676                                         echo
677                                         echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
678                                         read answer
679
680                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
681                                         then
682                                                 unset answer
683                                                 break
684                                         fi
685                                 done
686                         fi
687
688                         echo "${dev}"
689                         return 0
690                 fi
691         done
692
693         panic "No loop devices available"
694 }
695
696 try_mount ()
697 {
698         dev="${1}"
699         mountp="${2}"
700         opts="${3}"
701         fstype="${4}"
702
703         old_mountp="$(where_is_mounted ${dev})"
704
705         if [ -n "${old_mountp}" ]
706         then
707                 if [ "${opts}" != "ro" ]
708                 then
709                         mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
710                 fi
711
712                 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
713         else
714                 if [ -z "${fstype}" ]
715                 then
716                         fstype=$(get_fstype "${dev}")
717                 fi
718                 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
719                 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
720         fi
721 }
722
723 mount_persistence_media ()
724 {
725         local device=${1}
726         local probe=${2}
727
728         local backing="/live/persistence/$(basename ${device})"
729
730         mkdir -p "${backing}"
731         local old_backing="$(where_is_mounted ${device})"
732         if [ -z "${old_backing}" ]
733         then
734                 local fstype="$(get_fstype ${device})"
735                 local mount_opts="rw,noatime"
736                 if [ -n "${PERSISTENCE_READONLY}" ]
737                 then
738                         mount_opts="ro,noatime"
739                 fi
740                 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
741                 then
742                         echo ${backing}
743                         return 0
744                 else
745                         [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
746                         rmdir "${backing}"
747                         return 1
748                 fi
749         elif [ "${backing}" != "${old_backing}" ]
750         then
751                 if mount --move ${old_backing} ${backing} >/dev/null
752                 then
753                         echo ${backing}
754                         return 0
755                 else
756                         [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
757                         rmdir "${backing}"
758                         return 1
759                 fi
760         fi
761         return 0
762 }
763
764 close_persistence_media () {
765         local device=${1}
766         local backing="$(where_is_mounted ${device})"
767
768         if [ -d "${backing}" ]
769         then
770                 umount "${backing}" >/dev/null 2>&1
771                 rmdir "${backing}" >/dev/null 2>&1
772         fi
773
774         if is_active_luks_mapping ${device}
775         then
776                 /sbin/cryptsetup luksClose ${device}
777         fi
778 }
779
780 open_luks_device ()
781 {
782         dev="${1}"
783         name="$(basename ${dev})"
784         opts="--key-file=-"
785         if [ -n "${PERSISTENCE_READONLY}" ]
786         then
787                 opts="${opts} --readonly"
788         fi
789
790         if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
791         then
792                 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
793                 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
794                 if [ "${opened_dev}" = "${dev}" ]
795                 then
796                         luks_device="/dev/mapper/${name}"
797                         echo ${luks_device}
798                         return 0
799                 else
800                         log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
801                         return 1
802                 fi
803         fi
804
805         load_keymap
806
807         while true
808         do
809                 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
810                         /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
811
812                 if [ 0 -eq ${?} ]
813                 then
814                         luks_device="/dev/mapper/${name}"
815                         echo ${luks_device}
816                         return 0
817                 fi
818
819                 echo >&6
820                 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
821                 read answer
822
823                 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
824                 then
825                         return 2
826                 fi
827         done
828 }
829
830 get_gpt_name ()
831 {
832     local dev="${1}"
833     /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
834 }
835
836 is_gpt_device ()
837 {
838     local dev="${1}"
839     [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
840 }
841
842 probe_for_gpt_name ()
843 {
844         local overlays="${1}"
845         local snapshots="${2}"
846         local dev="${3}"
847
848         local gpt_dev="${dev}"
849         if is_active_luks_mapping ${dev}
850         then
851                 # if $dev is an opened luks device, we need to check
852                 # GPT stuff on the backing device
853                 gpt_dev=$(get_luks_backing_device "${dev}")
854         fi
855
856         if ! is_gpt_device ${gpt_dev}
857         then
858                 return
859         fi
860
861         local gpt_name=$(get_gpt_name ${gpt_dev})
862         for label in ${overlays} ${snapshots}
863         do
864                 if [ "${gpt_name}" = "${label}" ]
865                 then
866                         echo "${label}=${dev}"
867                 fi
868         done
869 }
870
871 probe_for_fs_label ()
872 {
873         local overlays="${1}"
874         local snapshots="${2}"
875         local dev="${3}"
876
877         for label in ${overlays} ${snapshots}
878         do
879                 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
880                 then
881                         echo "${label}=${dev}"
882                 fi
883         done
884 }
885
886 probe_for_file_name ()
887 {
888         local overlays="${1}"
889         local snapshots="${2}"
890         local dev="${3}"
891
892         local ret=""
893         local backing="$(mount_persistence_media ${dev} probe)"
894         if [ -z "${backing}" ]
895         then
896             return
897         fi
898
899         for label in ${overlays}
900         do
901                 path=${backing}/${PERSISTENCE_PATH}${label}
902                 if [ -f "${path}" ]
903                 then
904                         local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
905                         ret="${ret} ${label}=${loopdev}"
906                 fi
907         done
908         for label in ${snapshots}
909         do
910                 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
911                 do
912                         path="${PERSISTENCE_PATH}${label}.${ext}"
913                         if [ -f "${backing}/${path}" ]
914                         then
915                                 ret="${ret} ${label}=${dev}:${backing}:${path}"
916                         fi
917                 done
918         done
919
920         if [ -n "${ret}" ]
921         then
922                 echo ${ret}
923         else
924                 umount ${backing} > /dev/null 2>&1 || true
925         fi
926 }
927
928 find_persistence_media ()
929 {
930         # Scans devices for overlays and snapshots, and returns a whitespace
931         # separated list of how to use them. Only overlays with a partition
932         # label or file name in ${overlays} are returned, and ditto for
933         # snapshots with labels in ${snapshots}.
934         #
935         # When scanning a LUKS device, the user will be asked to enter the
936         # passphrase; on failure to enter it, or if no persistence partitions
937         # or files were found, the LUKS device is closed.
938         #
939         # For a snapshot file the return value is ${label}=${snapdata}", where
940         # ${snapdata} is the parameter used for try_snap().
941         #
942         # For all other cases (overlay/snapshot partition and overlay file) the
943         # return value is "${label}=${device}", where ${device} a device that
944         # can mount the content. In the case of an overlay file, the device
945         # containing the file will remain mounted as a side-effect.
946         #
947         # No devices in ${black_listed_devices} will be scanned, and if
948         # ${white_list_devices} is non-empty, only devices in it will be
949         # scanned.
950
951         local overlays="${1}"
952         local snapshots="${2}"
953         local white_listed_devices="${3}"
954         local ret=""
955
956         local black_listed_devices="$(what_is_mounted_on /live/image)"
957
958         for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
959         do
960                 local result=""
961
962                 local luks_device=""
963                 # Check if it's a luks device; we'll have to open the device
964                 # in order to probe any filesystem it contains, like we do
965                 # below. activate_custom_mounts() also depends on that any luks
966                 # device already has been opened.
967                 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && \
968                    is_luks_partition ${dev}
969                 then
970                         if luks_device=$(open_luks_device "${dev}")
971                         then
972                                 dev="${luks_device}"
973                         else
974                                 # skip $dev since we failed/chose not to open it
975                                 continue
976                         fi
977                 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
978                 then
979                         # skip $dev since we don't allow unencrypted storage
980                         continue
981                 fi
982
983                 # Probe for matching GPT partition names or filesystem labels
984                 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
985                 then
986                         result=$(probe_for_gpt_name "${overlays}" "${snapshots}" ${dev})
987                         if [ -n "${result}" ]
988                         then
989                                 ret="${ret} ${result}"
990                                 continue
991                         fi
992
993                         result=$(probe_for_fs_label "${overlays}" "${snapshots}" ${dev})
994                         if [ -n "${result}" ]
995                         then
996                                 ret="${ret} ${result}"
997                                 continue
998                         fi
999                 fi
1000
1001                 # Probe for files with matching name on mounted partition
1002                 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
1003                 then
1004                         result=$(probe_for_file_name "${overlays}" "${snapshots}" ${dev})
1005                         if [ -n "${result}" ]
1006                         then
1007                                 ret="${ret} ${result}"
1008                                 continue
1009                         fi
1010                 fi
1011
1012                 # Close luks device if it isn't used
1013                 if [ -z "${result}" ] && [ -n "${luks_device}" ] && \
1014                    is_active_luks_mapping "${luks_device}"
1015                 then
1016                         /sbin/cryptsetup luksClose "${luks_device}"
1017                 fi
1018         done
1019
1020         if [ -n "${ret}" ]
1021         then
1022                 echo ${ret}
1023         fi
1024 }
1025
1026 get_mac ()
1027 {
1028         mac=""
1029
1030         for adaptor in /sys/class/net/*
1031         do
1032                 status="$(cat ${adaptor}/iflink)"
1033
1034                 if [ "${status}" -eq 2 ]
1035                 then
1036                         mac="$(cat ${adaptor}/address)"
1037                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1038                 fi
1039         done
1040
1041         echo ${mac}
1042 }
1043
1044 is_luks_partition ()
1045 {
1046         device="${1}"
1047         /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1048 }
1049
1050 is_active_luks_mapping ()
1051 {
1052         device="${1}"
1053         /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1054 }
1055
1056 get_luks_backing_device () {
1057         device=${1}
1058         cryptsetup status ${device} 2> /dev/null | \
1059                 awk '{if ($1 == "device:") print $2}'
1060 }
1061
1062 removable_dev ()
1063 {
1064         output_format="${1}"
1065         want_usb="${2}"
1066         ret=
1067
1068         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1069         do
1070                 dev_ok=
1071                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1072                 then
1073                         if [ -z "${want_usb}" ]
1074                         then
1075                                 dev_ok="true"
1076                         else
1077                                 if readlink ${sysblock} | grep -q usb
1078                                 then
1079                                         dev_ok="true"
1080                                 fi
1081                         fi
1082                 fi
1083
1084                 if [ "${dev_ok}" = "true" ]
1085                 then
1086                         case "${output_format}" in
1087                                 sys)
1088                                         ret="${ret} ${sysblock}"
1089                                         ;;
1090                                 *)
1091                                         devname=$(sys2dev "${sysblock}")
1092                                         ret="${ret} ${devname}"
1093                                         ;;
1094                         esac
1095                 fi
1096         done
1097
1098         echo "${ret}"
1099 }
1100
1101 removable_usb_dev ()
1102 {
1103         output_format="${1}"
1104
1105         removable_dev "${output_format}" "want_usb"
1106 }
1107
1108 non_removable_dev ()
1109 {
1110         output_format="${1}"
1111         ret=
1112
1113         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1114         do
1115                 if [ "$(cat ${sysblock}/removable)" = "0" ]
1116                 then
1117                         case "${output_format}" in
1118                                 sys)
1119                                         ret="${ret} ${sysblock}"
1120                                         ;;
1121                                 *)
1122                                         devname=$(sys2dev "${sysblock}")
1123                                         ret="${ret} ${devname}"
1124                                         ;;
1125                         esac
1126                 fi
1127         done
1128
1129         echo "${ret}"
1130 }
1131
1132 link_files ()
1133 {
1134         # create source's directory structure in dest, and recursively
1135         # create symlinks in dest to to all files in source. if mask
1136         # is non-empty, remove mask from all source paths when
1137         # creating links (will be necessary if we change root, which
1138         # live-boot normally does (into $rootmnt)).
1139
1140         # remove multiple /:s and ensure ending on /
1141         local src_dir="$(trim_path ${1})/"
1142         local dest_dir="$(trim_path ${2})/"
1143         local src_mask="${3}"
1144
1145         # This check can only trigger on the inital, non-recursive call since
1146         # we create the destination before recursive calls
1147         if [ ! -d "${dest_dir}" ]
1148         then
1149                 log_warning_msg "Must link_files into a directory"
1150                 return
1151         fi
1152
1153         find "${src_dir}" -mindepth 1 -maxdepth 1 | while read src; do
1154                 local dest="${dest_dir}$(basename "${src}")"
1155                 if [ -d "${src}" ]
1156                 then
1157                         if [ -z "$(ls -A "${src}")" ]
1158                         then
1159                                 continue
1160                         fi
1161                         if [ ! -d "${dest}" ]
1162                         then
1163                                 mkdir -p "${dest}"
1164                                 chown_ref "${src}" "${dest}"
1165                                 chmod_ref "${src}" "${dest}"
1166                         fi
1167                         link_files "${src}" "${dest}" "${src_mask}"
1168                 else
1169                         local final_src=${src}
1170                         if [ -n "${src_mask}" ]
1171                         then
1172                                 final_src="$(echo ${final_src} | sed "s|^${src_mask}||")"
1173                         fi
1174                         rm -rf "${dest}" 2> /dev/null
1175                         ln -s "${final_src}" "${dest}"
1176                         chown_ref "${src}" "${dest}"
1177                 fi
1178         done
1179 }
1180
1181 do_union ()
1182 {
1183         local unionmountpoint="${1}"    # directory where the union is mounted
1184         local unionrw="${2}"            # branch where the union changes are stored
1185         local unionro1="${3}"           # first underlying read-only branch (optional)
1186         local unionro2="${4}"           # second underlying read-only branch (optional)
1187
1188         if [ "${UNIONTYPE}" = "aufs" ]
1189         then
1190                 rw_opt="rw"
1191                 ro_opt="rr+wh"
1192                 noxino_opt="noxino"
1193         elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1194         then
1195                 rw_opt="RW"
1196                 ro_opt="RO"
1197         else
1198                 rw_opt="rw"
1199                 ro_opt="ro"
1200         fi
1201
1202         case "${UNIONTYPE}" in
1203                 unionfs-fuse)
1204                         unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1205                         unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1206                         if [ -n "${unionro1}" ]
1207                         then
1208                                 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1209                         fi
1210                         if [ -n "${unionro2}" ]
1211                         then
1212                                 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1213                         fi
1214                         ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1215                         unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1216                         ( mkdir -p /run/sendsigs.omit.d
1217                         pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1218                         ;;
1219
1220                 overlayfs)
1221                         # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1222                         # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1223                         unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1224                         mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1225                         ;;
1226
1227                 *)
1228                         unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1229                         if [ -n "${unionro1}" ]
1230                         then
1231                                 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1232                         fi
1233                         if [ -n "${unionro2}" ]
1234                         then
1235                                 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1236                         fi
1237                         mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1238                         ;;
1239         esac
1240 }
1241
1242 get_custom_mounts ()
1243 {
1244         # Side-effect: leaves $devices with live-persistence.conf mounted in /live/persistence
1245         # Side-effect: prints info to file $custom_mounts
1246
1247         local custom_mounts=${1}
1248         shift
1249         local devices=${@}
1250
1251         local bindings="/tmp/bindings.list"
1252         local links="/tmp/links.list"
1253         rm -rf ${bindings} ${links} 2> /dev/null
1254
1255         for device in ${devices}
1256         do
1257                 if [ ! -b "${device}" ]
1258                 then
1259                         continue
1260                 fi
1261
1262                 local device_name="$(basename ${device})"
1263                 local backing=$(mount_persistence_media ${device})
1264                 if [ -z "${backing}" ]
1265                 then
1266                         continue
1267                 fi
1268
1269                 local include_list="${backing}/${persistence_list}"
1270                 if [ ! -r "${include_list}" ]
1271                 then
1272                         continue
1273                 fi
1274
1275                 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
1276                 then
1277                         cp ${include_list} /live/persistence/${persistence_list}.${device_name}
1278                 fi
1279
1280                 while read dir options # < ${include_list}
1281                 do
1282                         if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1283                         then
1284                                 # skipping empty or commented lines
1285                                 continue
1286                         fi
1287
1288                         if trim_path ${dir} | grep -q -e "^[^/]" -e "^/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1289                         then
1290                                 log_warning_msg "Skipping unsafe custom mount ${dir}: must be an absolute path containing neither the \".\" nor \"..\" special dirs, and cannot be \"/live\" or any sub-directory therein."
1291                                 continue
1292                         fi
1293
1294                         local opt_source=""
1295                         local opt_link=""
1296                         for opt in $(echo ${options} | tr ',' ' ');
1297                         do
1298                                 case "${opt}" in
1299                                         source=*)
1300                                                 opt_source=${opt#source=}
1301                                                 ;;
1302                                         link)
1303                                                 opt_link="true"
1304                                                 ;;
1305                                         union|bind)
1306                                                 ;;
1307                                         *)
1308                                                 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1309                                                 continue 2
1310                                                 ;;
1311                                 esac
1312                         done
1313
1314                         local source="${dir}"
1315                         if [ -n "${opt_source}" ]
1316                         then
1317                                 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${source}" != "." ]
1318                                 then
1319                                         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"
1320                                         continue
1321                                 else
1322                                         source="${opt_source}"
1323                                 fi
1324                         fi
1325
1326                         local full_source="$(trim_path ${backing}/${source})"
1327                         local full_dest="$(trim_path ${rootmnt}/${dir})"
1328                         if [ -n "${opt_link}" ]
1329                         then
1330                                 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1331                         else
1332                                 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1333                         fi
1334                 done < ${include_list}
1335         done
1336
1337         # We sort the list according to destination so we're sure that
1338         # we won't hide a previous mount. We also ignore duplicate
1339         # destinations in a more or less arbitrary way.
1340         [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1341
1342         # After all mounts are considered we add symlinks so they
1343         # won't be hidden by some mount.
1344         [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1345
1346         # We need to make sure that no two custom mounts have the same sources
1347         # or are nested; if that is the case, too much weird stuff can happen.
1348         local prev_source="impossible source" # first iteration must not match
1349         local prev_dest=""
1350         # This sort will ensure that a source /a comes right before a source
1351         # /a/b so we only need to look at the previous source
1352         sort -k2 -b ${custom_mounts} |
1353         while read device source dest options
1354         do
1355                 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1356                 then
1357                         panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1358                 fi
1359                 prev_source=${source}
1360                 prev_dest=${dest}
1361         done
1362 }
1363
1364 activate_custom_mounts ()
1365 {
1366         local custom_mounts="${1}" # the ouput from get_custom_mounts()
1367         local used_devices=""
1368
1369         while read device source dest options # < ${custom_mounts}
1370         do
1371                 local opt_bind="true"
1372                 local opt_link=""
1373                 local opt_union=""
1374                 for opt in $(echo ${options} | tr ',' ' ');
1375                 do
1376                         case "${opt}" in
1377                                 bind)
1378                                         opt_bind="true"
1379                                         unset opt_link opt_union
1380                                         ;;
1381                                 link)
1382                                         opt_link="true"
1383                                         unset opt_bind opt_union
1384                                         ;;
1385                                 union)
1386                                         opt_union="true"
1387                                         unset opt_bind opt_link
1388                                         ;;
1389                         esac
1390                 done
1391
1392                 if [ -n "$(what_is_mounted_on "${dest}")" ]
1393                 then
1394                         if [ "${dest}" = "${rootmnt}" ]
1395                         then
1396                                 umount "${dest}"
1397                         else
1398                                 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1399                                 continue
1400                         fi
1401                 fi
1402
1403                 if [ ! -d "${dest}" ]
1404                 then
1405                         # create the destination and delete existing files in
1406                         # its path that are in the way
1407                         path="/"
1408                         for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1409                         do
1410                                 path=$(trim_path ${path}/${dir})
1411                                 if [ -f ${path} ]
1412                                 then
1413                                         rm -f ${path}
1414                                 fi
1415                                 if [ ! -e ${path} ]
1416                                 then
1417                                         mkdir -p ${path}
1418                                         if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1419                                         then
1420                                                 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1421                                                 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1422                                                 chown 1000:1000 ${path}
1423                                         fi
1424                                 fi
1425                         done
1426                 fi
1427
1428                 # if ${source} doesn't exist on our persistence media
1429                 # we bootstrap it with $dest from the live filesystem.
1430                 # this both makes sense and is critical if we're
1431                 # dealing with /etc or other system dir.
1432                 if [ ! -d "${source}" ]
1433                 then
1434                         if [ -n "${PERSISTENCE_READONLY}" ]
1435                         then
1436                                 continue
1437                         elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1438                         then
1439                                 # unions and don't need to be bootstrapped
1440                                 # link dirs can't be bootstrapped in a sensible way
1441                                 mkdir -p "${source}"
1442                                 chown_ref "${dest}" "${source}"
1443                                 chmod_ref "${dest}" "${source}"
1444                         elif [ -n "${opt_bind}" ]
1445                         then
1446                                 # ensure that $dest is not copied *into* $source
1447                                 mkdir -p "$(dirname ${source})"
1448                                 cp -a "${dest}" "${source}"
1449                         fi
1450                 fi
1451
1452                 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1453                 # ignore the loop below and set rofs_dest_backing=$dest
1454                 local rofs_dest_backing=""
1455                 if [ -n "${opt_link}"]
1456                 then
1457                         for d in /live/rofs/*
1458                         do
1459                                 if [ -n "${rootmnt}" ]
1460                                 then
1461                                         rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1462                                 else
1463                                         rofs_dest_backing="${d}/${dest}"
1464                                 fi
1465                                 if [ -d "${rofs_dest_backing}" ]
1466                                 then
1467                                         break
1468                                 else
1469                                         rofs_dest_backing=""
1470                                 fi
1471                         done
1472                 fi
1473
1474                 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1475                 then
1476                         link_files ${source} ${dest} ${rootmnt}
1477                 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1478                 then
1479                         mkdir -p /live/persistence
1480                         local links_source=$(mktemp -d /live/persistence/links-source-XXXXXX)
1481                         chown_ref ${source} ${links_source}
1482                         chmod_ref ${source} ${links_source}
1483                         # We put the cow dir in the below strange place to
1484                         # make it absolutely certain that the link source
1485                         # has its own directory and isn't nested with some
1486                         # other custom mount (if so that mount's files would
1487                         # be linked, causing breakage.
1488                         local cow_dir="/live/overlay/live/persistence/$(basename ${links_source})"
1489                         mkdir -p ${cow_dir}
1490                         chown_ref "${source}" "${cow_dir}"
1491                         chmod_ref "${source}" "${cow_dir}"
1492                         do_union ${links_source} ${cow_dir} ${source} ${rofs_dest_backing}
1493                         link_files ${links_source} ${dest} ${rootmnt}
1494                 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1495                 then
1496                         do_union ${dest} ${source} ${rofs_dest_backing}
1497                 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1498                 then
1499                         mount --bind "${source}" "${dest}"
1500                 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1501                 then
1502                         # bind-mount and union mount are handled the same
1503                         # in read-only mode, but note that rofs_dest_backing
1504                         # is non-empty (and necessary) only for unions
1505                         if [ -n "${rootmnt}" ]
1506                         then
1507                                 local cow_dir="$(echo ${dest} | sed -e "s|^${rootmnt}|/live/overlay/|")"
1508                         else
1509                                 # This is happens if persistence is activated
1510                                 # post boot
1511                                 local cow_dir="/live/overlay/${dest}"
1512                         fi
1513                         if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1514                         then
1515                                 # If an earlier custom mount has files here
1516                                 # it will "block" the current mount's files
1517                                 # which is undesirable
1518                                 rm -rf "${cow_dir}"
1519                         fi
1520                         mkdir -p ${cow_dir}
1521                         chown_ref "${source}" "${cow_dir}"
1522                         chmod_ref "${source}" "${cow_dir}"
1523                         do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1524                 fi
1525
1526                 PERSISTENCE_IS_ON="1"
1527                 export PERSISTENCE_IS_ON
1528
1529                 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1530                 then
1531                         used_devices="${used_devices} ${device}"
1532                 fi
1533         done < ${custom_mounts}
1534
1535         echo ${used_devices}
1536 }
1537
1538 fix_backwards_compatibility ()
1539 {
1540         local device=${1}
1541         local dir=${2}
1542         local opt=${3}
1543
1544         if [ -n "${PERSISTENCE_READONLY}" ]
1545         then
1546                 return
1547         fi
1548
1549         local backing="$(mount_persistence_media ${device})"
1550         if [ -z "${backing}" ]
1551         then
1552                 return
1553         fi
1554
1555         local include_list="${backing}/${persistence_list}"
1556         if [ ! -r "${include_list}" ]
1557         then
1558                 echo "# persistence backwards compatibility:
1559 ${dir} ${opt},source=." > "${include_list}"
1560         fi
1561 }
1562
1563 is_mountpoint ()
1564 {
1565         directory="$1"
1566
1567         [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]
1568 }