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