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