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