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