We don't black list storage devices any more.
[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
518         if grep -q "^${device} " /proc/mounts
519         then
520                 # return the first found
521                 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
522         fi
523 }
524
525 lastline ()
526 {
527         while read lines
528         do
529                 line=${lines}
530         done
531
532         echo "${line}"
533 }
534
535 base_path ()
536 {
537         testpath="${1}"
538         mounts="$(awk '{print $2}' /proc/mounts)"
539         testpath="$(busybox realpath ${testpath})"
540
541         while true
542         do
543                 if echo "${mounts}" | grep -qs "^${testpath}"
544                 then
545                         set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
546                         echo ${1}
547                         break
548                 else
549                         testpath=$(dirname $testpath)
550                 fi
551         done
552 }
553
554 fs_size ()
555 {
556         # Returns used/free fs kbytes + 5% more
557         # You could pass a block device as ${1} or the mount point as ${2}
558
559         dev="${1}"
560         mountp="${2}"
561         used="${3}"
562
563         if [ -z "${mountp}" ]
564         then
565                 mountp="$(where_is_mounted ${dev})"
566
567                 if [ -z "${mountp}" ]
568                 then
569                         mountp="/mnt/tmp_fs_size"
570
571                         mkdir -p "${mountp}"
572                         mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
573
574                         doumount=1
575                 fi
576         fi
577
578         if [ "${used}" = "used" ]
579         then
580                 size=$(du -ks ${mountp} | cut -f1)
581                 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
582         else
583                 # free space
584                 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
585         fi
586
587         if [ -n "${doumount}" ]
588         then
589                 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
590                 rmdir "${mountp}"
591         fi
592
593         echo "${size}"
594 }
595
596 load_keymap ()
597 {
598         # Load custom keymap
599         if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
600         then
601                 loadkeys /etc/boottime.kmap.gz
602         fi
603 }
604
605 setup_loop ()
606 {
607         local fspath=${1}
608         local module=${2}
609         local pattern=${3}
610         local offset=${4}
611         local encryption=${5}
612         local readonly=${6}
613
614         # the output of setup_loop is evaluated in other functions,
615         # modprobe leaks kernel options like "libata.dma=0"
616         # as "options libata dma=0" on stdout, causing serious
617         # problems therefor, so instead always avoid output to stdout
618         modprobe -q -b "${module}" 1>/dev/null
619
620         udevadm settle
621
622         for loopdev in ${pattern}
623         do
624                 if [ "$(cat ${loopdev}/size)" -eq 0 ]
625                 then
626                         dev=$(sys2dev "${loopdev}")
627                         options=''
628
629                         if [ -n "${readonly}" ]
630                         then
631                                 if losetup --help 2>&1 | grep -q -- "-r\b"
632                                 then
633                                         options="${options} -r"
634                                 fi
635                         fi
636
637                         if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
638                         then
639                                 options="${options} -o ${offset}"
640                         fi
641
642                         if [ -z "${encryption}" ]
643                         then
644                                 losetup ${options} "${dev}" "${fspath}"
645                         else
646                                 # Loop AES encryption
647                                 while true
648                                 do
649                                         load_keymap
650
651                                         echo -n "Enter passphrase for root filesystem: " >&6
652                                         read -s passphrase
653                                         echo "${passphrase}" > /tmp/passphrase
654                                         unset passphrase
655                                         exec 9</tmp/passphrase
656                                         /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
657                                         error=${?}
658                                         exec 9<&-
659                                         rm -f /tmp/passphrase
660
661                                         if [ 0 -eq ${error} ]
662                                         then
663                                                 unset error
664                                                 break
665                                         fi
666
667                                         echo
668                                         echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
669                                         read answer
670
671                                         if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
672                                         then
673                                                 unset answer
674                                                 break
675                                         fi
676                                 done
677                         fi
678
679                         echo "${dev}"
680                         return 0
681                 fi
682         done
683
684         panic "No loop devices available"
685 }
686
687 try_mount ()
688 {
689         dev="${1}"
690         mountp="${2}"
691         opts="${3}"
692         fstype="${4}"
693
694         old_mountp="$(where_is_mounted ${dev})"
695
696         if [ -n "${old_mountp}" ]
697         then
698                 if [ "${opts}" != "ro" ]
699                 then
700                         mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
701                 fi
702
703                 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
704         else
705                 if [ -z "${fstype}" ]
706                 then
707                         fstype=$(get_fstype "${dev}")
708                 fi
709                 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
710                 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > live-boot.log && return 0 )
711         fi
712 }
713
714 open_luks_device ()
715 {
716         dev="${1}"
717         name="$(basename ${dev})"
718         opts="--key-file=-"
719         if [ -n "${PERSISTENT_READONLY}" ]
720         then
721                 opts="${opts} --readonly"
722         fi
723
724         load_keymap
725
726         while true
727         do
728                 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
729                         /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
730
731                 if [ 0 -eq ${?} ]
732                 then
733                         luks_device="/dev/mapper/${name}"
734                         echo ${luks_device}
735                         return 0
736                 fi
737
738                 echo >&6
739                 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
740                 read answer
741
742                 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
743                 then
744                         return 2
745                 fi
746         done
747 }
748
749 get_gpt_name () {
750     local dev="${1}"
751     /sbin/blkid -s PART_ENTRY_NAME -p -o value ${dev} 2>/dev/null
752 }
753
754 is_gpt_device () {
755     local dev="${1}"
756     [ "$(/sbin/blkid -s PART_ENTRY_SCHEME -p -o value ${dev} 2>/dev/null)" = "gpt" ]
757 }
758
759 probe_for_gpt_name ()
760 {
761         local overlays="${1}"
762         local snapshots="${2}"
763         local dev="${3}"
764
765         if ! is_gpt_device ${dev} || \
766            ( echo ${PERSISTENT_ENCRYPTION} | grep -qve "\<luks\>" && \
767              /sbin/cryptsetup isLuks ${dev} > /dev/null 2>&1 )
768         then
769                 return
770         fi
771         for label in ${overlays} ${snapshots}
772         do
773                 if [ "$(get_gpt_name ${dev})" = "${label}" ]
774                 then
775                         echo "${label}=${dev}"
776                 fi
777         done
778 }
779
780 probe_for_fs_label () {
781         local overlays="${1}"
782         local snapshots="${2}"
783         local dev="${3}"
784
785         for label in ${overlays} ${snapshots}
786         do
787                 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
788                 then
789                         echo "${label}=${dev}"
790                 fi
791         done
792 }
793
794 probe_for_file_name () {
795         local overlays="${1}"
796         local snapshots="${2}"
797         local dev="${3}"
798
799         local devfstype="$(get_fstype ${dev})"
800         local backing="${rootmnt}/live/persistent/$(basename ${dev})"
801         local ret=""
802         if is_supported_fs ${devfstype} && mkdir -p "${backing}" && \
803            try_mount "${dev}" "${backing}" "rw" "${devfstype}"
804         then
805                 for label in ${overlays}
806                 do
807                         path=${backing}/${PERSISTENT_PATH}${label}
808                         if [ -f "${path}" ]
809                         then
810                                 local loopdev=$(setup_loop "${path}" "loop" "/sys/block/loop*")
811                                 ret="${ret} ${label}=${loopdev}"
812                         fi
813                 done
814                 for label in ${snapshots}
815                 do
816                         for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
817                         do
818                                 path="${PERSISTENT_PATH}${label}.${ext}"
819                                 if [ -f "${backing}/${path}" ]
820                                 then
821                                         ret="${ret} ${label}=${dev}:${backing}:${path}"
822                                 fi
823                         done
824                 done
825
826                 if [ -n "${ret}" ]
827                 then
828                         echo ${ret}
829                 else
830                         umount ${backing} > /dev/null 2>&1 || true
831                 fi
832         fi
833 }
834
835 find_persistent_media ()
836 {
837         # Scans devices for overlays and snapshots, and returns a whitespace
838         # separated list of how to use them. Only overlays with a partition
839         # label or file name in ${overlays} are returned, and ditto for
840         # snapshots with labels in ${snapshots}.
841         #
842         # When scanning a LUKS device, the user will be asked to enter the
843         # passphrase; on failure to enter it, or if no persistent partitions
844         # or files were found, the LUKS device is closed.
845         #
846         # For a snapshot file the return value is ${label}=${snapdata}", where
847         # ${snapdata} is the parameter used for try_snap().
848         #
849         # For all other cases (overlay/snapshot partition and overlay file) the
850         # return value is "${label}=${device}", where ${device} a device that
851         # can mount the content. In the case of an overlay file, the device
852         # containing the file will remain mounted as a side-effect.
853         #
854         # No devices in ${black_listed_devices} will be scanned, and if
855         # ${white_list_devices} is non-empty, only devices in it will be
856         # scanned.
857
858         local overlays="${1}"
859         local snapshots="${2}"
860         local white_listed_devices="${3}"
861         local ret=""
862
863         for dev in $(storage_devices "" "${white_listed_devices}")
864         do
865                 local result=""
866
867                 local real_dev=""
868                 local luks_device=""
869                 # Check if it's a luks device; we'll have to open the device
870                 # in order to probe any filesystem it contains, like we do
871                 # below. do_custom_mounts() also depends on that any luks
872                 # device already has been opened.
873                 if echo ${PERSISTENT_ENCRYPTION} | grep -qe "\<luks\>" && \
874                    /sbin/cryptsetup isLuks ${dev} >/dev/null 2>&1
875                 then
876                         if luks_device=$(open_luks_device "${dev}")
877                         then
878                                 real_dev="${dev}"
879                                 dev="${luks_device}"
880                         else
881                                 # skip $dev since we failed/chose not to open it
882                                 continue
883                         fi
884                 elif echo ${PERSISTENT_ENCRYPTION} | grep -qve "\<none\>"
885                 then
886                         # skip $dev since we don't allow unencrypted storage
887                         continue
888                 fi
889
890                 # Probe for matching GPT partition names or filesystem labels
891                 if echo ${PERSISTENT_STORAGE} | grep -qe "\<filesystem\>"
892                 then
893                         local gpt_dev="${dev}"
894                         if [ -n "${luks_device}" ]
895                         then
896                                 # When we probe GPT partitions we need to look
897                                 # at the real device, not the virtual, opened
898                                 # luks device
899                                 gpt_dev="${real_dev}"
900                         fi
901                         result=$(probe_for_gpt_name "${overlays}" "${snapshots}" ${gpt_dev})
902                         if [ -n "${result}" ]
903                         then
904                                 ret="${ret} ${result}"
905                                 continue
906                         fi
907
908                         result=$(probe_for_fs_label "${overlays}" "${snapshots}" ${dev})
909                         if [ -n "${result}" ]
910                         then
911                                 ret="${ret} ${result}"
912                                 continue
913                         fi
914                 fi
915
916                 # Probe for files with matching name on mounted partition
917                 if echo ${PERSISTENT_STORAGE} | grep -qe "\<file\>"
918                 then
919                         result=$(probe_for_file_name "${overlays}" "${snapshots}" ${dev})
920                         if [ -n "${result}" ]
921                         then
922                                 ret="${ret} ${result}"
923                                 continue
924                         fi
925                 fi
926
927                 # Close luks device if it isn't used
928                 if [ -z "${result}" ] && [ -n "${luks_device}" ] && \
929                    /sbin/cryptsetup status "${luks_device}" 1> /dev/null 2>&1
930                 then
931                         /sbin/cryptsetup luksClose "${luks_device}"
932                 fi
933         done
934
935         if [ -n "${ret}" ]
936         then
937                 echo ${ret}
938         fi
939 }
940
941 get_mac ()
942 {
943         mac=""
944
945         for adaptor in /sys/class/net/*
946         do
947                 status="$(cat ${adaptor}/iflink)"
948
949                 if [ "${status}" -eq 2 ]
950                 then
951                         mac="$(cat ${adaptor}/address)"
952                         mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
953                 fi
954         done
955
956         echo ${mac}
957 }
958
959 is_luks()
960 {
961     devname="${1}"
962     if [ -x /sbin/cryptsetup ]
963     then
964         /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
965         return ${ret}
966     else
967         return 1
968     fi
969
970 }
971
972 removable_dev ()
973 {
974         output_format="${1}"
975         want_usb="${2}"
976         ret=
977
978         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
979         do
980                 dev_ok=
981                 if [ "$(cat ${sysblock}/removable)" = "1" ]
982                 then
983                         if [ -z "${want_usb}" ]
984                         then
985                                 dev_ok="yes"
986                         else
987                                 if readlink ${sysblock} | grep -q usb
988                                 then
989                                         dev_ok="yes"
990                                 fi
991                         fi
992                 fi
993
994                 if [ "${dev_ok}" = "yes" ]
995                 then
996                         case "${output_format}" in
997                                 sys)
998                                         ret="${ret} ${sysblock}"
999                                         ;;
1000                                 *)
1001                                         devname=$(sys2dev "${sysblock}")
1002                                         ret="${ret} ${devname}"
1003                                         ;;
1004                         esac
1005                 fi
1006         done
1007
1008         echo "${ret}"
1009 }
1010
1011 removable_usb_dev ()
1012 {
1013         output_format="${1}"
1014
1015         removable_dev "${output_format}" "want_usb"
1016 }
1017
1018 non_removable_dev ()
1019 {
1020         output_format="${1}"
1021         ret=
1022
1023         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
1024         do
1025                 if [ "$(cat ${sysblock}/removable)" = "0" ]
1026                 then
1027                         case "${output_format}" in
1028                                 sys)
1029                                         ret="${ret} ${sysblock}"
1030                                         ;;
1031                                 *)
1032                                         devname=$(sys2dev "${sysblock}")
1033                                         ret="${ret} ${devname}"
1034                                         ;;
1035                         esac
1036                 fi
1037         done
1038
1039         echo "${ret}"
1040 }
1041
1042 link_files ()
1043 {
1044         # create source's directory structure in dest, and recursively
1045         # create symlinks in dest to to all files in source. if mask
1046         # is non-empty, remove mask from all source paths when
1047         # creating links (will be necessary if we change root, which
1048         # live-boot normally does (into $rootmnt)).
1049
1050         # remove multiple /:s and ensure ending on /
1051         local src_dir="$(echo "${1}"/ | sed -e 's|/\+|/|g')"
1052         local dest_dir="$(echo "${2}"/ | sed -e 's|/\+|/|g')"
1053         local src_mask="${3}"
1054
1055         # This check can only trigger on the inital, non-recursive call since
1056         # we create the destination before recursive calls
1057         if [ ! -d "${dest_dir}" ];
1058         then
1059                 log_warning_msg "Must link_files into a directory"
1060                 return
1061         fi
1062
1063         find "${src_dir}" -mindepth 1 -maxdepth 1 | while read x; do
1064                 local src="${x}"
1065                 local dest="${dest_dir}$(basename "${x}")"
1066                 if [ -d "${src}" ];
1067                 then
1068                         if [ -z "$(ls -A "${src}")" ];
1069                         then
1070                                 continue
1071                         fi
1072                         if [ ! -d "${dest}" ];
1073                         then
1074                                 mkdir -p "${dest}"
1075                                 prev="$(dirname "${dest}")"
1076                                 chown --reference "${prev}" "${dest}"
1077                                 chmod --reference "${prev}" "${dest}"
1078                         fi
1079                         link_files "${src}" "${dest}" "${src_mask}"
1080                 else
1081                         if [ -n "${src_mask}" ]
1082                         then
1083                                 src="$(echo ${src} | sed "s|^${src_mask}||")"
1084                         fi
1085                         rm -rf "${dest}" 2> /dev/null
1086                         ln -s "${src}" "${dest}"
1087                 fi
1088         done
1089 }
1090
1091 do_union () {
1092         local unionmountpoint="${1}"    # directory where the union is mounted
1093         local unionrw="${2}"            # branch where the union changes are stored
1094         local unionro1="${3}"           # first underlying read-only branch (optional)
1095         local unionro2="${4}"           # second underlying read-only branch (optional)
1096
1097         if [ "${UNIONTYPE}" = "aufs" ]
1098         then
1099                 rw_opt="rw"
1100                 ro_opt="rr+wh"
1101                 noxino_opt="noxino"
1102         elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
1103         then
1104                 rw_opt="RW"
1105                 ro_opt="RO"
1106         else
1107                 rw_opt="rw"
1108                 ro_opt="ro"
1109         fi
1110
1111         case "${UNIONTYPE}" in
1112                 unionfs-fuse)
1113                         unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
1114                         unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
1115                         if [ -n "${unionro1}" ]
1116                         then
1117                                 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1118                         fi
1119                         if [ -n "${unionro2}" ]
1120                         then
1121                                 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1122                         fi
1123                         ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
1124                         unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
1125                         ( mkdir -p /run/sendsigs.omit.d
1126                         pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
1127                         ;;
1128
1129                 overlayfs)
1130                         # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
1131                         # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
1132                         unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
1133                         mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1134                         ;;
1135
1136                 *)
1137                         unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
1138                         if [ -n "${unionro1}" ]
1139                         then
1140                                 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
1141                         fi
1142                         if [ -n "${unionro2}" ]
1143                         then
1144                                 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
1145                         fi
1146                         mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
1147                         ;;
1148         esac
1149 }
1150
1151 get_custom_mounts () {
1152         # Side-effect: leaves $devices with live.persist mounted in ${rootmnt}/live/persistent
1153         # Side-effect: prints info to file $custom_mounts
1154
1155         local devices="${1}"
1156         local custom_mounts="${2}" # print result to this file
1157         local rootmnt="${3}"       # should be set empty post-live-boot
1158
1159         local bindings="/bindings.list"
1160         local links="/links.list"
1161         rm -rf ${bindings} ${links} 2> /dev/null
1162         local persistent_backing="${rootmnt}/live/persistent"
1163
1164         for device in ${devices}
1165         do
1166                 if [ ! -b "${device}" ]
1167                 then
1168                         continue
1169                 fi
1170
1171                 local device_name="$(basename ${device})"
1172                 local device_used=""
1173                 # $device may already have been mounted by
1174                 # probe_for_file_name() in find_persistent_media() ...
1175                 local backing=$(where_is_mounted ${device})
1176                 if [ -z "${backing}" ]
1177                 then
1178                         # ... otherwise we mount it now
1179                         backing="${persistent_backing}/${device_name}"
1180                         mkdir -p "${backing}"
1181                         local device_fstype="$(get_fstype ${device})"
1182                         if [ -z "${PERSISTENT_READONLY}" ]
1183                         then
1184                                 device_mount_opts="rw,noatime"
1185                         else
1186                                 device_mount_opts="ro,noatime"
1187                         fi
1188                         if ! mount -t "${device_fstype}" -o "${device_mount_opts}" "${device}" "${backing}" >/dev/null 2>&1
1189                         then
1190                                 log_warning_msg "Could not mount persistent media ${device} (${device_fstype})"
1191                         fi
1192                 fi
1193                 local include_list="${backing}/${persistence_list}"
1194                 if [ ! -r "${include_list}" ]
1195                 then
1196                         umount "${backing}" >/dev/null 2>&1
1197                         rmdir "${backing}" >/dev/null 2>&1
1198                         if /sbin/cryptsetup status ${device_name} >/dev/null 2>&1
1199                         then
1200                                 /sbin/cryptsetup luksClose "${device_name}"
1201                         fi
1202                         continue
1203                 fi
1204
1205                 [ "${DEBUG}" = "Yes" ] && cp ${include_list} ${persistent_backing}/${persistence_list}.${device_name}
1206                 while read source dest options # < ${include_list}
1207                 do
1208                         if echo ${source} | grep -qe "^[[:space:]]*\(#.*\)\?$"
1209                         then
1210                                 # skipping empty or commented lines
1211                                 continue
1212                         fi
1213
1214                         if echo ${dest} | grep -qe "^[^/]"
1215                         then
1216                                 options="${dest}"
1217                                 dest="${source}"
1218                         elif [ -z "${dest}" ]
1219                         then
1220                                 dest="${source}"
1221                         fi
1222
1223                         if echo ${dest} | grep -qe "^/\+$\|^/\+live\(/.*\)\?$"
1224                         then
1225                                 # mounting on / or /live could cause trouble
1226                                 log_warning_msg "Skipping unsafe custom mount on ${dest}"
1227                                 continue
1228                         fi
1229
1230                         for opt in $(echo ${options} | tr ',' ' ');
1231                         do
1232                                 case "${opt}" in
1233                                         linkfiles|union)
1234                                                 ;;
1235                                         *)
1236                                                 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
1237                                                 continue 2
1238                                                 ;;
1239                                 esac
1240                         done
1241
1242                         # FIXME: handle case: we already have /a/b in
1243                         # $bindings added from current $device, but
1244                         # now we find /a -- /a should replace /a/b in
1245                         # $bindings.
1246
1247                         # FIXME: handle case: we have /a in $bindings
1248                         # from current $device, now we find /a/b, so
1249                         # we skip /a/b
1250
1251                         # ensure that no multiple-/ occur in paths
1252                         local full_source="$(echo ${backing}/${source}/ | sed -e 's|/\+|/|g')"
1253                         local full_dest="$(echo ${rootmnt}/${dest}/ | sed -e 's|/\+|/|g')"
1254                         device_used="yes"
1255                         if echo ${options} | grep -qe "\<linkfiles\>";
1256                         then
1257                                 echo "${full_source} ${full_dest} ${options}" >> ${links}
1258                         else
1259                                 echo "${full_source} ${full_dest} ${options}" >> ${bindings}
1260                         fi
1261                 done < ${include_list}
1262
1263                 if [ -z "${device_used}" ]
1264                 then
1265                         # this device was not used for / earlier, or
1266                         # custom mount point now, so it's useless
1267                         umount "${backing}"
1268                         rmdir "${backing}"
1269                 fi
1270         done
1271
1272         # We sort the list according to destination so we're sure that
1273         # we won't hide a previous mount. We also ignore duplicate
1274         # destinations in a more or less arbitrary way.
1275         [ -e "${bindings}" ] && sort -k2 -sbu ${bindings} >> ${custom_mounts} && rm ${bindings}
1276
1277         # After all mounts are considered we add symlinks so they
1278         # won't be hidden by some mount.
1279         [ -e "${links}" ] && sort -k2 -sbu ${links} >> ${custom_mounts} && rm ${links}
1280 }
1281
1282 do_custom_mounts () {
1283         local custom_mounts="${1}" # the ouput from get_custom_mounts()
1284         local rootmnt="${2}"       # should be set empty post-live-boot
1285
1286         while read source dest options # < ${custom_mounts}
1287         do
1288                 local opt_linkfiles=""
1289                 local opt_union=""
1290                 for opt in $(echo ${options} | tr ',' ' ');
1291                 do
1292                          case "${opt}" in
1293                                 linkfiles)
1294                                         opt_linkfiles="yes"
1295                                         ;;
1296                                 union)
1297                                         opt_union="yes"
1298                                         ;;
1299                         esac
1300                 done
1301
1302                 if mountpoint -q "${dest}";
1303                 then
1304                         log_warning_msg "Skipping custom mount ${source} on ${dest}: destination is already a mount point"
1305                         continue
1306                 fi
1307
1308                 # FIXME: we don't handle already existing
1309                 # non-directory files in the paths of both $source and
1310                 # $dest.
1311
1312                 if [ ! -d "${dest}" ]
1313                 then
1314                         # if ${dest} is in /home/$user, try fixing
1315                         # proper ownership
1316                         # FIXME: this should really be handled by
1317                         # live-config since we don't know for sure
1318                         # which uid a certain user has until then
1319                         if echo ${dest} | grep -qe "^${rootmnt}/*home/\+[^/]\+"
1320                         then
1321                                 path="/"
1322                                 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
1323                                 do
1324                                         path=${path}/${dir}
1325                                         if [ ! -e ${path} ]
1326                                         then
1327                                                 mkdir -p ${path}
1328                                                 # assume that the intended user is the first, which is usually the case
1329                                                 chown 1000:1000 ${path}
1330                                         fi
1331                                 done
1332                         else
1333                                 mkdir -p ${dest}
1334                         fi
1335                 fi
1336
1337                 # if ${source} doesn't exist on our persistent media
1338                 # we bootstrap it with $dest from the live filesystem.
1339                 # this both makes sense and is critical if we're
1340                 # dealing with /etc or other system dir.
1341                 if [ ! -d "${source}" ]
1342                 then
1343                         if [ -n "${PERSISTENT_READONLY}" ] || [ -n "${opt_linkfiles}" ]
1344                         then
1345                                 continue
1346                         elif [ -n "${opt_union}" ]
1347                         then
1348                                 # union's don't need to be bootstrapped
1349                                 mkdir "${source}"
1350                         else
1351                                 # ensure that $dest is not copied *into* $source
1352                                 mkdir -p "$(dirname ${source})"
1353                                 cp -a "${dest}" "${source}"
1354                         fi
1355                 fi
1356
1357                 rofs_dest_backing=""
1358                 for d in ${rootmnt}/live/rofs/*
1359                 do
1360                         if [ -n "${rootmnt}" ]
1361                         then
1362                                 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
1363                         else
1364                                 rofs_dest_backing="${d}/${dest}"
1365
1366                         fi
1367                         if [ -d "${rofs_dest_backing}" ]
1368                         then
1369                                 break
1370                         else
1371                                 rofs_dest_backing=""
1372                         fi
1373                 done
1374
1375                 if [ -z "${PERSISTENT_READONLY}" ]
1376                 then
1377                         if [ -n "${opt_linkfiles}" ]
1378                         then
1379                                 links_source="${source}"
1380                                 links_dest="${dest}"
1381                         elif [ -n "${opt_union}" ]
1382                         then
1383                                 do_union ${dest} ${source} ${rofs_dest_backing}
1384                         else
1385                                 mount --bind "${source}" "${dest}"
1386                         fi
1387                 else
1388                         if [ -n "${opt_linkfiles}" ]
1389                         then
1390                                 links_dest="${dest}"
1391                                 dest="$(mktemp -d ${persistent_backing}/links_source-XXXXXX)"
1392                                 links_source="${dest}"
1393                         fi
1394                         if [ -n "${rootmnt}" ]
1395                         then
1396                                 cow_dir="$(echo ${dest} | sed -e "s|${rootmnt}|/cow/|")"
1397                         else
1398                                 cow_dir="/live/cow/${dest}"
1399                         fi
1400                         mkdir -p ${cow_dir}
1401                         do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
1402                 fi
1403
1404                 if [ -n "${opt_linkfiles}" ]
1405                 then
1406                         link_files "${links_source}" "${links_dest}" "${rootmnt}"
1407                 fi
1408
1409                 PERSISTENCE_IS_ON="1"
1410                 export PERSISTENCE_IS_ON
1411         done < ${custom_mounts}
1412 }
1413
1414 fix_home_rw_compatibility ()
1415 {
1416         local device=${1}
1417
1418         if [ -n "${PERSISTENT_READONLY}" ]
1419         then
1420                 return
1421         fi
1422
1423         local backing="$(where_is_mounted ${device})"
1424         if [ -z "${backing}" ]
1425         then
1426                 backing="${rootmnt}/live/persistent/$(basename ${device})"
1427                 mkdir -p "${backing}"
1428                 local device_fstype="$(get_fstype ${device})"
1429         local device_mount_opts="rw,noatime"
1430         if ! mount -t "${device_fstype}" -o "${device_mount_opts}" "${device}" "${backing}" >/dev/null 2>&1
1431         then
1432                 return
1433         fi
1434
1435         local include_list="${backing}/${persistence_list}"
1436         if [ ! -r "${include_list}" ]
1437         then
1438                 echo "# home-rw backwards compatibility:
1439 . /home" > "${include_list}"
1440         fi
1441 }