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