1 # live-boot helper functions, used by live-boot on boot and by live-snapshot
3 if [ ! -x "/bin/fstype" ]
5 # klibc not in path -> not in initramfs
6 export PATH="${PATH}:/usr/lib/klibc/bin"
9 # handle upgrade path from old udev (using udevinfo) to
10 # recent versions of udev (using udevadm info)
11 if [ -x /sbin/udevadm ]
13 udevinfo='/sbin/udevadm info'
21 echo "/dev/$($udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
29 for dev in "${sysblock}"/* "${sysblock}"
31 if [ -e "${dev}/dev" ]
42 black_listed_devices="${1}"
43 white_listed_devices="${2}"
45 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
47 fulldevname=$(sys2dev "${sysblock}")
49 if echo "${black_listed_devices}" | grep -qe "\<${fulldevname}\>" || \
50 [ -n "${white_listed_devices}" ] && \
51 echo "${white_listed_devices}" | grep -qve "\<${fulldevname}\>"
53 # skip this device entirely
57 for dev in $(subdevices "${sysblock}")
59 devname=$(sys2dev "${dev}")
61 if echo "${black_listed_devices}" | grep -qe "\<${devname}\>"
76 # Validate input first
82 # Try to look if it is already supported by the kernel
83 if grep -q ${fstype} /proc/filesystems
87 # Then try to add support for it the gentle way using the initramfs capabilities
89 if grep -q ${fstype} /proc/filesystems
92 # Then try the hard way if /root is already reachable
94 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
95 if [ -e "${kmodule}" ]
98 if grep -q ${fstype} /proc/filesystems
111 /sbin/blkid -s TYPE -o value $1 2>/dev/null
118 if grep -q "^${device} " /proc/mounts
120 # return the first found
121 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
138 mounts="$(awk '{print $2}' /proc/mounts)"
139 testpath="$(busybox realpath ${testpath})"
143 if echo "${mounts}" | grep -qs "^${testpath}"
145 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
149 testpath=$(dirname $testpath)
156 # Returns used/free fs kbytes + 5% more
157 # You could pass a block device as ${1} or the mount point as ${2}
163 if [ -z "${mountp}" ]
165 mountp="$(where_is_mounted ${dev})"
167 if [ -z "${mountp}" ]
169 mountp="/mnt/tmp_fs_size"
172 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
178 if [ "${used}" = "used" ]
180 size=$(du -ks ${mountp} | cut -f1)
181 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
184 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
187 if [ -n "${doumount}" ]
189 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
199 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
201 loadkeys /etc/boottime.kmap.gz
211 local encryption=${5}
214 # the output of setup_loop is evaluated in other functions,
215 # modprobe leaks kernel options like "libata.dma=0"
216 # as "options libata dma=0" on stdout, causing serious
217 # problems therefor, so instead always avoid output to stdout
218 modprobe -q -b "${module}" 1>/dev/null
222 for loopdev in ${pattern}
224 if [ "$(cat ${loopdev}/size)" -eq 0 ]
226 dev=$(sys2dev "${loopdev}")
229 if [ -n "${readonly}" ]
231 if losetup --help 2>&1 | grep -q -- "-r\b"
233 options="${options} -r"
237 if [ -n "${offset}" ] && [ 0 -lt "${offset}" ]
239 options="${options} -o ${offset}"
242 if [ -z "${encryption}" ]
244 losetup ${options} "${dev}" "${fspath}"
246 # Loop AES encryption
251 echo -n "Enter passphrase for root filesystem: " >&6
253 echo "${passphrase}" > /tmp/passphrase
255 exec 9</tmp/passphrase
256 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
259 rm -f /tmp/passphrase
261 if [ 0 -eq ${error} ]
268 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
271 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
284 panic "No loop devices available"
294 old_mountp="$(where_is_mounted ${dev})"
296 if [ -n "${old_mountp}" ]
298 if [ "${opts}" != "ro" ]
300 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
303 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
305 if [ -z "${fstype}" ]
307 fstype=$(get_fstype "${dev}")
309 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
310 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > live-boot.log && return 0 )
317 name="$(basename ${dev})"
319 if [ -n "${PERSISTENT_READONLY}" ]
321 opts="${opts} --readonly"
328 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
329 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
333 luks_device="/dev/mapper/${name}"
339 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
342 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
349 find_persistent_media ()
351 # Scans devices for overlays and snapshots, and returns a whitespace
352 # separated list of how to use them. Only overlays with a partition
353 # label or file name in ${overlays} are returned, and ditto for
354 # snapshots with labels in ${snapshots}.
356 # When scanning a LUKS device, the user will be asked to enter the
357 # passphrase; on failure to enter it, or if no persistent partitions
358 # or files were found, the LUKS device is closed.
360 # For a snapshot file the return value is ${label}=${snapdata}", where
361 # ${snapdata} is the parameter used for try_snap().
363 # For all other cases (overlay/snapshot partition and overlay file) the
364 # return value is "${label}=${device}", where ${device} a device that
365 # can mount the content. In the case of an overlay file, the device
366 # containing the file will remain mounted as a side-effect.
368 # No devices in ${black_listed_devices} will be scanned, and if
369 # ${white_list_devices} is non-empty, only devices in it will be
374 black_listed_devices="${3}"
375 white_listed_devices="${4}"
376 persistent_backing="${rootmnt}/live/persistent"
378 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
382 # Checking for a luks device
383 if echo ${PERSISTENT_ENCRYPTION} | grep -qe "\<luks\>" && \
384 /sbin/cryptsetup isLuks ${dev}
386 if luks_device=$(open_luks_device "${dev}")
390 # skip $dev since we failed/chose not to open it
393 elif echo ${PERSISTENT_ENCRYPTION} | grep -qve "\<none\>"
395 # skip $dev since we don't allow unencrypted storage
399 if echo ${PERSISTENT_STORAGE} | grep -qe "\<filesystem\>"
401 for label in ${overlays} ${snapshots}
403 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
405 echo "${label}=${dev}"
406 # skip to the next device
414 if echo ${PERSISTENT_STORAGE} | grep -qe "\<file\>"
416 devfstype="$(get_fstype ${dev})"
417 backing="${persistent_backing}/$(basename ${dev})"
418 mkdir -p "${backing}"
419 if is_supported_fs ${devfstype} && try_mount "${dev}" "${backing}" "rw" "${devfstype}"
421 for label in ${overlays}
423 path=${backing}/${PERSISTENT_PATH}${label}
427 echo "${label}=$(setup_loop "${path}" "loop" "/sys/block/loop*")"
431 for label in ${snapshots}
433 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
435 path="${PERSISTENT_PATH}${label}.${ext}"
436 if [ -f "${backing}/${path}" ]
438 snapshot_on_dev="yes"
439 echo "${label}=${dev}:${backing}:${path}"
444 if [ -z "${overlay_on_dev}" ] && [ -z "${snapshot_on_dev}" ]
446 umount ${backing} > /dev/null 2>&1 || true
449 if [ -z "${overlay_on_dev}" ] && [ -z "${snapshot_on_dev}" ] && \
450 [ -n "${luks_device}" ] && \
451 /sbin/cryptsetup status "${luks_device}" 1> /dev/null 2>&1
453 /sbin/cryptsetup luksClose "${luks_device}"
462 for adaptor in /sys/class/net/*
464 status="$(cat ${adaptor}/iflink)"
466 if [ "${status}" -eq 2 ]
468 mac="$(cat ${adaptor}/address)"
469 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
479 if [ -x /sbin/cryptsetup ]
481 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
495 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
498 if [ "$(cat ${sysblock}/removable)" = "1" ]
500 if [ -z "${want_usb}" ]
504 if readlink ${sysblock} | grep -q usb
511 if [ "${dev_ok}" = "yes" ]
513 case "${output_format}" in
515 ret="${ret} ${sysblock}"
518 devname=$(sys2dev "${sysblock}")
519 ret="${ret} ${devname}"
532 removable_dev "${output_format}" "want_usb"
540 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
542 if [ "$(cat ${sysblock}/removable)" = "0" ]
544 case "${output_format}" in
546 ret="${ret} ${sysblock}"
549 devname=$(sys2dev "${sysblock}")
550 ret="${ret} ${devname}"
561 # create source's directory structure in dest, and recursively
562 # create symlinks in dest to to all files in source. if mask
563 # is non-empty, remove mask from all source paths when
564 # creating links (will be necessary if we change root, which
565 # live-boot normally does (into $rootmnt)).
567 # remove multiple /:s and ensure ending on /
568 local src_dir="$(echo "${1}"/ | sed -e 's|/\+|/|g')"
569 local dest_dir="$(echo "${2}"/ | sed -e 's|/\+|/|g')"
570 local src_mask="${3}"
572 # This check can only trigger on the inital, non-recursive call since
573 # we create the destination before recursive calls
574 if [ ! -d "${dest_dir}" ];
576 log_warning_msg "Must link_files into a directory"
580 find "${src_dir}" -mindepth 1 -maxdepth 1 | while read x; do
582 local dest="${dest_dir}$(basename "${x}")"
585 if [ -z "$(ls -A "${src}")" ];
589 if [ ! -d "${dest}" ];
592 prev="$(dirname "${dest}")"
593 chown $(stat -c %u:%g "${prev}") "${dest}"
594 chmod $(stat -c %a "${prev}") "${dest}"
596 link_files "${src}" "${dest}" "${src_mask}"
598 if [ -n "${src_mask}" ]
600 src="$(echo ${src} | sed "s|^${src_mask}||")"
602 rm -rf "${dest}" 2> /dev/null
603 ln -s "${src}" "${dest}"
609 local unionmountpoint="${1}" # directory where the union is mounted
610 local unionrw="${2}" # branch where the union changes are stored
611 local unionro1="${3}" # first underlying read-only branch (optional)
612 local unionro2="${4}" # second underlying read-only branch (optional)
614 if [ "${UNIONTYPE}" = "aufs" ]
619 elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
628 case "${UNIONTYPE}" in
630 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
631 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
632 if [ -n "${unionro1}" ]
634 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
636 if [ -n "${unionro2}" ]
638 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
640 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
641 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
642 ( mkdir -p /run/sendsigs.omit.d
643 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
647 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
648 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
649 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
650 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
654 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
655 if [ -n "${unionro1}" ]
657 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
659 if [ -n "${unionro2}" ]
661 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
663 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
668 get_custom_mounts () {
669 # Side-effect: leaves $devices with live.persist mounted in ${rootmnt}/live/persistent
670 # Side-effect: prints info to file $custom_mounts
673 local custom_mounts="${2}" # print result to this file
674 local rootmnt="${3}" # should be set empty post-live-boot
676 local bindings="/bindings.list"
677 local links="/links.list"
678 rm -rf ${bindings} ${links} 2> /dev/null
679 local persistent_backing="${rootmnt}/live/persistent"
681 for device in ${devices}
683 if [ ! -b "${device}" ]
687 local device_name="$(basename ${device})"
688 local backing="${persistent_backing}/${device_name}"
689 mkdir -p "${backing}"
690 local device_fstype="$(get_fstype ${device})"
691 if [ -z "${PERSISTENT_READONLY}" ]
693 device_mount_opts="rw,noatime"
695 device_mount_opts="ro,noatime"
698 mount -t "${device_fstype}" -o "${device_mount_opts}" "${device}" "${backing}"
699 local include_list="${backing}/${persistence_list}"
700 if [ ! -r "${include_list}" ]
707 [ "${DEBUG}" = "Yes" ] && cp ${include_list} ${persistent_backing}/${persistence_list}.${device_name}
708 while read source dest options # < ${include_list}
710 if echo ${source} | grep -qe "^[[:space:]]*\(#.*\)\?$"
712 # skipping empty or commented lines
716 if echo ${dest} | grep -qe "^[^/]"
720 elif [ -z "${dest}" ]
725 if echo ${dest} | grep -qe "^/\+$\|^/\+live\(/.*\)\?$"
727 # mounting on / or /live could cause trouble
728 log_warning_msg "Skipping unsafe custom mount on ${dest}"
732 for opt in $(echo ${options} | tr ',' ' ');
738 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
744 # FIXME: handle case: we already have /a/b in
745 # $bindings added from current $device, but
746 # now we find /a -- /a should replace /a/b in
749 # FIXME: handle case: we have /a in $bindings
750 # from current $device, now we find /a/b, so
753 # ensure that no multiple-/ occur in paths
754 local full_source="$(echo ${backing}/${source}/ | sed -e 's|/\+|/|g')"
755 local full_dest="$(echo ${rootmnt}/${dest}/ | sed -e 's|/\+|/|g')"
757 if echo ${options} | grep -qe "\<linkfiles\>";
759 echo "${full_source} ${full_dest} ${options}" >> ${links}
761 echo "${full_source} ${full_dest} ${options}" >> ${bindings}
763 done < ${include_list}
765 if [ -z "${device_used}" ]
767 # this device was not used for / earlier, or
768 # custom mount point now, so it's useless
774 # We sort the list according to destination so we're sure that
775 # we won't hide a previous mount. We also ignore duplicate
776 # destinations in a more or less arbitrary way.
777 [ -e "${bindings}" ] && sort -k2 -sbu ${bindings} >> ${custom_mounts}
780 # After all mounts are considered we add symlinks so they
781 # won't be hidden by some mount.
782 [ -e "${links}" ] && sort -k2 -sbu ${links} >> ${custom_mounts}
785 rm -f ${bindings} ${links} 2> /dev/null
786 echo ${custom_mounts}
789 do_custom_mounts () {
790 local custom_mounts="${1}" # the ouput from get_custom_mounts()
791 local rootmnt="${2}" # should be set empty post-live-boot
793 while read source dest options # < ${custom_mounts}
795 local opt_linkfiles=""
797 for opt in $(echo ${options} | tr ',' ' ');
809 if mountpoint -q "${dest}";
811 log_warning_msg "Skipping custom mount ${source} on ${dest}: destination is already a mount point"
815 # FIXME: we don't handle already existing
816 # non-directory files in the paths of both $source and
819 if [ ! -d "${dest}" ]
821 # if ${dest} is in /home/$user, try fixing
823 # FIXME: this should really be handled by
824 # live-config since we don't know for sure
825 # which uid a certain user has until then
826 if echo ${dest} | grep -qe "^${rootmnt}/*home/\+[^/]\+"
829 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
835 # assume that the intended user is the first, which is usually the case
836 chown 1000:1000 ${path}
844 # if ${source} doesn't exist on our persistent media
845 # we bootstrap it with $dest from the live filesystem.
846 # this both makes sense and is critical if we're
847 # dealing with /etc or other system dir.
848 if [ ! -d "${source}" ]
850 if [ -n "${PERSISTENT_READONLY}" ] || [ -n "${opt_linkfiles}" ]
853 elif [ -n "${opt_union}" ]
855 # union's don't need to be bootstrapped
858 # ensure that $dest is not copied *into* $source
859 mkdir -p "$(dirname ${source})"
860 cp -a "${dest}" "${source}"
865 for d in ${rootmnt}/live/rofs/*
867 if [ -n "${rootmnt}" ]
869 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
871 rofs_dest_backing="${d}/${dest}"
874 if [ -d "${rofs_dest_backing}" ]
882 if [ -z "${PERSISTENT_READONLY}" ]
884 if [ -n "${opt_linkfiles}" ]
886 links_source="${source}"
888 elif [ -n "${opt_union}" ]
890 do_union ${dest} ${source} ${rofs_dest_backing}
892 mount --bind "${source}" "${dest}"
895 if [ -n "${opt_linkfiles}" ]
898 dest="$(mktemp -d ${persistent_backing}/links_source-XXXXXX)"
899 links_source="${dest}"
901 if [ -n "${rootmnt}" ]
903 cow_dir="$(echo ${dest} | sed -e "s|${rootmnt}|/cow/|")"
905 cow_dir="/live/cow/${dest}"
908 do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
911 if [ -n "${opt_linkfiles}" ]
913 link_files "${links_source}" "${links_dest}" "${rootmnt}"
916 PERSISTENCE_IS_ON="1"
917 export PERSISTENCE_IS_ON
918 done < ${custom_mounts}