Splitting out a first bunch of functions out to /live/live/boot/.
[live-boot-grml.git] / initramfs-tools / scripts / live-helpers
1 # live-boot helper functions, used by live-boot on boot and by live-snapshot
2
3 if [ ! -x "/bin/fstype" ]
4 then
5         # klibc not in path -> not in initramfs
6         export PATH="${PATH}:/usr/lib/klibc/bin"
7 fi
8
9 # handle upgrade path from old udev (using udevinfo) to
10 # recent versions of udev (using udevadm info)
11 if [ -x /sbin/udevadm ]
12 then
13         udevinfo='/sbin/udevadm info'
14 else
15         udevinfo='udevinfo'
16 fi
17
18 old_root_overlay_label="live-rw"
19 old_home_overlay_label="home-rw"
20 custom_overlay_label="custom-ov"
21 root_snapshot_label="live-sn"
22 old_root_snapshot_label="live-sn"
23 home_snapshot_label="home-sn"
24 persistence_list="live-persistence.conf"
25
26 is_in_list_separator_helper () {
27         local sep=${1}
28         shift
29         local element=${1}
30         shift
31         local list=${*}
32         echo ${list} | grep -qe "^\(.*${sep}\)\?${element}\(${sep}.*\)\?$"
33 }
34
35 is_in_space_sep_list () {
36         local element=${1}
37         shift
38         is_in_list_separator_helper "[[:space:]]" "${element}" "${*}"
39 }
40
41 is_in_comma_sep_list () {
42         local element=${1}
43         shift
44         is_in_list_separator_helper "," "${element}" "${*}"
45 }
46
47 sys2dev ()
48 {
49         sysdev=${1#/sys}
50         echo "/dev/$($udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
51 }
52
53 subdevices ()
54 {
55         sysblock=${1}
56         r=""
57
58         for dev in "${sysblock}"/* "${sysblock}"
59         do
60                 if [ -e "${dev}/dev" ]
61                 then
62                         r="${r} ${dev}"
63                 fi
64         done
65
66         echo ${r}
67 }
68
69 storage_devices()
70 {
71         black_listed_devices="${1}"
72         white_listed_devices="${2}"
73
74         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
75         do
76                 fulldevname=$(sys2dev "${sysblock}")
77
78                 if is_in_space_sep_list ${fulldevname} ${black_listed_devices} || \
79                         [ -n "${white_listed_devices}" ] && \
80                         ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
81                 then
82                         # skip this device entirely
83                         continue
84                 fi
85
86                 for dev in $(subdevices "${sysblock}")
87                 do
88                         devname=$(sys2dev "${dev}")
89
90                         if is_in_space_sep_list ${devname} ${black_listed_devices}
91                         then
92                                 # skip this subdevice
93                                 continue
94                         else
95                                 echo "${devname}"
96                         fi
97                 done
98         done
99 }
100
101 is_supported_fs ()
102 {
103         fstype="${1}"
104
105         # Validate input first
106         if [ -z "${fstype}" ]
107         then
108                 return 1
109         fi
110
111         # Try to look if it is already supported by the kernel
112         if grep -q ${fstype} /proc/filesystems
113         then
114                 return 0
115         else
116                 # Then try to add support for it the gentle way using the initramfs capabilities
117                 modprobe ${fstype}
118                 if grep -q ${fstype} /proc/filesystems
119                 then
120                         return 0
121                 # Then try the hard way if /root is already reachable
122                 else
123                         kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
124                         if [ -e "${kmodule}" ]
125                         then
126                                 insmod "${kmodule}"
127                                 if grep -q ${fstype} /proc/filesystems
128                                 then
129                                         return 0
130                                 fi
131                         fi
132                 fi
133         fi
134
135         return 1
136 }
137
138 get_fstype ()
139 {
140         /sbin/blkid -s TYPE -o value $1 2>/dev/null
141 }
142
143 where_is_mounted ()
144 {
145         device=${1}
146         # return first found
147         grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
148 }
149
150 trim_path () {
151     # remove all unnecessary /:s in the path, including last one (except
152     # if path is just "/")
153     echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
154 }
155
156 what_is_mounted_on ()
157 {
158         local dir="$(trim_path ${1})"
159         grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
160 }
161
162 chown_ref ()
163 {
164         local reference="${1}"
165         shift
166         local targets=${@}
167         local owner=$(stat -c %u:%g "${reference}")
168         chown -h ${owner} ${targets}
169 }
170
171 chmod_ref ()
172 {
173         local reference="${1}"
174         shift
175         local targets=${@}
176         local rights=$(stat -c %a "${reference}")
177         chmod ${rights} ${targets}
178 }
179
180 lastline ()
181 {
182         while read lines
183         do
184                 line=${lines}
185         done
186
187         echo "${line}"
188 }
189
190 base_path ()
191 {
192         testpath="${1}"
193         mounts="$(awk '{print $2}' /proc/mounts)"
194         testpath="$(busybox realpath ${testpath})"
195
196         while true
197         do
198                 if echo "${mounts}" | grep -qs "^${testpath}"
199                 then
200                         set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
201                         echo ${1}
202                         break
203                 else
204                         testpath=$(dirname $testpath)
205                 fi
206         done
207 }
208
209 fs_size ()
210 {
211         # Returns used/free fs kbytes + 5% more
212         # You could pass a block device as ${1} or the mount point as ${2}
213
214         dev="${1}"
215         mountp="${2}"
216         used="${3}"
217
218         if [ -z "${mountp}" ]
219         then
220                 mountp="$(where_is_mounted ${dev})"
221
222                 if [ -z "${mountp}" ]
223                 then
224                         mountp="/mnt/tmp_fs_size"
225
226                         mkdir -p "${mountp}"
227                         mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
228
229                         doumount=1
230                 fi
231         fi
232
233         if [ "${used}" = "used" ]
234         then
235                 size=$(du -ks ${mountp} | cut -f1)
236                 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
237         else
238                 # free space
239                 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
240         fi
241
242         if [ -n "${doumount}" ]
243         then
244                 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
245                 rmdir "${mountp}"
246         fi
247
248         echo "${size}"
249 }
250
251 load_keymap ()
252 {
253         # Load custom keymap
254         if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
255         then
256                 loadkeys /etc/boottime.kmap.gz
257         fi
258 }
259
260 setup_loop ()
261 {
262         local fspath=${1}
263         local module=${2}
264         local pattern=${3}
265         local offset=${4}
266         local encryption=${5}
267         local readonly=${6}
268
269         # the output of setup_loop is evaluated in other functions,
270         # modprobe leaks kernel options like "libata.dma=0"
271         # as "options libata dma=0" on stdout, causing serious
272         # problems therefor, so instead always avoid output to stdout
273         modprobe -q -b "${module}" 1>/dev/null
274
275         udevadm settle
276
277         for loopdev in ${pattern}
278         do
279                 if [ "$(cat ${loopdev}/size)" -eq 0 ]
280                 then
281                         dev=$(sys2dev "${loopdev}")
282                         options=''
283
284                         if [ -n "${readonly}" ]
285                         then
286                                 if losetup --help 2>&1 | grep -q -- "-r\b"
287                                 then
288                                         options="${options} -r"
289                                 fi
290                         fi
291
292                         if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
293                         then
294                                 options="${options} -o ${offset}"
295                         fi
296
297                         if [ -z "${encryption}" ]
298                         then
299                                 losetup ${options} "${dev}" "${fspath}"
300                         else
301                                 # Loop AES encryption
302                                 while true
303                                 do
304                                         load_keymap
305
306                                         echo -n "Enter passphrase for root filesystem: " >&6
307                                         read -s passphrase
308                                         echo "${passphrase}" > /tmp/passphrase
309                                         unset passphrase
310                                         exec 9</tmp/passphrase
311                                         /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
312                                         error=${?}
313                                         exec 9<&-
314                                         rm -f /tmp/passphrase
315
316                                         if [ 0 -eq ${error} ]
317                                         then
318                                                 unset error
319                                                 break
320                                         fi
321
322                                         echo
323                                         echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
324                                         read answer
325
326                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
327                                         then
328                                                 unset answer
329                                                 break
330                                         fi
331                                 done
332                         fi
333
334                         echo "${dev}"
335                         return 0
336                 fi
337         done
338
339         panic "No loop devices available"
340 }
341
342 try_mount ()
343 {
344         dev="${1}"
345         mountp="${2}"
346         opts="${3}"
347         fstype="${4}"
348
349         old_mountp="$(where_is_mounted ${dev})"
350
351         if [ -n "${old_mountp}" ]
352         then
353                 if [ "${opts}" != "ro" ]
354                 then
355                         mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
356                 fi
357
358                 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
359         else
360                 if [ -z "${fstype}" ]
361                 then
362                         fstype=$(get_fstype "${dev}")
363                 fi
364                 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
365                 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > boot.log && return 0 )
366         fi
367 }
368
369 mount_persistence_media ()
370 {
371         local device=${1}
372         local probe=${2}
373
374         local backing="/live/persistence/$(basename ${device})"
375
376         mkdir -p "${backing}"
377         local old_backing="$(where_is_mounted ${device})"
378         if [ -z "${old_backing}" ]
379         then
380                 local fstype="$(get_fstype ${device})"
381                 local mount_opts="rw,noatime"
382                 if [ -n "${PERSISTENCE_READONLY}" ]
383                 then
384                         mount_opts="ro,noatime"
385                 fi
386                 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
387                 then
388                         echo ${backing}
389                         return 0
390                 else
391                         [ -z "${probe}" ] && log_warning_msg "Failed to mount persistence media ${device}"
392                         rmdir "${backing}"
393                         return 1
394                 fi
395         elif [ "${backing}" != "${old_backing}" ]
396         then
397                 if mount --move ${old_backing} ${backing} >/dev/null
398                 then
399                         echo ${backing}
400                         return 0
401                 else
402                         [ -z "${probe}" ] && log_warning_msg "Failed to move persistence media ${device}"
403                         rmdir "${backing}"
404                         return 1
405                 fi
406         fi
407         return 0
408 }
409
410 close_persistence_media () {
411         local device=${1}
412         local backing="$(where_is_mounted ${device})"
413
414         if [ -d "${backing}" ]
415         then
416                 umount "${backing}" >/dev/null 2>&1
417                 rmdir "${backing}" >/dev/null 2>&1
418         fi
419
420         if is_active_luks_mapping ${device}
421         then
422                 /sbin/cryptsetup luksClose ${device}
423         fi
424 }
425
426 open_luks_device ()
427 {
428         dev="${1}"
429         name="$(basename ${dev})"
430         opts="--key-file=-"
431         if [ -n "${PERSISTENCE_READONLY}" ]
432         then
433                 opts="${opts} --readonly"
434         fi
435
436         if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
437         then
438                 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
439                 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
440                 if [ "${opened_dev}" = "${dev}" ]
441                 then
442                         luks_device="/dev/mapper/${name}"
443                         echo ${luks_device}
444                         return 0
445                 else
446                         log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
447                         return 1
448                 fi
449         fi
450
451         load_keymap
452
453         while true
454         do
455                 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
456                         /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
457
458                 if [ 0 -eq ${?} ]
459                 then
460                         luks_device="/dev/mapper/${name}"
461                         echo ${luks_device}
462                         return 0
463                 fi
464
465                 echo >&6
466                 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
467                 read answer
468
469                 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
470                 then
471                         return 2
472                 fi
473         done
474 }
475
476 get_gpt_name ()
477 {
478     local dev="${1}"
479     /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
480 }
481
482 is_gpt_device ()
483 {
484     local dev="${1}"
485     [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
486 }
487
488 probe_for_gpt_name ()
489 {
490         local overlays="${1}"
491         local snapshots="${2}"
492         local dev="${3}"
493
494         local gpt_dev="${dev}"
495         if is_active_luks_mapping ${dev}
496         then
497                 # if $dev is an opened luks device, we need to check
498                 # GPT stuff on the backing device
499                 gpt_dev=$(get_luks_backing_device "${dev}")
500         fi
501
502         if ! is_gpt_device ${gpt_dev}
503         then
504                 return
505         fi
506
507         local gpt_name=$(get_gpt_name ${gpt_dev})
508         for label in ${overlays} ${snapshots}
509         do
510                 if [ "${gpt_name}" = "${label}" ]
511                 then
512                         echo "${label}=${dev}"
513                 fi
514         done
515 }
516
517 probe_for_fs_label ()
518 {
519         local overlays="${1}"
520         local snapshots="${2}"
521         local dev="${3}"
522
523         for label in ${overlays} ${snapshots}
524         do
525                 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
526                 then
527                         echo "${label}=${dev}"
528                 fi
529         done
530 }
531
532 probe_for_file_name ()
533 {
534         local overlays="${1}"
535         local snapshots="${2}"
536         local dev="${3}"
537
538         local ret=""
539         local backing="$(mount_persistence_media ${dev} probe)"
540         if [ -z "${backing}" ]
541         then
542             return
543         fi
544
545         for label in ${overlays}
546         do
547                 path=${backing}/${PERSISTENCE_PATH}${label}
548                 if [ -f "${path}" ]
549                 then
550                         local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
551                         ret="${ret} ${label}=${loopdev}"
552                 fi
553         done
554         for label in ${snapshots}
555         do
556                 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
557                 do
558                         path="${PERSISTENCE_PATH}${label}.${ext}"
559                         if [ -f "${backing}/${path}" ]
560                         then
561                                 ret="${ret} ${label}=${dev}:${backing}:${path}"
562                         fi
563                 done
564         done
565
566         if [ -n "${ret}" ]
567         then
568                 echo ${ret}
569         else
570                 umount ${backing} > /dev/null 2>&1 || true
571         fi
572 }
573
574 find_persistence_media ()
575 {
576         # Scans devices for overlays and snapshots, and returns a whitespace
577         # separated list of how to use them. Only overlays with a partition
578         # label or file name in ${overlays} are returned, and ditto for
579         # snapshots with labels in ${snapshots}.
580         #
581         # When scanning a LUKS device, the user will be asked to enter the
582         # passphrase; on failure to enter it, or if no persistence partitions
583         # or files were found, the LUKS device is closed.
584         #
585         # For a snapshot file the return value is ${label}=${snapdata}", where
586         # ${snapdata} is the parameter used for try_snap().
587         #
588         # For all other cases (overlay/snapshot partition and overlay file) the
589         # return value is "${label}=${device}", where ${device} a device that
590         # can mount the content. In the case of an overlay file, the device
591         # containing the file will remain mounted as a side-effect.
592         #
593         # No devices in ${black_listed_devices} will be scanned, and if
594         # ${white_list_devices} is non-empty, only devices in it will be
595         # scanned.
596
597         local overlays="${1}"
598         local snapshots="${2}"
599         local white_listed_devices="${3}"
600         local ret=""
601
602         local black_listed_devices="$(what_is_mounted_on /live/image)"
603
604         for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
605         do
606                 local result=""
607
608                 local luks_device=""
609                 # Check if it's a luks device; we'll have to open the device
610                 # in order to probe any filesystem it contains, like we do
611                 # below. activate_custom_mounts() also depends on that any luks
612                 # device already has been opened.
613                 if is_in_comma_sep_list luks ${PERSISTENCE_ENCRYPTION} && \
614                    is_luks_partition ${dev}
615                 then
616                         if luks_device=$(open_luks_device "${dev}")
617                         then
618                                 dev="${luks_device}"
619                         else
620                                 # skip $dev since we failed/chose not to open it
621                                 continue
622                         fi
623                 elif ! is_in_comma_sep_list none ${PERSISTENCE_ENCRYPTION}
624                 then
625                         # skip $dev since we don't allow unencrypted storage
626                         continue
627                 fi
628
629                 # Probe for matching GPT partition names or filesystem labels
630                 if is_in_comma_sep_list filesystem ${PERSISTENCE_STORAGE}
631                 then
632                         result=$(probe_for_gpt_name "${overlays}" "${snapshots}" ${dev})
633                         if [ -n "${result}" ]
634                         then
635                                 ret="${ret} ${result}"
636                                 continue
637                         fi
638
639                         result=$(probe_for_fs_label "${overlays}" "${snapshots}" ${dev})
640                         if [ -n "${result}" ]
641                         then
642                                 ret="${ret} ${result}"
643                                 continue
644                         fi
645                 fi
646
647                 # Probe for files with matching name on mounted partition
648                 if is_in_comma_sep_list file ${PERSISTENCE_STORAGE}
649                 then
650                         result=$(probe_for_file_name "${overlays}" "${snapshots}" ${dev})
651                         if [ -n "${result}" ]
652                         then
653                                 ret="${ret} ${result}"
654                                 continue
655                         fi
656                 fi
657
658                 # Close luks device if it isn't used
659                 if [ -z "${result}" ] && [ -n "${luks_device}" ] && \
660                    is_active_luks_mapping "${luks_device}"
661                 then
662                         /sbin/cryptsetup luksClose "${luks_device}"
663                 fi
664         done
665
666         if [ -n "${ret}" ]
667         then
668                 echo ${ret}
669         fi
670 }
671
672 get_mac ()
673 {
674         mac=""
675
676         for adaptor in /sys/class/net/*
677         do
678                 status="$(cat ${adaptor}/iflink)"
679
680                 if [ "${status}" -eq 2 ]
681                 then
682                         mac="$(cat ${adaptor}/address)"
683                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
684                 fi
685         done
686
687         echo ${mac}
688 }
689
690 is_luks_partition ()
691 {
692         device="${1}"
693         /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
694 }
695
696 is_active_luks_mapping ()
697 {
698         device="${1}"
699         /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
700 }
701
702 get_luks_backing_device () {
703         device=${1}
704         cryptsetup status ${device} 2> /dev/null | \
705                 awk '{if ($1 == "device:") print $2}'
706 }
707
708 removable_dev ()
709 {
710         output_format="${1}"
711         want_usb="${2}"
712         ret=
713
714         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
715         do
716                 dev_ok=
717                 if [ "$(cat ${sysblock}/removable)" = "1" ]
718                 then
719                         if [ -z "${want_usb}" ]
720                         then
721                                 dev_ok="yes"
722                         else
723                                 if readlink ${sysblock} | grep -q usb
724                                 then
725                                         dev_ok="yes"
726                                 fi
727                         fi
728                 fi
729
730                 if [ "${dev_ok}" = "yes" ]
731                 then
732                         case "${output_format}" in
733                                 sys)
734                                         ret="${ret} ${sysblock}"
735                                         ;;
736                                 *)
737                                         devname=$(sys2dev "${sysblock}")
738                                         ret="${ret} ${devname}"
739                                         ;;
740                         esac
741                 fi
742         done
743
744         echo "${ret}"
745 }
746
747 removable_usb_dev ()
748 {
749         output_format="${1}"
750
751         removable_dev "${output_format}" "want_usb"
752 }
753
754 non_removable_dev ()
755 {
756         output_format="${1}"
757         ret=
758
759         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
760         do
761                 if [ "$(cat ${sysblock}/removable)" = "0" ]
762                 then
763                         case "${output_format}" in
764                                 sys)
765                                         ret="${ret} ${sysblock}"
766                                         ;;
767                                 *)
768                                         devname=$(sys2dev "${sysblock}")
769                                         ret="${ret} ${devname}"
770                                         ;;
771                         esac
772                 fi
773         done
774
775         echo "${ret}"
776 }
777
778 link_files ()
779 {
780         # create source's directory structure in dest, and recursively
781         # create symlinks in dest to to all files in source. if mask
782         # is non-empty, remove mask from all source paths when
783         # creating links (will be necessary if we change root, which
784         # live-boot normally does (into $rootmnt)).
785
786         # remove multiple /:s and ensure ending on /
787         local src_dir="$(trim_path ${1})/"
788         local dest_dir="$(trim_path ${2})/"
789         local src_mask="${3}"
790
791         # This check can only trigger on the inital, non-recursive call since
792         # we create the destination before recursive calls
793         if [ ! -d "${dest_dir}" ]
794         then
795                 log_warning_msg "Must link_files into a directory"
796                 return
797         fi
798
799         find "${src_dir}" -mindepth 1 -maxdepth 1 | while read src; do
800                 local dest="${dest_dir}$(basename "${src}")"
801                 if [ -d "${src}" ]
802                 then
803                         if [ -z "$(ls -A "${src}")" ]
804                         then
805                                 continue
806                         fi
807                         if [ ! -d "${dest}" ]
808                         then
809                                 mkdir -p "${dest}"
810                                 chown_ref "${src}" "${dest}"
811                                 chmod_ref "${src}" "${dest}"
812                         fi
813                         link_files "${src}" "${dest}" "${src_mask}"
814                 else
815                         local final_src=${src}
816                         if [ -n "${src_mask}" ]
817                         then
818                                 final_src="$(echo ${final_src} | sed "s|^${src_mask}||")"
819                         fi
820                         rm -rf "${dest}" 2> /dev/null
821                         ln -s "${final_src}" "${dest}"
822                         chown_ref "${src}" "${dest}"
823                 fi
824         done
825 }
826
827 do_union ()
828 {
829         local unionmountpoint="${1}"    # directory where the union is mounted
830         local unionrw="${2}"            # branch where the union changes are stored
831         local unionro1="${3}"           # first underlying read-only branch (optional)
832         local unionro2="${4}"           # second underlying read-only branch (optional)
833
834         if [ "${UNIONTYPE}" = "aufs" ]
835         then
836                 rw_opt="rw"
837                 ro_opt="rr+wh"
838                 noxino_opt="noxino"
839         elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
840         then
841                 rw_opt="RW"
842                 ro_opt="RO"
843         else
844                 rw_opt="rw"
845                 ro_opt="ro"
846         fi
847
848         case "${UNIONTYPE}" in
849                 unionfs-fuse)
850                         unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
851                         unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
852                         if [ -n "${unionro1}" ]
853                         then
854                                 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
855                         fi
856                         if [ -n "${unionro2}" ]
857                         then
858                                 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
859                         fi
860                         ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
861                         unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
862                         ( mkdir -p /run/sendsigs.omit.d
863                         pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
864                         ;;
865
866                 overlayfs)
867                         # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
868                         # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
869                         unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
870                         mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
871                         ;;
872
873                 *)
874                         unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
875                         if [ -n "${unionro1}" ]
876                         then
877                                 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
878                         fi
879                         if [ -n "${unionro2}" ]
880                         then
881                                 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
882                         fi
883                         mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
884                         ;;
885         esac
886 }
887
888 get_custom_mounts ()
889 {
890         # Side-effect: leaves $devices with live-persistence.conf mounted in /live/persistence
891         # Side-effect: prints info to file $custom_mounts
892
893         local custom_mounts=${1}
894         shift
895         local devices=${@}
896
897         local bindings="/tmp/bindings.list"
898         local links="/tmp/links.list"
899         rm -rf ${bindings} ${links} 2> /dev/null
900
901         for device in ${devices}
902         do
903                 if [ ! -b "${device}" ]
904                 then
905                         continue
906                 fi
907
908                 local device_name="$(basename ${device})"
909                 local backing=$(mount_persistence_media ${device})
910                 if [ -z "${backing}" ]
911                 then
912                         continue
913                 fi
914
915                 local include_list="${backing}/${persistence_list}"
916                 if [ ! -r "${include_list}" ]
917                 then
918                         continue
919                 fi
920
921                 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
922                 then
923                         cp ${include_list} /live/persistence/${persistence_list}.${device_name}
924                 fi
925
926                 while read dir options # < ${include_list}
927                 do
928                         if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
929                         then
930                                 # skipping empty or commented lines
931                                 continue
932                         fi
933
934                         if trim_path ${dir} | grep -q -e "^[^/]" -e "^/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
935                         then
936                                 log_warning_msg "Skipping unsafe custom mount ${dir}: must be an absolute path containing neither the \".\" nor \"..\" special dirs, and cannot be \"/live\" or any sub-directory therein."
937                                 continue
938                         fi
939
940                         local opt_source=""
941                         local opt_link=""
942                         for opt in $(echo ${options} | tr ',' ' ');
943                         do
944                                 case "${opt}" in
945                                         source=*)
946                                                 opt_source=${opt#source=}
947                                                 ;;
948                                         link)
949                                                 opt_link="yes"
950                                                 ;;
951                                         union|bind)
952                                                 ;;
953                                         *)
954                                                 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
955                                                 continue 2
956                                                 ;;
957                                 esac
958                         done
959
960                         local source="${dir}"
961                         if [ -n "${opt_source}" ]
962                         then
963                                 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${source}" != "." ]
964                                 then
965                                         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"
966                                         continue
967                                 else
968                                         source="${opt_source}"
969                                 fi
970                         fi
971
972                         local full_source="$(trim_path ${backing}/${source})"
973                         local full_dest="$(trim_path ${rootmnt}/${dir})"
974                         if [ -n "${opt_link}" ]
975                         then
976                                 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
977                         else
978                                 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
979                         fi
980                 done < ${include_list}
981         done
982
983         # We sort the list according to destination so we're sure that
984         # we won't hide a previous mount. We also ignore duplicate
985         # destinations in a more or less arbitrary way.
986         [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
987
988         # After all mounts are considered we add symlinks so they
989         # won't be hidden by some mount.
990         [ -e "${links}" ] && cat ${links} >> ${custom_mounts} && rm ${links}
991
992         # We need to make sure that no two custom mounts have the same sources
993         # or are nested; if that is the case, too much weird stuff can happen.
994         local prev_source="impossible source" # first iteration must not match
995         local prev_dest=""
996         # This sort will ensure that a source /a comes right before a source
997         # /a/b so we only need to look at the previous source
998         sort -k2 -b ${custom_mounts} |
999         while read device source dest options
1000         do
1001                 if echo ${source} | grep -qe "^${prev_source}\(/.*\)\?$"
1002                 then
1003                         panic "Two persistence mounts have the same or nested sources: ${source} on ${dest}, and ${prev_source} on ${prev_dest}"
1004                 fi
1005                 prev_source=${source}
1006                 prev_dest=${dest}
1007         done
1008 }
1009
1010 activate_custom_mounts ()
1011 {
1012         local custom_mounts="${1}" # the ouput from get_custom_mounts()
1013         local used_devices=""
1014
1015         while read device source dest options # < ${custom_mounts}
1016         do
1017                 local opt_bind="yes"
1018                 local opt_link=""
1019                 local opt_union=""
1020                 for opt in $(echo ${options} | tr ',' ' ');
1021                 do
1022                         case "${opt}" in
1023                                 bind)
1024                                         opt_bind="yes"
1025                                         unset opt_link opt_union
1026                                         ;;
1027                                 link)
1028                                         opt_link="yes"
1029                                         unset opt_bind opt_union
1030                                         ;;
1031                                 union)
1032                                         opt_union="yes"
1033                                         unset opt_bind opt_link
1034                                         ;;
1035                         esac
1036                 done
1037
1038                 if [ -n "$(what_is_mounted_on "${dest}")" ]
1039                 then
1040                         if [ "${dest}" = "${rootmnt}" ]
1041                         then
1042                                 umount "${dest}"
1043                         else
1044                                 log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1045                                 continue
1046                         fi
1047                 fi
1048
1049                 if [ ! -d "${dest}" ]
1050                 then
1051                         # create the destination and delete existing files in
1052                         # its path that are in the way
1053                         path="/"
1054                         for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1055                         do
1056                                 path=$(trim_path ${path}/${dir})
1057                                 if [ -f ${path} ]
1058                                 then
1059                                         rm -f ${path}
1060                                 fi
1061                                 if [ ! -e ${path} ]
1062                                 then
1063                                         mkdir -p ${path}
1064                                         if echo ${path} | grep -qe "^${rootmnt}/*home/[^/]\+"
1065                                         then
1066                                                 # if ${dest} is in /home try fixing proper ownership by assuming that the intended user is the first, which is usually the case
1067                                                 # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
1068                                                 chown 1000:1000 ${path}
1069                                         fi
1070                                 fi
1071                         done
1072                 fi
1073
1074                 # if ${source} doesn't exist on our persistence media
1075                 # we bootstrap it with $dest from the live filesystem.
1076                 # this both makes sense and is critical if we're
1077                 # dealing with /etc or other system dir.
1078                 if [ ! -d "${source}" ]
1079                 then
1080                         if [ -n "${PERSISTENCE_READONLY}" ]
1081                         then
1082                                 continue
1083                         elif [ -n "${opt_union}" ] || [ -n "${opt_link}" ]
1084                         then
1085                                 # unions and don't need to be bootstrapped
1086                                 # link dirs can't be bootstrapped in a sensible way
1087                                 mkdir -p "${source}"
1088                                 chown_ref "${dest}" "${source}"
1089                                 chmod_ref "${dest}" "${source}"
1090                         elif [ -n "${opt_bind}" ]
1091                         then
1092                                 # ensure that $dest is not copied *into* $source
1093                                 mkdir -p "$(dirname ${source})"
1094                                 cp -a "${dest}" "${source}"
1095                         fi
1096                 fi
1097
1098                 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1099                 # ignore the loop below and set rofs_dest_backing=$dest
1100                 local rofs_dest_backing=""
1101                 if [ -n "${opt_link}"]
1102                 then
1103                         for d in /live/rofs/*
1104                         do
1105                                 if [ -n "${rootmnt}" ]
1106                                 then
1107                                         rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1108                                 else
1109                                         rofs_dest_backing="${d}/${dest}"
1110                                 fi
1111                                 if [ -d "${rofs_dest_backing}" ]
1112                                 then
1113                                         break
1114                                 else
1115                                         rofs_dest_backing=""
1116                                 fi
1117                         done
1118                 fi
1119
1120                 if [ -n "${opt_link}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1121                 then
1122                         link_files ${source} ${dest} ${rootmnt}
1123                 elif [ -n "${opt_link}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1124                 then
1125                         mkdir -p /live/persistence
1126                         local links_source=$(mktemp -d /live/persistence/links-source-XXXXXX)
1127                         chown_ref ${source} ${links_source}
1128                         chmod_ref ${source} ${links_source}
1129                         # We put the cow dir in the below strange place to
1130                         # make it absolutely certain that the link source
1131                         # has its own directory and isn't nested with some
1132                         # other custom mount (if so that mount's files would
1133                         # be linked, causing breakage.
1134                         local cow_dir="/live/overlay/live/persistence/$(basename ${links_source})"
1135                         mkdir -p ${cow_dir}
1136                         chown_ref "${source}" "${cow_dir}"
1137                         chmod_ref "${source}" "${cow_dir}"
1138                         do_union ${links_source} ${cow_dir} ${source} ${rofs_dest_backing}
1139                         link_files ${links_source} ${dest} ${rootmnt}
1140                 elif [ -n "${opt_union}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1141                 then
1142                         do_union ${dest} ${source} ${rofs_dest_backing}
1143                 elif [ -n "${opt_bind}" ] && [ -z "${PERSISTENCE_READONLY}" ]
1144                 then
1145                         mount --bind "${source}" "${dest}"
1146                 elif [ -n "${opt_bind}" -o -n "${opt_union}" ] && [ -n "${PERSISTENCE_READONLY}" ]
1147                 then
1148                         # bind-mount and union mount are handled the same
1149                         # in read-only mode, but note that rofs_dest_backing
1150                         # is non-empty (and necessary) only for unions
1151                         if [ -n "${rootmnt}" ]
1152                         then
1153                                 local cow_dir="$(echo ${dest} | sed -e "s|^${rootmnt}|/live/overlay/|")"
1154                         else
1155                                 # This is happens if persistence is activated
1156                                 # post boot
1157                                 local cow_dir="/live/overlay/${dest}"
1158                         fi
1159                         if [ -e "${cow_dir}" ] && [ -z "${opt_link}" ]
1160                         then
1161                                 # If an earlier custom mount has files here
1162                                 # it will "block" the current mount's files
1163                                 # which is undesirable
1164                                 rm -rf "${cow_dir}"
1165                         fi
1166                         mkdir -p ${cow_dir}
1167                         chown_ref "${source}" "${cow_dir}"
1168                         chmod_ref "${source}" "${cow_dir}"
1169                         do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1170                 fi
1171
1172                 PERSISTENCE_IS_ON="1"
1173                 export PERSISTENCE_IS_ON
1174
1175                 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1176                 then
1177                         used_devices="${used_devices} ${device}"
1178                 fi
1179         done < ${custom_mounts}
1180
1181         echo ${used_devices}
1182 }
1183
1184 fix_backwards_compatibility ()
1185 {
1186         local device=${1}
1187         local dir=${2}
1188         local opt=${3}
1189
1190         if [ -n "${PERSISTENCE_READONLY}" ]
1191         then
1192                 return
1193         fi
1194
1195         local backing="$(mount_persistence_media ${device})"
1196         if [ -z "${backing}" ]
1197         then
1198                 return
1199         fi
1200
1201         local include_list="${backing}/${persistence_list}"
1202         if [ ! -r "${include_list}" ]
1203         then
1204                 echo "# persistence backwards compatibility:
1205 ${dir} ${opt},source=." > "${include_list}"
1206         fi
1207 }
1208
1209 is_mountpoint ()
1210 {
1211         directory="$1"
1212
1213         [ $(stat -fc%d:%D "${directory}") != $(stat -fc%d:%D "${directory}/..") ]
1214 }