Make trim_path() handle "/", and arbitrarily many consecutive /:es.
[live-boot-grml.git] / scripts / live-helpers
1 # live-boot helper functions, used by live-boot on boot and by live-snapshot
2
3 if [ ! -x "/bin/fstype" ]
4 then
5         # klibc not in path -> not in initramfs
6         export PATH="${PATH}:/usr/lib/klibc/bin"
7 fi
8
9 # handle upgrade path from old udev (using udevinfo) to
10 # recent versions of udev (using udevadm info)
11 if [ -x /sbin/udevadm ]
12 then
13         udevinfo='/sbin/udevadm info'
14 else
15         udevinfo='udevinfo'
16 fi
17
18 root_overlay_label="full-ov"
19 old_root_overlay_label="live-rw"
20 old_home_overlay_label="home-rw"
21 custom_overlay_label="custom-ov"
22 root_snapshot_label="live-sn"
23 old_root_snapshot_label="live-sn"
24 home_snapshot_label="home-sn"
25 persistence_list="live.persist"
26
27 Arguments ()
28 {
29         PRESEEDS=""
30         LOCATIONS=""
31
32         for ARGUMENT in $(cat /proc/cmdline)
33         do
34                 case "${ARGUMENT}" in
35                         skipconfig)
36                                 NOACCESSIBILITY="Yes"
37                                 NOFASTBOOT="Yes"
38                                 NOFSTAB="Yes"
39                                 NONETWORKING="Yes"
40
41                                 export NOACCESSIBILITY NOFASTBOOT NOFSTAB NONETWORKING
42                                 ;;
43
44                         access=*)
45                                 ACCESS="${ARGUMENT#access=}"
46                                 export ACCESS
47                                 ;;
48
49                         console=*)
50                                 DEFCONSOLE="${ARGUMENT#*=}"
51                                 export DEFCONSOLE
52                                 ;;
53
54                         BOOTIF=*)
55                                 BOOTIF="${x#BOOTIF=}"
56                                 ;;
57
58                         debug)
59                                 DEBUG="Yes"
60                                 export DEBUG
61
62                                 set -x
63                                 ;;
64
65                         dhcp)
66                                 # Force dhcp even while netbooting
67                                 # Use for debugging in case somebody works on fixing dhclient
68                                 DHCP="Force";
69                                 export DHCP
70                                 ;;
71
72                         nodhcp)
73                                 unset DHCP
74                                 ;;
75
76                         ethdevice=*)
77                                 DEVICE="${ARGUMENT#ethdevice=}"
78                                 ETHDEVICE="${DEVICE}"
79                                 export DEVICE ETHDEVICE
80                                 ;;
81
82                         ethdevice-timeout=*)
83                                 ETHDEV_TIMEOUT="${ARGUMENT#ethdevice-timeout=}"
84                                 export ETHDEV_TIMEOUT
85                                 ;;
86
87                         fetch=*)
88                                 FETCH="${ARGUMENT#fetch=}"
89                                 export FETCH
90                                 ;;
91
92                         forcepersistentfsck)
93                                 FORCEPERSISTENTFSCK="Yes"
94                                 export FORCEPERSISTENTFSCK
95                                 ;;
96
97                         ftpfs=*)
98                                 FTPFS="${ARGUMENT#ftpfs=}"
99                                 export FTPFS
100                                 ;;
101
102                         httpfs=*)
103                                 HTTPFS="${ARGUMENT#httpfs=}"
104                                 export HTTPFS
105                                 ;;
106
107                         iscsi=*)
108                                 ISCSI="${ARGUMENT#iscsi=}"
109                                 #ip:port - separated by ;
110                                 ISCSI_PORTAL="${ISCSI%;*}"
111                                 if echo "${ISCSI_PORTAL}" | grep -q , ; then
112                                         ISCSI_SERVER="${ISCSI_PORTAL%,*}"
113                                         ISCSI_PORT="${ISCSI_PORTAL#*,}"
114                                 fi
115                                 #target name
116                                 ISCSI_TARGET="${ISCSI#*;}"
117                                 export ISCSI ISCSI_PORTAL ISCSI_TARGET ISCSI_SERVER ISCSI_PORT
118                                 ;;
119
120                         isofrom=*|fromiso=*)
121                                 FROMISO="${ARGUMENT#*=}"
122                                 export FROMISO
123                                 ;;
124
125                         ignore_uuid)
126                                 IGNORE_UUID="Yes"
127                                 export IGNORE_UUID
128                                 ;;
129
130                         integrity-check)
131                                 INTEGRITY_CHECK="Yes"
132                                 export INTEGRITY_CHECK
133                                 ;;
134
135                         ip=*)
136                                 STATICIP="${ARGUMENT#ip=}"
137
138                                 if [ -z "${STATICIP}" ]
139                                 then
140                                         STATICIP="frommedia"
141                                 fi
142
143                                 export STATICIP
144                                 ;;
145
146                         live-getty)
147                                 LIVE_GETTY="1"
148                                 export LIVE_GETTY
149                                 ;;
150
151                         live-media=*|bootfrom=*)
152                                 LIVE_MEDIA="${ARGUMENT#*=}"
153                                 export LIVE_MEDIA
154                                 ;;
155
156                         live-media-encryption=*|encryption=*)
157                                 LIVE_MEDIA_ENCRYPTION="${ARGUMENT#*=}"
158                                 export LIVE_MEDIA_ENCRYPTION
159                                 ;;
160
161                         live-media-offset=*)
162                                 LIVE_MEDIA_OFFSET="${ARGUMENT#live-media-offset=}"
163                                 export LIVE_MEDIA_OFFSET
164                                 ;;
165
166                         live-media-path=*)
167                                 LIVE_MEDIA_PATH="${ARGUMENT#live-media-path=}"
168                                 export LIVE_MEDIA_PATH
169                                 ;;
170
171                         live-media-timeout=*)
172                                 LIVE_MEDIA_TIMEOUT="${ARGUMENT#live-media-timeout=}"
173                                 export LIVE_MEDIA_TIMEOUT
174                                 ;;
175
176                         module=*)
177                                 MODULE="${ARGUMENT#module=}"
178                                 export MODULE
179                                 ;;
180
181                         netboot=*)
182                                 NETBOOT="${ARGUMENT#netboot=}"
183                                 export NETBOOT
184                                 ;;
185
186                         nfsopts=*)
187                                 NFSOPTS="${ARGUMENT#nfsopts=}"
188                                 export NFSOPTS
189                                 ;;
190
191                         nfscow=*)
192                                 NFS_COW="${ARGUMENT#nfscow=}"
193                                 export NFS_COW
194                                 ;;
195
196                         noaccessibility)
197                                 NOACCESSIBILITY="Yes"
198                                 export NOACCESSIBILITY
199                                 ;;
200
201                         nofastboot)
202                                 NOFASTBOOT="Yes"
203                                 export NOFASTBOOT
204                                 ;;
205
206                         nofstab)
207                                 NOFSTAB="Yes"
208                                 export NOFSTAB
209                                 ;;
210
211                         nonetworking)
212                                 NONETWORKING="Yes"
213                                 export NONETWORKING
214                                 ;;
215
216                         ramdisk-size=*)
217                                 ramdisk_size="${ARGUMENT#ramdisk-size=}"
218                                 ;;
219
220                         swapon)
221                                 SWAPON="Yes"
222                                 export SWAPON
223                                 ;;
224
225                         persistent)
226                                 PERSISTENT="Yes"
227                                 export PERSISTENT
228                                 ;;
229
230                         persistent-encryption=*)
231                                 PERSISTENT_ENCRYPTION="${ARGUMENT#*=}"
232                                 export PERSISTENT_ENCRYPTION
233                                 ;;
234
235                         persistent-media=*)
236                                 PERSISTENT_MEDIA="${ARGUMENT#*=}"
237                                 export PERSISTENT_MEDIA
238                                 ;;
239                         persistent-method=*)
240                                 PERSISTENT_METHOD="${ARGUMENT#*=}"
241                                 export PERSISTENT_METHOD
242                                 ;;
243
244                         persistent-path=*)
245                                 PERSISTENT_PATH="${ARGUMENT#persistent-path=}"
246                                 export PERSISTENT_PATH
247                                 ;;
248                         persistent-read-only)
249                                 PERSISTENT_READONLY="Yes"
250                                 export PERSISTENT_READONLY
251                                 ;;
252
253                         persistent-storage=*)
254                                 PERSISTENT_STORAGE="${ARGUMENT#persistent-storage=}"
255                                 export PERSISTENT_STORAGE
256                                 ;;
257
258                         persistent-subtext=*)
259                                 root_overlay_label="${root_overlay_label}-${ARGUMENT#persistent-subtext=}"
260                                 old_root_overlay_label="${old_root_overlay_label}-${ARGUMENT#persistent-subtext=}"
261                                 old_home_overlay_label="${old_home_overlay_label}-${ARGUMENT#persistent-subtext=}"
262                                 custom_overlay_label="${custom_overlay_label}-${ARGUMENT#persistent-subtext=}"
263                                 root_snapshot_label="${root_snapshot_label}-${ARGUMENT#persistent-subtext=}"
264                                 old_root_snapshot_label="${root_snapshot_label}-${ARGUMENT#persistent-subtext=}"
265                                 home_snapshot_label="${home_snapshot_label}-${ARGUMENT#persistent-subtext=}"
266                                 ;;
267
268                         nopersistent)
269                                 NOPERSISTENT="Yes"
270                                 export NOPERSISTENT
271                                 ;;
272
273                         noprompt)
274                                 NOPROMPT="Yes"
275                                 export NOPROMPT
276                                 ;;
277
278                         noprompt=*)
279                                 NOPROMPT="${ARGUMENT#noprompt=}"
280                                 export NOPROMPT
281                                 ;;
282
283                         quickusbmodules)
284                                 QUICKUSBMODULES="Yes"
285                                 export QUICKUSBMODULES
286                                 ;;
287
288                         preseed/file=*|file=*)
289                                 LOCATIONS="${ARGUMENT#*=} ${LOCATIONS}"
290                                 export LOCATIONS
291                                 ;;
292
293                         nopreseed)
294                                 NOPRESEED="Yes"
295                                 export NOPRESEED
296                                 ;;
297
298                         */*=*)
299                                 question="${ARGUMENT%%=*}"
300                                 value="${ARGUMENT#*=}"
301                                 PRESEEDS="${PRESEEDS}\"${question}=${value}\" "
302                                 export PRESEEDS
303                                 ;;
304
305                         showmounts)
306                                 SHOWMOUNTS="Yes"
307                                 export SHOWMOUNTS
308                                 ;;
309
310                         silent)
311                                 SILENT="Yes"
312                                 export SILENT
313                                 ;;
314
315                         todisk=*)
316                                 TODISK="${ARGUMENT#todisk=}"
317                                 export TODISK
318                                 ;;
319
320                         toram)
321                                 TORAM="Yes"
322                                 export TORAM
323                                 ;;
324
325                         toram=*)
326                                 TORAM="Yes"
327                                 MODULETORAM="${ARGUMENT#toram=}"
328                                 export TORAM MODULETORAM
329                                 ;;
330
331                         exposedroot)
332                                 EXPOSED_ROOT="Yes"
333                                 export EXPOSED_ROOT
334                                 ;;
335
336                         plainroot)
337                                 PLAIN_ROOT="Yes"
338                                 export PLAIN_ROOT
339                                 ;;
340
341                         skipunion)
342                                 SKIP_UNION_MOUNTS="Yes"
343                                 export SKIP_UNION_MOUNTS
344                                 ;;
345
346                         root=*)
347                                 ROOT="${ARGUMENT#root=}"
348                                 export ROOT
349                                 ;;
350
351                         union=*)
352                                 UNIONTYPE="${ARGUMENT#union=}"
353                                 export UNIONTYPE
354                                 ;;
355                 esac
356         done
357
358         # sort of compatibility with netboot.h from linux docs
359         if [ -z "${NETBOOT}" ]
360         then
361                 if [ "${ROOT}" = "/dev/nfs" ]
362                 then
363                         NETBOOT="nfs"
364                         export NETBOOT
365                 elif [ "${ROOT}" = "/dev/cifs" ]
366                 then
367                         NETBOOT="cifs"
368                         export NETBOOT
369                 fi
370         fi
371
372         if [ -z "${MODULE}" ]
373         then
374                 MODULE="filesystem"
375                 export MODULE
376         fi
377
378         if [ -z "${UNIONTYPE}" ]
379         then
380                 UNIONTYPE="aufs"
381                 export UNIONTYPE
382         fi
383
384         if [ -z "${PERSISTENT_ENCRYPTION}" ]
385         then
386                 PERSISTENT_ENCRYPTION="none"
387                 export PERSISTENT_ENCRYPTION
388         elif echo ${PERSISTENT_ENCRYPTION} | grep -qe "\<luks\>"
389         then
390                 if ! modprobe dm-crypt
391                 then
392                         log_warning_msg "Unable to load module dm-crypt"
393                         PERSISTENT_ENCRYPTION=$(echo ${PERSISTENT_ENCRYPTION} | sed -e 's/\<luks,\|,\?luks$//g')
394                         export PERSISTENT_ENCRYPTION
395                 fi
396
397                 if [ ! -x /lib/cryptsetup/askpass ] || [ ! -x /sbin/cryptsetup ]
398                 then
399                         log_warning_msg "cryptsetup in unavailable"
400                         PERSISTENT_ENCRYPTION=$(echo ${PERSISTENT_ENCRYPTION} | sed -e 's/\<luks,\|,\?luks$//g')
401                         export PERSISTENT_ENCRYPTION
402                 fi
403         fi
404
405         if [ -z "${PERSISTENT_METHOD}" ]
406         then
407                 PERSISTENT_METHOD="snapshot,overlay"
408                 export PERSISTENT_METHOD
409         fi
410
411         if [ -z "${PERSISTENT_STORAGE}" ]
412         then
413                 PERSISTENT_STORAGE="filesystem,file"
414                 export PERSISTENT_STORAGE
415         fi
416 }
417
418 sys2dev ()
419 {
420         sysdev=${1#/sys}
421         echo "/dev/$($udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
422 }
423
424 subdevices ()
425 {
426         sysblock=${1}
427         r=""
428
429         for dev in "${sysblock}"/* "${sysblock}"
430         do
431                 if [ -e "${dev}/dev" ]
432                 then
433                         r="${r} ${dev}"
434                 fi
435         done
436
437         echo ${r}
438 }
439
440 storage_devices()
441 {
442         black_listed_devices="${1}"
443         white_listed_devices="${2}"
444
445         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
446         do
447                 fulldevname=$(sys2dev "${sysblock}")
448
449                 if echo "${black_listed_devices}" | grep -qe "\<${fulldevname}\>" || \
450                         [ -n "${white_listed_devices}" ] && \
451                         echo "${white_listed_devices}" | grep -qve "\<${fulldevname}\>"
452                 then
453                         # skip this device entirely
454                         continue
455                 fi
456
457                 for dev in $(subdevices "${sysblock}")
458                 do
459                         devname=$(sys2dev "${dev}")
460
461                         if echo "${black_listed_devices}" | grep -qe "\<${devname}\>"
462                         then
463                                 # skip this subdevice
464                                 continue
465                         else
466                                 echo "${devname}"
467                         fi
468                 done
469         done
470 }
471
472 is_supported_fs ()
473 {
474         fstype="${1}"
475
476         # Validate input first
477         if [ -z "${fstype}" ]
478         then
479                 return 1
480         fi
481
482         # Try to look if it is already supported by the kernel
483         if grep -q ${fstype} /proc/filesystems
484         then
485                 return 0
486         else
487                 # Then try to add support for it the gentle way using the initramfs capabilities
488                 modprobe ${fstype}
489                 if grep -q ${fstype} /proc/filesystems
490                 then
491                         return 0
492                 # Then try the hard way if /root is already reachable
493                 else
494                         kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
495                         if [ -e "${kmodule}" ]
496                         then
497                                 insmod "${kmodule}"
498                                 if grep -q ${fstype} /proc/filesystems
499                                 then
500                                         return 0
501                                 fi
502                         fi
503                 fi
504         fi
505
506         return 1
507 }
508
509 get_fstype ()
510 {
511         /sbin/blkid -s TYPE -o value $1 2>/dev/null
512 }
513
514 where_is_mounted ()
515 {
516         device=${1}
517         # return first found
518         grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
519 }
520
521 trim_path () {
522     # remove all unnecessary /:s in the path, including last one (except
523     # if path is just "/")
524     echo ${1} | sed 's|//\+|/|g' | sed 's|^\(.*[^/]\)/$|\1|'
525 }
526
527 what_is_mounted_on ()
528 {
529         local dir="$(trim_path ${1})"
530         grep -m1 "^[^ ]\+ ${dir} " /proc/mounts | cut -d' ' -f1
531 }
532
533 lastline ()
534 {
535         while read lines
536         do
537                 line=${lines}
538         done
539
540         echo "${line}"
541 }
542
543 base_path ()
544 {
545         testpath="${1}"
546         mounts="$(awk '{print $2}' /proc/mounts)"
547         testpath="$(busybox realpath ${testpath})"
548
549         while true
550         do
551                 if echo "${mounts}" | grep -qs "^${testpath}"
552                 then
553                         set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
554                         echo ${1}
555                         break
556                 else
557                         testpath=$(dirname $testpath)
558                 fi
559         done
560 }
561
562 fs_size ()
563 {
564         # Returns used/free fs kbytes + 5% more
565         # You could pass a block device as ${1} or the mount point as ${2}
566
567         dev="${1}"
568         mountp="${2}"
569         used="${3}"
570
571         if [ -z "${mountp}" ]
572         then
573                 mountp="$(where_is_mounted ${dev})"
574
575                 if [ -z "${mountp}" ]
576                 then
577                         mountp="/mnt/tmp_fs_size"
578
579                         mkdir -p "${mountp}"
580                         mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
581
582                         doumount=1
583                 fi
584         fi
585
586         if [ "${used}" = "used" ]
587         then
588                 size=$(du -ks ${mountp} | cut -f1)
589                 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
590         else
591                 # free space
592                 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
593         fi
594
595         if [ -n "${doumount}" ]
596         then
597                 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
598                 rmdir "${mountp}"
599         fi
600
601         echo "${size}"
602 }
603
604 load_keymap ()
605 {
606         # Load custom keymap
607         if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
608         then
609                 loadkeys /etc/boottime.kmap.gz
610         fi
611 }
612
613 setup_loop ()
614 {
615         local fspath=${1}
616         local module=${2}
617         local pattern=${3}
618         local offset=${4}
619         local encryption=${5}
620         local readonly=${6}
621
622         # the output of setup_loop is evaluated in other functions,
623         # modprobe leaks kernel options like "libata.dma=0"
624         # as "options libata dma=0" on stdout, causing serious
625         # problems therefor, so instead always avoid output to stdout
626         modprobe -q -b "${module}" 1>/dev/null
627
628         udevadm settle
629
630         for loopdev in ${pattern}
631         do
632                 if [ "$(cat ${loopdev}/size)" -eq 0 ]
633                 then
634                         dev=$(sys2dev "${loopdev}")
635                         options=''
636
637                         if [ -n "${readonly}" ]
638                         then
639                                 if losetup --help 2>&1 | grep -q -- "-r\b"
640                                 then
641                                         options="${options} -r"
642                                 fi
643                         fi
644
645                         if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
646                         then
647                                 options="${options} -o ${offset}"
648                         fi
649
650                         if [ -z "${encryption}" ]
651                         then
652                                 losetup ${options} "${dev}" "${fspath}"
653                         else
654                                 # Loop AES encryption
655                                 while true
656                                 do
657                                         load_keymap
658
659                                         echo -n "Enter passphrase for root filesystem: " >&6
660                                         read -s passphrase
661                                         echo "${passphrase}" > /tmp/passphrase
662                                         unset passphrase
663                                         exec 9</tmp/passphrase
664                                         /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
665                                         error=${?}
666                                         exec 9<&-
667                                         rm -f /tmp/passphrase
668
669                                         if [ 0 -eq ${error} ]
670                                         then
671                                                 unset error
672                                                 break
673                                         fi
674
675                                         echo
676                                         echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
677                                         read answer
678
679                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
680                                         then
681                                                 unset answer
682                                                 break
683                                         fi
684                                 done
685                         fi
686
687                         echo "${dev}"
688                         return 0
689                 fi
690         done
691
692         panic "No loop devices available"
693 }
694
695 try_mount ()
696 {
697         dev="${1}"
698         mountp="${2}"
699         opts="${3}"
700         fstype="${4}"
701
702         old_mountp="$(where_is_mounted ${dev})"
703
704         if [ -n "${old_mountp}" ]
705         then
706                 if [ "${opts}" != "ro" ]
707                 then
708                         mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
709                 fi
710
711                 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
712         else
713                 if [ -z "${fstype}" ]
714                 then
715                         fstype=$(get_fstype "${dev}")
716                 fi
717                 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
718                 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > live-boot.log && return 0 )
719         fi
720 }
721
722 mount_persistent_media ()
723 {
724         local device=${1}
725         local backing=""
726
727         # We can't mount into ${rootmnt}/live before ${rootmnt} has been
728         # mounted since that would cover our mountpoint.
729         if [ -n "${rootmnt}" ] && [ -z "$(what_is_mounted_on ${rootmnt})" ]
730         then
731                 backing="/$(basename ${device})-backing"
732         else
733                 backing="${rootmnt}/live/persistent/$(basename ${device})"
734         fi
735
736         mkdir -p "${backing}"
737         local old_backing="$(where_is_mounted ${device})"
738         if [ -z "${old_backing}" ]
739         then
740                 local fstype="$(get_fstype ${device})"
741                 local mount_opts="rw,noatime"
742                 if [ -n "${PERSISTENT_READONLY}" ]
743                 then
744                         mount_opts="ro,noatime"
745                 fi
746                 if mount -t "${fstype}" -o "${mount_opts}" "${device}" "${backing}" >/dev/null
747                 then
748                         echo ${backing}
749                         return 0
750                 else
751                         log_warning_msg "Failed to mount persistent media ${device}"
752                         return 1
753                 fi
754         elif [ "${backing}" != "${old_backing}" ]
755         then
756                 if mount --move ${old_backing} ${backing} >/dev/null
757                 then
758                         echo ${backing}
759                         return 0
760                 else
761                         log_warning_msg "Failed to move persistent media ${device}"
762                         return 1
763                 fi
764         fi
765         return 0
766 }
767
768 close_persistent_media () {
769         local device=${1}
770         local backing="$(where_is_mounted ${device})"
771
772         if [ -d "${backing}" ]
773         then
774                 umount "${backing}" >/dev/null 2>&1
775                 rmdir "${backing}" >/dev/null 2>&1
776         fi
777
778         if is_active_luks_mapping ${device}
779         then
780                 /sbin/cryptsetup luksClose ${device}
781         fi
782 }
783
784 open_luks_device ()
785 {
786         dev="${1}"
787         name="$(basename ${dev})"
788         opts="--key-file=-"
789         if [ -n "${PERSISTENT_READONLY}" ]
790         then
791                 opts="${opts} --readonly"
792         fi
793
794         if /sbin/cryptsetup status "${name}" >/dev/null 2>&1
795         then
796                 re="^[[:space:]]*device:[[:space:]]*\([^[:space:]]*\)$"
797                 opened_dev=$(cryptsetup status ${name} 2>/dev/null | grep "${re}" | sed "s|${re}|\1|")
798                 if [ "${opened_dev}" = "${dev}" ]
799                 then
800                         luks_device="/dev/mapper/${name}"
801                         echo ${luks_device}
802                         return 0
803                 else
804                         log_warning_msg "Cannot open luks device ${dev} since ${opened_dev} already is opened with its name"
805                         return 1
806                 fi
807         fi
808
809         load_keymap
810
811         while true
812         do
813                 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
814                         /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
815
816                 if [ 0 -eq ${?} ]
817                 then
818                         luks_device="/dev/mapper/${name}"
819                         echo ${luks_device}
820                         return 0
821                 fi
822
823                 echo >&6
824                 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
825                 read answer
826
827                 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
828                 then
829                         return 2
830                 fi
831         done
832 }
833
834 get_gpt_name ()
835 {
836     local dev="${1}"
837     /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
838 }
839
840 is_gpt_device ()
841 {
842     local dev="${1}"
843     [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
844 }
845
846 probe_for_gpt_name ()
847 {
848         local overlays="${1}"
849         local snapshots="${2}"
850         local dev="${3}"
851
852         local gpt_dev="${dev}"
853         if is_active_luks_mapping ${dev}
854         then
855                 # if $dev is an opened luks device, we need to check
856                 # GPT stuff on the backing device
857                 gpt_dev=$(get_luks_backing_device "${dev}")
858         fi
859
860         if ! is_gpt_device ${gpt_dev}
861         then
862                 return
863         fi
864
865         local gpt_name=$(get_gpt_name ${gpt_dev})
866         for label in ${overlays} ${snapshots}
867         do
868                 if [ "${gpt_name}" = "${label}" ]
869                 then
870                         echo "${label}=${dev}"
871                 fi
872         done
873 }
874
875 probe_for_fs_label ()
876 {
877         local overlays="${1}"
878         local snapshots="${2}"
879         local dev="${3}"
880
881         for label in ${overlays} ${snapshots}
882         do
883                 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
884                 then
885                         echo "${label}=${dev}"
886                 fi
887         done
888 }
889
890 probe_for_file_name ()
891 {
892         local overlays="${1}"
893         local snapshots="${2}"
894         local dev="${3}"
895
896         local ret=""
897         local backing="$(mount_persistent_media ${dev})"
898         if [ -z "${backing}" ]
899         then
900             return
901         fi
902
903         for label in ${overlays}
904         do
905                 path=${backing}/${PERSISTENT_PATH}${label}
906                 if [ -f "${path}" ]
907                 then
908                         local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
909                         ret="${ret} ${label}=${loopdev}"
910                 fi
911         done
912         for label in ${snapshots}
913         do
914                 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
915                 do
916                         path="${PERSISTENT_PATH}${label}.${ext}"
917                         if [ -f "${backing}/${path}" ]
918                         then
919                                 ret="${ret} ${label}=${dev}:${backing}:${path}"
920                         fi
921                 done
922         done
923
924         if [ -n "${ret}" ]
925         then
926                 echo ${ret}
927         else
928                 umount ${backing} > /dev/null 2>&1 || true
929         fi
930 }
931
932 find_persistent_media ()
933 {
934         # Scans devices for overlays and snapshots, and returns a whitespace
935         # separated list of how to use them. Only overlays with a partition
936         # label or file name in ${overlays} are returned, and ditto for
937         # snapshots with labels in ${snapshots}.
938         #
939         # When scanning a LUKS device, the user will be asked to enter the
940         # passphrase; on failure to enter it, or if no persistent partitions
941         # or files were found, the LUKS device is closed.
942         #
943         # For a snapshot file the return value is ${label}=${snapdata}", where
944         # ${snapdata} is the parameter used for try_snap().
945         #
946         # For all other cases (overlay/snapshot partition and overlay file) the
947         # return value is "${label}=${device}", where ${device} a device that
948         # can mount the content. In the case of an overlay file, the device
949         # containing the file will remain mounted as a side-effect.
950         #
951         # No devices in ${black_listed_devices} will be scanned, and if
952         # ${white_list_devices} is non-empty, only devices in it will be
953         # scanned.
954
955         local overlays="${1}"
956         local snapshots="${2}"
957         local white_listed_devices="${3}"
958         local ret=""
959
960         for dev in $(storage_devices "" "${white_listed_devices}")
961         do
962                 local result=""
963
964                 local luks_device=""
965                 # Check if it's a luks device; we'll have to open the device
966                 # in order to probe any filesystem it contains, like we do
967                 # below. activate_custom_mounts() also depends on that any luks
968                 # device already has been opened.
969                 if echo ${PERSISTENT_ENCRYPTION} | grep -qe "\<luks\>" && \
970                    is_luks_partition ${dev}
971                 then
972                         if luks_device=$(open_luks_device "${dev}")
973                         then
974                                 dev="${luks_device}"
975                         else
976                                 # skip $dev since we failed/chose not to open it
977                                 continue
978                         fi
979                 elif echo ${PERSISTENT_ENCRYPTION} | grep -qve "\<none\>"
980                 then
981                         # skip $dev since we don't allow unencrypted storage
982                         continue
983                 fi
984
985                 # Probe for matching GPT partition names or filesystem labels
986                 if echo ${PERSISTENT_STORAGE} | grep -qe "\<filesystem\>"
987                 then
988                         result=$(probe_for_gpt_name "${overlays}" "${snapshots}" ${dev})
989                         if [ -n "${result}" ]
990                         then
991                                 ret="${ret} ${result}"
992                                 continue
993                         fi
994
995                         result=$(probe_for_fs_label "${overlays}" "${snapshots}" ${dev})
996                         if [ -n "${result}" ]
997                         then
998                                 ret="${ret} ${result}"
999                                 continue
1000                         fi
1001                 fi
1002
1003                 # Probe for files with matching name on mounted partition
1004                 if echo ${PERSISTENT_STORAGE} | grep -qe "\<file\>"
1005                 then
1006                         result=$(probe_for_file_name "${overlays}" "${snapshots}" ${dev})
1007                         if [ -n "${result}" ]
1008                         then
1009                                 ret="${ret} ${result}"
1010                                 continue
1011                         fi
1012                 fi
1013
1014                 # Close luks device if it isn't used
1015                 if [ -z "${result}" ] && [ -n "${luks_device}" ] && \
1016                    is_active_luks_mapping "${luks_device}"
1017                 then
1018                         /sbin/cryptsetup luksClose "${luks_device}"
1019                 fi
1020         done
1021
1022         if [ -n "${ret}" ]
1023         then
1024                 echo ${ret}
1025         fi
1026 }
1027
1028 get_mac ()
1029 {
1030         mac=""
1031
1032         for adaptor in /sys/class/net/*
1033         do
1034                 status="$(cat ${adaptor}/iflink)"
1035
1036                 if [ "${status}" -eq 2 ]
1037                 then
1038                         mac="$(cat ${adaptor}/address)"
1039                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
1040                 fi
1041         done
1042
1043         echo ${mac}
1044 }
1045
1046 is_luks_partition ()
1047 {
1048         device="${1}"
1049         /sbin/cryptsetup isLuks "${device}" 1>/dev/null 2>&1
1050 }
1051
1052 is_active_luks_mapping ()
1053 {
1054         device="${1}"
1055         /sbin/cryptsetup status "${device}" 1>/dev/null 2>&1
1056 }
1057
1058 get_luks_backing_device () {
1059         device=${1}
1060         cryptsetup status ${device} 2> /dev/null | \
1061                 awk '{if ($1 == "device:") print $2}'
1062 }
1063
1064 removable_dev ()
1065 {
1066         output_format="${1}"
1067         want_usb="${2}"
1068         ret=
1069
1070         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1071         do
1072                 dev_ok=
1073                 if [ "$(cat ${sysblock}/removable)" = "1" ]
1074                 then
1075                         if [ -z "${want_usb}" ]
1076                         then
1077                                 dev_ok="yes"
1078                         else
1079                                 if readlink ${sysblock} | grep -q usb
1080                                 then
1081                                         dev_ok="yes"
1082                                 fi
1083                         fi
1084                 fi
1085
1086                 if [ "${dev_ok}" = "yes" ]
1087                 then
1088                         case "${output_format}" in
1089                                 sys)
1090                                         ret="${ret} ${sysblock}"
1091                                         ;;
1092                                 *)
1093                                         devname=$(sys2dev "${sysblock}")
1094                                         ret="${ret} ${devname}"
1095                                         ;;
1096                         esac
1097                 fi
1098         done
1099
1100         echo "${ret}"
1101 }
1102
1103 removable_usb_dev ()
1104 {
1105         output_format="${1}"
1106
1107         removable_dev "${output_format}" "want_usb"
1108 }
1109
1110 non_removable_dev ()
1111 {
1112         output_format="${1}"
1113         ret=
1114
1115         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1116         do
1117                 if [ "$(cat ${sysblock}/removable)" = "0" ]
1118                 then
1119                         case "${output_format}" in
1120                                 sys)
1121                                         ret="${ret} ${sysblock}"
1122                                         ;;
1123                                 *)
1124                                         devname=$(sys2dev "${sysblock}")
1125                                         ret="${ret} ${devname}"
1126                                         ;;
1127                         esac
1128                 fi
1129         done
1130
1131         echo "${ret}"
1132 }
1133
1134 link_files ()
1135 {
1136         # create source's directory structure in dest, and recursively
1137         # create symlinks in dest to to all files in source. if mask
1138         # is non-empty, remove mask from all source paths when
1139         # creating links (will be necessary if we change root, which
1140         # live-boot normally does (into $rootmnt)).
1141
1142         # remove multiple /:s and ensure ending on /
1143         local src_dir="$(echo "${1}"/ | sed -e 's|/\+|/|g')"
1144         local dest_dir="$(echo "${2}"/ | sed -e 's|/\+|/|g')"
1145         local src_mask="${3}"
1146
1147         # This check can only trigger on the inital, non-recursive call since
1148         # we create the destination before recursive calls
1149         if [ ! -d "${dest_dir}" ]
1150         then
1151                 log_warning_msg "Must link_files into a directory"
1152                 return
1153         fi
1154
1155         find "${src_dir}" -mindepth 1 -maxdepth 1 | while read src; do
1156                 local dest="${dest_dir}$(basename "${src}")"
1157                 if [ -d "${src}" ]
1158                 then
1159                         if [ -z "$(ls -A "${src}")" ]
1160                         then
1161                                 continue
1162                         fi
1163                         if [ ! -d "${dest}" ]
1164                         then
1165                                 mkdir -p "${dest}"
1166                                 prev="$(dirname "${dest}")"
1167                                 chown --reference "${prev}" "${dest}"
1168                                 chmod --reference "${prev}" "${dest}"
1169                         fi
1170                         link_files "${src}" "${dest}" "${src_mask}"
1171                 else
1172                         if [ -n "${src_mask}" ]
1173                         then
1174                                 src="$(echo ${src} | sed "s|^${src_mask}||")"
1175                         fi
1176                         rm -rf "${dest}" 2> /dev/null
1177                         ln -s "${src}" "${dest}"
1178                 fi
1179         done
1180 }
1181
1182 do_union ()
1183 {
1184         local unionmountpoint="${1}"    # directory where the union is mounted
1185         local unionrw="${2}"            # branch where the union changes are stored
1186         local unionro1="${3}"           # first underlying read-only branch (optional)
1187         local unionro2="${4}"           # second underlying read-only branch (optional)
1188
1189         if [ "${UNIONTYPE}" = "aufs" ]
1190         then
1191                 rw_opt="rw"
1192                 ro_opt="rr+wh"
1193                 noxino_opt="noxino"
1194         elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1195         then
1196                 rw_opt="RW"
1197                 ro_opt="RO"
1198         else
1199                 rw_opt="rw"
1200                 ro_opt="ro"
1201         fi
1202
1203         case "${UNIONTYPE}" in
1204                 unionfs-fuse)
1205                         unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1206                         unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1207                         if [ -n "${unionro1}" ]
1208                         then
1209                                 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1210                         fi
1211                         if [ -n "${unionro2}" ]
1212                         then
1213                                 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1214                         fi
1215                         ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1216                         unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1217                         ( mkdir -p /run/sendsigs.omit.d
1218                         pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1219                         ;;
1220
1221                 overlayfs)
1222                         # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1223                         # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1224                         unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1225                         mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1226                         ;;
1227
1228                 *)
1229                         unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1230                         if [ -n "${unionro1}" ]
1231                         then
1232                                 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1233                         fi
1234                         if [ -n "${unionro2}" ]
1235                         then
1236                                 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1237                         fi
1238                         mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1239                         ;;
1240         esac
1241 }
1242
1243 get_custom_mounts ()
1244 {
1245         # Side-effect: leaves $devices with live.persist mounted in ${rootmnt}/live/persistent
1246         # Side-effect: prints info to file $custom_mounts
1247
1248         local custom_mounts=${1}
1249         shift
1250         local devices=${@}
1251
1252         local bindings="/tmp/bindings.list"
1253         local links="/tmp/links.list"
1254         rm -rf ${bindings} ${links} 2> /dev/null
1255
1256         for device in ${devices}
1257         do
1258                 if [ ! -b "${device}" ]
1259                 then
1260                         continue
1261                 fi
1262
1263                 local device_name="$(basename ${device})"
1264                 local backing=$(mount_persistent_media ${device})
1265                 if [ -z "${backing}" ]
1266                 then
1267                         continue
1268                 fi
1269
1270                 local include_list="${backing}/${persistence_list}"
1271                 if [ ! -r "${include_list}" ]
1272                 then
1273                         continue
1274                 fi
1275
1276                 if [ -n "${DEBUG}" ] && [ -e "${include_list}" ]
1277                 then
1278                         cp ${include_list} ${rootmnt}/live/persistent/${persistence_list}.${device_name}
1279                 fi
1280
1281                 while read dir options # < ${include_list}
1282                 do
1283                         if echo ${dir} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1284                         then
1285                                 # skipping empty or commented lines
1286                                 continue
1287                         fi
1288
1289                         if trim_path ${dir} | grep -q -e "^[^/]" -e "^/$" -e "^/live\(/.*\)\?$" -e "^/\(.*/\)\?\.\.\?\(/.*\)\?$"
1290                         then
1291                                 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) or \"/\" (for the latter, use ${root_overlay_label}-type persistence)"
1292                                 continue
1293                         fi
1294
1295                         local opt_source=""
1296                         local opt_linkfiles=""
1297                         for opt in $(echo ${options} | tr ',' ' ');
1298                         do
1299                                 case "${opt}" in
1300                                         source=*)
1301                                                 opt_source=${opt#source=}
1302                                                 ;;
1303                                         linkfiles)
1304                                                 opt_linkfiles="yes"
1305                                                 ;;
1306                                         union)
1307                                                 ;;
1308                                         *)
1309                                                 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1310                                                 continue 2
1311                                                 ;;
1312                                 esac
1313                         done
1314
1315                         local source="${dir}"
1316                         if [ -n "${opt_source}" ]
1317                         then
1318                                 if echo ${opt_source} | grep -q -e "^/" -e "^\(.*/\)\?\.\.\?\(/.*\)\?$" && [ "${source}" != "." ]
1319                                 then
1320                                         log_warning_msg "Skipping unsafe custom mount with option source=${opt_source}: must be either \".\" (the media root) a relative path w.r.t. the media root that doesn't contain comas or the special dirs \".\" and \"..\""
1321                                         continue
1322                                 else
1323                                         source="${opt_source}"
1324                                 fi
1325                         fi
1326
1327                         local full_source="$(trim_path ${backing}/${source})"
1328                         local full_dest="$(trim_path ${rootmnt}/${dir})"
1329                         if [ -n "${opt_linkfiles}" ]
1330                         then
1331                                 echo "${device} ${full_source} ${full_dest} ${options}" >> ${links}
1332                         else
1333                                 echo "${device} ${full_source} ${full_dest} ${options}" >> ${bindings}
1334                         fi
1335                 done < ${include_list}
1336         done
1337
1338         # We sort the list according to destination so we're sure that
1339         # we won't hide a previous mount. We also ignore duplicate
1340         # destinations in a more or less arbitrary way.
1341         [ -e "${bindings}" ] && sort -k3 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1342
1343         # After all mounts are considered we add symlinks so they
1344         # won't be hidden by some mount.
1345         [ -e "${links}" ] && sort -k3 -sbu ${links} >> ${custom_mounts} && rm ${links}
1346 }
1347
1348 activate_custom_mounts ()
1349 {
1350         local custom_mounts="${1}" # the ouput from get_custom_mounts()
1351         local used_devices=""
1352
1353         while read device source dest options # < ${custom_mounts}
1354         do
1355                 local opt_linkfiles=""
1356                 local opt_union=""
1357                 for opt in $(echo ${options} | tr ',' ' ');
1358                 do
1359                          case "${opt}" in
1360                                 linkfiles)
1361                                         opt_linkfiles="yes"
1362                                         ;;
1363                                 union)
1364                                         opt_union="yes"
1365                                         ;;
1366                         esac
1367                 done
1368
1369                 if [ -n "${opt_linkfiles}" ] && [ -n "${opt_union}" ]
1370                 then
1371                         log_warning_msg "Skipping custom mount ${dest} with options ${options}: \"linkfiles\" and \"union\" are mutually exclusive options"
1372                 fi
1373
1374                 if [ -n "$(what_is_mounted_on "${dest}")" ]
1375                 then
1376                         log_warning_msg "Skipping custom mount ${dest}: $(what_is_mounted_on "${dest}") is already mounted there"
1377                         continue
1378                 fi
1379
1380                 # FIXME: we don't handle already existing
1381                 # non-directory files in the paths of both $source and
1382                 # $dest.
1383
1384                 if [ ! -d "${dest}" ]
1385                 then
1386                         # if ${dest} is in /home/$user, try fixing
1387                         # proper ownership
1388                         # FIXME: this should really be handled by
1389                         # live-config since we don't know for sure
1390                         # which uid a certain user has until then
1391                         if trim_path ${dest} | grep -qe "^${rootmnt}/*home/[^/]\+"
1392                         then
1393                                 path="/"
1394                                 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1395                                 do
1396                                         path=${path}/${dir}
1397                                         if [ ! -e ${path} ]
1398                                         then
1399                                                 mkdir -p ${path}
1400                                                 # assume that the intended user is the first, which is usually the case
1401                                                 chown 1000:1000 ${path}
1402                                         fi
1403                                 done
1404                         else
1405                                 mkdir -p ${dest}
1406                         fi
1407                 fi
1408
1409                 # if ${source} doesn't exist on our persistent media
1410                 # we bootstrap it with $dest from the live filesystem.
1411                 # this both makes sense and is critical if we're
1412                 # dealing with /etc or other system dir.
1413                 if [ ! -d "${source}" ]
1414                 then
1415                         if [ -n "${PERSISTENT_READONLY}" ]
1416                         then
1417                                 continue
1418                         elif [ -n "${opt_union}" ] || [ -n "${opt_linkfiles}" ]
1419                         then
1420                                 # unions and don't need to be bootstrapped
1421                                 # linkfiles dirs can't be bootstrapped in a sensible way
1422                                 mkdir "${source}"
1423                                 chown --reference "${dest}" "${source}"
1424                                 chmod --reference "${dest}" "${source}"
1425                         else
1426                                 # ensure that $dest is not copied *into* $source
1427                                 mkdir -p "$(dirname ${source})"
1428                                 cp -a "${dest}" "${source}"
1429                         fi
1430                 fi
1431
1432                 # XXX: If CONFIG_AUFS_ROBR is added to the Debian kernel we can
1433                 # ignore the loop below and set rofs_dest_backing=$dest
1434                 rofs_dest_backing=""
1435                 for d in ${rootmnt}/live/rofs/*
1436                 do
1437                         if [ -n "${rootmnt}" ]
1438                         then
1439                                 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1440                         else
1441                                 rofs_dest_backing="${d}/${dest}"
1442                         fi
1443                         if [ -d "${rofs_dest_backing}" ]
1444                         then
1445                                 break
1446                         else
1447                                 rofs_dest_backing=""
1448                         fi
1449                 done
1450
1451                 if [ -z "${PERSISTENT_READONLY}" ]
1452                 then
1453                         if [ -n "${opt_linkfiles}" ]
1454                         then
1455                                 links_source="${source}"
1456                                 links_dest="${dest}"
1457                         elif [ -n "${opt_union}" ]
1458                         then
1459                                 do_union ${dest} ${source} ${rofs_dest_backing}
1460                         else
1461                                 mount --bind "${source}" "${dest}"
1462                         fi
1463                 else
1464                         if [ -n "${opt_linkfiles}" ]
1465                         then
1466                                 links_dest="${dest}"
1467                                 dest="$(mktemp -d ${persistent_backing}/links_source-XXXXXX)"
1468                                 links_source="${dest}"
1469                         fi
1470                         if [ -n "${rootmnt}" ]
1471                         then
1472                                 cow_dir="$(echo ${dest} | sed -e "s|${rootmnt}|/cow/|")"
1473                         else
1474                                 cow_dir="/live/cow/${dest}"
1475                         fi
1476                         mkdir -p ${cow_dir}
1477                         chown --reference "${source}" "${cow_dir}"
1478                         chmod --reference "${source}" "${cow_dir}"
1479                         do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1480                 fi
1481
1482                 if [ -n "${opt_linkfiles}" ]
1483                 then
1484                         link_files "${links_source}" "${links_dest}" "${rootmnt}"
1485                 fi
1486
1487                 PERSISTENCE_IS_ON="1"
1488                 export PERSISTENCE_IS_ON
1489
1490                 if echo ${used_devices} | grep -qve "^\(.* \)\?${device}\( .*\)\?$"
1491                 then
1492                         used_devices="${used_devices} ${device}"
1493                 fi
1494         done < ${custom_mounts}
1495
1496         echo ${used_devices}
1497 }
1498
1499 fix_home_rw_compatibility ()
1500 {
1501         local device=${1}
1502
1503         if [ -n "${PERSISTENT_READONLY}" ]
1504         then
1505                 return
1506         fi
1507
1508         local backing="$(mount_persistent_media ${device})"
1509         if [ -z "${backing}" ]
1510         then
1511                 return
1512         fi
1513
1514         local include_list="${backing}/${persistence_list}"
1515         if [ ! -r "${include_list}" ]
1516         then
1517                 echo "# home-rw backwards compatibility:
1518 /home source=." > "${include_list}"
1519         fi
1520 }