Dropping snapshot functionality, superseeded by awesome custom mount persistence.
[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 dev="${2}"
846
847         local gpt_dev="${dev}"
848         if is_active_luks_mapping ${dev}
849         then
850                 # if $dev is an opened luks device, we need to check
851                 # GPT stuff on the backing device
852                 gpt_dev=$(get_luks_backing_device "${dev}")
853         fi
854
855         if ! is_gpt_device ${gpt_dev}
856         then
857                 return
858         fi
859
860         local gpt_name=$(get_gpt_name ${gpt_dev})
861         for label in ${overlays}
862         do
863                 if [ "${gpt_name}" = "${label}" ]
864                 then
865                         echo "${label}=${dev}"
866                 fi
867         done
868 }
869
870 probe_for_fs_label ()
871 {
872         local overlays="${1}"
873         local dev="${2}"
874
875         for label in ${overlays}
876         do
877                 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
878                 then
879                         echo "${label}=${dev}"
880                 fi
881         done
882 }
883
884 probe_for_file_name ()
885 {
886         local overlays="${1}"
887         local dev="${2}"
888
889         local ret=""
890         local backing="$(mount_persistence_media ${dev} probe)"
891         if [ -z "${backing}" ]
892         then
893             return
894         fi
895
896         for label in ${overlays}
897         do
898                 path=${backing}/${PERSISTENCE_PATH}${label}
899                 if [ -f "${path}" ]
900                 then
901                         local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
902                         ret="${ret} ${label}=${loopdev}"
903                 fi
904         done
905
906         if [ -n "${ret}" ]
907         then
908                 echo ${ret}
909         else
910                 umount ${backing} > /dev/null 2>&1 || true
911         fi
912 }
913
914 find_persistence_media ()
915 {
916         # Scans devices for overlays, and returns a whitespace
917         # separated list of how to use them. Only overlays with a partition
918         # label or file name in ${overlays} are returned.
919         #
920         # When scanning a LUKS device, the user will be asked to enter the
921         # passphrase; on failure to enter it, or if no persistence partitions
922         # or files were found, the LUKS device is closed.
923         #
924         # For all other cases (overlay partition and overlay file) the
925         # return value is "${label}=${device}", where ${device} a device that
926         # can mount the content. In the case of an overlay file, the device
927         # containing the file will remain mounted as a side-effect.
928         #
929         # No devices in ${black_listed_devices} will be scanned, and if
930         # ${white_list_devices} is non-empty, only devices in it will be
931         # scanned.
932
933         local overlays="${1}"
934         local white_listed_devices="${2}"
935         local ret=""
936
937         local black_listed_devices="$(what_is_mounted_on /live/image)"
938
939         for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
940         do
941                 local result=""
942
943                 local luks_device=""
944                 # Check if it's a luks device; we'll have to open the device
945                 # in order to probe any filesystem it contains, like we do
946                 # below. activate_custom_mounts() also depends on that any luks
947                 # device already has been opened.
948                 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && \
949                    is_luks_partition ${dev}
950                 then
951                         if luks_device=$(open_luks_device "${dev}")
952                         then
953                                 dev="${luks_device}"
954                         else
955                                 # skip $dev since we failed/chose not to open it
956                                 continue
957                         fi
958                 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
959                 then
960                         # skip $dev since we don't allow unencrypted storage
961                         continue
962                 fi
963
964                 # Probe for matching GPT partition names or filesystem labels
965                 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
966                 then
967                         result=$(probe_for_gpt_name "${overlays}" ${dev})
968                         if [ -n "${result}" ]
969                         then
970                                 ret="${ret} ${result}"
971                                 continue
972                         fi
973
974                         result=$(probe_for_fs_label "${overlays}" ${dev})
975                         if [ -n "${result}" ]
976                         then
977                                 ret="${ret} ${result}"
978                                 continue
979                         fi
980                 fi
981
982                 # Probe for files with matching name on mounted partition
983                 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
984                 then
985                         result=$(probe_for_file_name "${overlays}" ${dev})
986                         if [ -n "${result}" ]
987                         then
988                                 ret="${ret} ${result}"
989                                 continue
990                         fi
991                 fi
992
993                 # Close luks device if it isn't used
994                 if [ -z "${result}" ] && [ -n "${luks_device}" ] && \
995                    is_active_luks_mapping "${luks_device}"
996                 then
997                         /sbin/cryptsetup luksClose "${luks_device}"
998                 fi
999         done
1000
1001         if [ -n "${ret}" ]
1002         then
1003                 echo ${ret}
1004         fi
1005 }
1006
1007 get_mac ()
1008 {
1009         mac=""
1010
1011         for adaptor in /sys/class/net/*
1012         do
1013                 status="$(cat ${adaptor}/iflink)"
1014
1015                 if [ "${status}" -eq 2 ]
1016                 then
1017                         mac="$(cat ${adaptor}/address)"
1018                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1019                 fi
1020         done
1021
1022         echo ${mac}
1023 }
1024
1025 is_luks_partition ()
1026 {
1027         device="${1}"
1028         /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1029 }
1030
1031 is_active_luks_mapping ()
1032 {
1033         device="${1}"
1034         /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1035 }
1036
1037 get_luks_backing_device () {
1038         device=${1}
1039         cryptsetup status ${device} 2> /dev/null | \
1040                 awk '{if ($1 == "device:") print $2}'
1041 }
1042
1043 removable_dev ()
1044 {
1045         output_format="${1}"
1046         want_usb="${2}"
1047         ret=
1048
1049         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1050         do
1051                 dev_ok=
1052                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1053                 then
1054                         if [ -z "${want_usb}" ]
1055                         then
1056                                 dev_ok="true"
1057                         else
1058                                 if readlink ${sysblock} | grep -q usb
1059                                 then
1060                                         dev_ok="true"
1061                                 fi
1062                         fi
1063                 fi
1064
1065                 if [ "${dev_ok}" = "true" ]
1066                 then
1067                         case "${output_format}" in
1068                                 sys)
1069                                         ret="${ret} ${sysblock}"
1070                                         ;;
1071                                 *)
1072                                         devname=$(sys2dev "${sysblock}")
1073                                         ret="${ret} ${devname}"
1074                                         ;;
1075                         esac
1076                 fi
1077         done
1078
1079         echo "${ret}"
1080 }
1081
1082 removable_usb_dev ()
1083 {
1084         output_format="${1}"
1085
1086         removable_dev "${output_format}" "want_usb"
1087 }
1088
1089 non_removable_dev ()
1090 {
1091         output_format="${1}"
1092         ret=
1093
1094         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1095         do
1096                 if [ "$(cat ${sysblock}/removable)" = "0" ]
1097                 then
1098                         case "${output_format}" in
1099                                 sys)
1100                                         ret="${ret} ${sysblock}"
1101                                         ;;
1102                                 *)
1103                                         devname=$(sys2dev "${sysblock}")
1104                                         ret="${ret} ${devname}"
1105                                         ;;
1106                         esac
1107                 fi
1108         done
1109
1110         echo "${ret}"
1111 }
1112
1113 link_files ()
1114 {
1115         # create source's directory structure in dest, and recursively
1116         # create symlinks in dest to to all files in source. if mask
1117         # is non-empty, remove mask from all source paths when
1118         # creating links (will be necessary if we change root, which
1119         # live-boot normally does (into $rootmnt)).
1120
1121         # remove multiple /:s and ensure ending on /
1122         local src_dir="$(trim_path ${1})/"
1123         local dest_dir="$(trim_path ${2})/"
1124         local src_mask="${3}"
1125
1126         # This check can only trigger on the inital, non-recursive call since
1127         # we create the destination before recursive calls
1128         if [ ! -d "${dest_dir}" ]
1129         then
1130                 log_warning_msg "Must link_files into a directory"
1131                 return
1132         fi
1133
1134         find "${src_dir}" -mindepth 1 -maxdepth 1 | while read src; do
1135                 local dest="${dest_dir}$(basename "${src}")"
1136                 if [ -d "${src}" ]
1137                 then
1138                         if [ -z "$(ls -A "${src}")" ]
1139                         then
1140                                 continue
1141                         fi
1142                         if [ ! -d "${dest}" ]
1143                         then
1144                                 mkdir -p "${dest}"
1145                                 chown_ref "${src}" "${dest}"
1146                                 chmod_ref "${src}" "${dest}"
1147                         fi
1148                         link_files "${src}" "${dest}" "${src_mask}"
1149                 else
1150                         local final_src=${src}
1151                         if [ -n "${src_mask}" ]
1152                         then
1153                                 final_src="$(echo ${final_src} | sed "s|^${src_mask}||")"
1154                         fi
1155                         rm -rf "${dest}" 2> /dev/null
1156                         ln -s "${final_src}" "${dest}"
1157                         chown_ref "${src}" "${dest}"
1158                 fi
1159         done
1160 }
1161
1162 do_union ()
1163 {
1164         local unionmountpoint="${1}"    # directory where the union is mounted
1165         local unionrw="${2}"            # branch where the union changes are stored
1166         local unionro1="${3}"           # first underlying read-only branch (optional)
1167         local unionro2="${4}"           # second underlying read-only branch (optional)
1168
1169         if [ "${UNIONTYPE}" = "aufs" ]
1170         then
1171                 rw_opt="rw"
1172                 ro_opt="rr+wh"
1173                 noxino_opt="noxino"
1174         elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1175         then
1176                 rw_opt="RW"
1177                 ro_opt="RO"
1178         else
1179                 rw_opt="rw"
1180                 ro_opt="ro"
1181         fi
1182
1183         case "${UNIONTYPE}" in
1184                 unionfs-fuse)
1185                         unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1186                         unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1187                         if [ -n "${unionro1}" ]
1188                         then
1189                                 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1190                         fi
1191                         if [ -n "${unionro2}" ]
1192                         then
1193                                 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1194                         fi
1195                         ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1196                         unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1197                         ( mkdir -p /run/sendsigs.omit.d
1198                         pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1199                         ;;
1200
1201                 overlayfs)
1202                         # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1203                         # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1204                         unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1205                         mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1206                         ;;
1207
1208                 *)
1209                         unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1210                         if [ -n "${unionro1}" ]
1211                         then
1212                                 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1213                         fi
1214                         if [ -n "${unionro2}" ]
1215                         then
1216                                 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1217                         fi
1218                         mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1219                         ;;
1220         esac
1221 }
1222
1223 get_custom_mounts ()
1224 {
1225         # Side-effect: leaves $devices with live-persistence.conf mounted in /live/persistence
1226         # Side-effect: prints info to file $custom_mounts
1227
1228         local custom_mounts=${1}
1229         shift
1230         local devices=${@}
1231
1232         local bindings="/tmp/bindings.list"
1233         local links="/tmp/links.list"
1234         rm -rf ${bindings} ${links} 2> /dev/null
1235
1236         for device in ${devices}
1237         do
1238                 if [ ! -b "${device}" ]
1239                 then
1240                         continue
1241                 fi
1242
1243                 local device_name="$(basename ${device})"
1244                 local backing=$(mount_persistence_media ${device})
1245                 if [ -z "${backing}" ]
1246                 then
1247                         continue
1248                 fi
1249
1250                 local include_list="${backing}/${persistence_list}"
1251                 if [ ! -r "${include_list}" ]
1252                 then
1253                         continue
1254                 fi
1255
1256                 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
1257                 then
1258                         cp ${include_list} /live/persistence/${persistence_list}.${device_name}
1259                 fi
1260
1261                 while read dir options # < ${include_list}
1262                 do
1263                         if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1264                         then
1265                                 # skipping empty or commented lines
1266                                 continue
1267                         fi
1268
1269                         if trim_path ${dir} | grep -q -e "^[^/]" -e "^/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1270                         then
1271                                 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."
1272                                 continue
1273                         fi
1274
1275                         local opt_source=""
1276                         local opt_link=""
1277                         for opt in $(echo ${options} | tr ',' ' ');
1278                         do
1279                                 case "${opt}" in
1280                                         source=*)
1281                                                 opt_source=${opt#source=}
1282                                                 ;;
1283                                         link)
1284                                                 opt_link="true"
1285                                                 ;;
1286                                         union|bind)
1287                                                 ;;
1288                                         *)
1289                                                 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1290                                                 continue 2
1291                                                 ;;
1292                                 esac
1293                         done
1294
1295                         local source="${dir}"
1296                         if [ -n "${opt_source}" ]
1297                         then
1298                                 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${source}" != "." ]
1299                                 then
1300                                         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"
1301                                         continue
1302                                 else
1303                                         source="${opt_source}"
1304                                 fi
1305                         fi
1306
1307                         local full_source="$(trim_path ${backing}/${source})"
1308                         local full_dest="$(trim_path ${rootmnt}/${dir})"
1309                         if [ -n "${opt_link}" ]
1310                         then
1311                                 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1312                         else
1313                                 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1314                         fi
1315                 done < ${include_list}
1316         done
1317
1318         # We sort the list according to destination so we're sure that
1319         # we won't hide a previous mount. We also ignore duplicate
1320         # destinations in a more or less arbitrary way.
1321         [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1322
1323         # After all mounts are considered we add symlinks so they
1324         # won't be hidden by some mount.
1325         [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
1326
1327         # We need to make sure that no two custom mounts have the same sources
1328         # or are nested; if that is the case, too much weird stuff can happen.
1329         local prev_source="impossible source" # first iteration must not match
1330         local prev_dest=""
1331         # This sort will ensure that a source /a comes right before a source
1332         # /a/b so we only need to look at the previous source
1333         sort -k2 -b ${custom_mounts} |
1334         while read device source dest options
1335         do
1336                 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1337                 then
1338                         panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1339                 fi
1340                 prev_source=${source}
1341                 prev_dest=${dest}
1342         done
1343 }
1344
1345 activate_custom_mounts ()
1346 {
1347         local custom_mounts="${1}" # the ouput from get_custom_mounts()
1348         local used_devices=""
1349
1350         while read device source dest options # < ${custom_mounts}
1351         do
1352                 local opt_bind="true"
1353                 local opt_link=""
1354                 local opt_union=""
1355                 for opt in $(echo ${options} | tr ',' ' ');
1356                 do
1357                         case "${opt}" in
1358                                 bind)
1359                                         opt_bind="true"
1360                                         unset opt_link opt_union
1361                                         ;;
1362                                 link)
1363                                         opt_link="true"
1364                                         unset opt_bind opt_union
1365                                         ;;
1366                                 union)
1367                                         opt_union="true"
1368                                         unset opt_bind opt_link
1369                                         ;;
1370                         esac
1371                 done
1372
1373                 if [ -n "$(what_is_mounted_on "${dest}")" ]
1374                 then
1375                         if [ "${dest}" = "${rootmnt}" ]
1376                         then
1377                                 umount "${dest}"
1378                         else
1379                                 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1380                                 continue
1381                         fi
1382                 fi
1383
1384                 if [ ! -d "${dest}" ]
1385                 then
1386                         # create the destination and delete existing files in
1387                         # its path that are in the way
1388                         path="/"
1389                         for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1390                         do
1391                                 path=$(trim_path ${path}/${dir})
1392                                 if [ -f ${path} ]
1393                                 then
1394                                         rm -f ${path}
1395                                 fi
1396                                 if [ ! -e ${path} ]
1397                                 then
1398                                         mkdir -p ${path}
1399                                         if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1400                                         then
1401                                                 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1402                                                 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1403                                                 chown 1000:1000 ${path}
1404                                         fi
1405                                 fi
1406                         done
1407                 fi
1408
1409                 # if ${source} doesn't exist on our persistence media
1410                 # we bootstrap it with $dest from the live filesystem.
1411                 # this both makes sense and is critical if we're
1412                 # dealing with /etc or other system dir.
1413                 if [ ! -d "${source}" ]
1414                 then
1415                         if [ -n "${PERSISTENCE_READONLY}" ]
1416                         then
1417                                 continue
1418                         elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1419                         then
1420                                 # unions and don't need to be bootstrapped
1421                                 # link dirs can't be bootstrapped in a sensible way
1422                                 mkdir -p "${source}"
1423                                 chown_ref "${dest}" "${source}"
1424                                 chmod_ref "${dest}" "${source}"
1425                         elif [ -n "${opt_bind}" ]
1426                         then
1427                                 # ensure that $dest is not copied *into* $source
1428                                 mkdir -p "$(dirname ${source})"
1429                                 cp -a "${dest}" "${source}"
1430                         fi
1431                 fi
1432
1433                 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1434                 # ignore the loop below and set rofs_dest_backing=$dest
1435                 local rofs_dest_backing=""
1436                 if [ -n "${opt_link}"]
1437                 then
1438                         for d in /live/rofs/*
1439                         do
1440                                 if [ -n "${rootmnt}" ]
1441                                 then
1442                                         rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1443                                 else
1444                                         rofs_dest_backing="${d}/${dest}"
1445                                 fi
1446                                 if [ -d "${rofs_dest_backing}" ]
1447                                 then
1448                                         break
1449                                 else
1450                                         rofs_dest_backing=""
1451                                 fi
1452                         done
1453                 fi
1454
1455                 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1456                 then
1457                         link_files ${source} ${dest} ${rootmnt}
1458                 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1459                 then
1460                         mkdir -p /live/persistence
1461                         local links_source=$(mktemp -d /live/persistence/links-source-XXXXXX)
1462                         chown_ref ${source} ${links_source}
1463                         chmod_ref ${source} ${links_source}
1464                         # We put the cow dir in the below strange place to
1465                         # make it absolutely certain that the link source
1466                         # has its own directory and isn't nested with some
1467                         # other custom mount (if so that mount's files would
1468                         # be linked, causing breakage.
1469                         local cow_dir="/live/overlay/live/persistence/$(basename ${links_source})"
1470                         mkdir -p ${cow_dir}
1471                         chown_ref "${source}" "${cow_dir}"
1472                         chmod_ref "${source}" "${cow_dir}"
1473                         do_union ${links_source} ${cow_dir} ${source} ${rofs_dest_backing}
1474                         link_files ${links_source} ${dest} ${rootmnt}
1475                 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1476                 then
1477                         do_union ${dest} ${source} ${rofs_dest_backing}
1478                 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1479                 then
1480                         mount --bind "${source}" "${dest}"
1481                 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1482                 then
1483                         # bind-mount and union mount are handled the same
1484                         # in read-only mode, but note that rofs_dest_backing
1485                         # is non-empty (and necessary) only for unions
1486                         if [ -n "${rootmnt}" ]
1487                         then
1488                                 local cow_dir="$(echo ${dest} | sed -e "s|^${rootmnt}|/live/overlay/|")"
1489                         else
1490                                 # This is happens if persistence is activated
1491                                 # post boot
1492                                 local cow_dir="/live/overlay/${dest}"
1493                         fi
1494                         if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1495                         then
1496                                 # If an earlier custom mount has files here
1497                                 # it will "block" the current mount's files
1498                                 # which is undesirable
1499                                 rm -rf "${cow_dir}"
1500                         fi
1501                         mkdir -p ${cow_dir}
1502                         chown_ref "${source}" "${cow_dir}"
1503                         chmod_ref "${source}" "${cow_dir}"
1504                         do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1505                 fi
1506
1507                 PERSISTENCE_IS_ON="1"
1508                 export PERSISTENCE_IS_ON
1509
1510                 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1511                 then
1512                         used_devices="${used_devices} ${device}"
1513                 fi
1514         done < ${custom_mounts}
1515
1516         echo ${used_devices}
1517 }
1518
1519 fix_backwards_compatibility ()
1520 {
1521         local device=${1}
1522         local dir=${2}
1523         local opt=${3}
1524
1525         if [ -n "${PERSISTENCE_READONLY}" ]
1526         then
1527                 return
1528         fi
1529
1530         local backing="$(mount_persistence_media ${device})"
1531         if [ -z "${backing}" ]
1532         then
1533                 return
1534         fi
1535
1536         local include_list="${backing}/${persistence_list}"
1537         if [ ! -r "${include_list}" ]
1538         then
1539                 echo "# persistence backwards compatibility:
1540 ${dir} ${opt},source=." > "${include_list}"
1541         fi
1542 }
1543
1544 is_mountpoint ()
1545 {
1546         directory="$1"
1547
1548         [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]
1549 }