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