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