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}"
377 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
381 # Checking for a luks device
382 if echo ${PERSISTENT_ENCRYPTION} | grep -qe "\<luks\>" && \
383 /sbin/cryptsetup isLuks ${dev}
385 if luks_device=$(open_luks_device "${dev}")
389 # skip $dev since we failed/chose not to open it
392 elif echo ${PERSISTENT_ENCRYPTION} | grep -qve "\<none\>"
394 # skip $dev since we don't allow unencrypted storage
398 if echo ${PERSISTENT_STORAGE} | grep -qe "\<filesystem\>"
400 for label in ${overlays} ${snapshots}
402 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
404 overlays=$(echo ${overlays} | sed -e "s|\<${label}\>||")
405 snapshots=$(echo ${snapshots} | sed -e "s|\<${label}\>||")
406 echo "${label}=${dev}"
407 # skip to the next device
413 if echo ${PERSISTENT_STORAGE} | grep -qe "\<file\>"
415 devfstype="$(get_fstype ${dev})"
418 backing="/$(basename ${dev})-backing"
419 mkdir -p "${backing}"
420 if is_supported_fs ${devfstype} && try_mount "${dev}" "${backing}" "rw" "${devfstype}"
422 for label in ${overlays}
424 path=${backing}/${PERSISTENT_PATH}${label}
427 overlays=$(echo ${overlays} | sed -e "s|\<${label}\>||")
429 echo "${label}=$(setup_loop "${path}" "loop" "/sys/block/loop*")"
433 for label in ${snapshots}
435 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
437 path="${PERSISTENT_PATH}${label}.${ext}"
438 if [ -f "${backing}/${path}" ]
440 snapshots=$(echo ${snapshots} | sed -e "s|\<${label}\>||")
441 snapshot_on_dev="yes"
442 echo "${label}=${dev}:${backing}:${path}"
447 if [ -z "${overlay_on_dev}" ]
449 umount ${backing} > /dev/null 2>&1 || true
450 if [ -z "${snapshot_on_dev}" ] && [ -n "${luks_device}" ] && /sbin/cryptsetup status "${luks_device}" 1> /dev/null
452 /sbin/cryptsetup luksClose "${luks_device}"
463 for adaptor in /sys/class/net/*
465 status="$(cat ${adaptor}/iflink)"
467 if [ "${status}" -eq 2 ]
469 mac="$(cat ${adaptor}/address)"
470 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
480 if [ -x /sbin/cryptsetup ]
482 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
496 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
499 if [ "$(cat ${sysblock}/removable)" = "1" ]
501 if [ -z "${want_usb}" ]
505 if readlink ${sysblock} | grep -q usb
512 if [ "${dev_ok}" = "yes" ]
514 case "${output_format}" in
516 ret="${ret} ${sysblock}"
519 devname=$(sys2dev "${sysblock}")
520 ret="${ret} ${devname}"
533 removable_dev "${output_format}" "want_usb"
541 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
543 if [ "$(cat ${sysblock}/removable)" = "0" ]
545 case "${output_format}" in
547 ret="${ret} ${sysblock}"
550 devname=$(sys2dev "${sysblock}")
551 ret="${ret} ${devname}"
562 # create source's directory structure in dest, and recursively
563 # create symlinks in dest to to all files in source. if mask
564 # is non-empty, remove mask from all source paths when
565 # creating links (will be necessary if we change root, which
566 # live-boot normally does (into $rootmnt)).
568 # remove multiple /:s and ensure ending on /
569 local src_dir="$(echo "${1}"/ | sed -e 's|/\+|/|g')"
570 local dest_dir="$(echo "${2}"/ | sed -e 's|/\+|/|g')"
571 local src_mask="${3}"
573 # This check can only trigger on the inital, non-recursive call since
574 # we create the destination before recursive calls
575 if [ ! -d "${dest_dir}" ];
577 log_warning_msg "Must link_files into a directory"
581 find "${src_dir}" -mindepth 1 -maxdepth 1 | while read x; do
583 local dest="${dest_dir}$(basename "${x}")"
586 if [ -z "$(ls -A "${src}")" ];
590 if [ ! -d "${dest}" ];
593 prev="$(dirname "${dest}")"
594 chown $(stat -c %u:%g "${prev}") "${dest}"
595 chmod $(stat -c %a "${prev}") "${dest}"
597 link_files "${src}" "${dest}" "${src_mask}"
599 if [ -n "${src_mask}" ]
601 src="$(echo ${src} | sed "s|^${src_mask}||")"
603 rm -rf "${dest}" 2> /dev/null
604 ln -s "${src}" "${dest}"
610 local unionmountpoint="${1}" # directory where the union is mounted
611 local unionrw="${2}" # branch where the union changes are stored
612 local unionro1="${3}" # first underlying read-only branch (optional)
613 local unionro2="${4}" # second underlying read-only branch (optional)
615 if [ "${UNIONTYPE}" = "aufs" ]
620 elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
629 case "${UNIONTYPE}" in
631 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
632 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
633 if [ -n "${unionro1}" ]
635 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
637 if [ -n "${unionro2}" ]
639 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
641 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
642 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
643 ( mkdir -p /run/sendsigs.omit.d
644 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
648 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
649 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
650 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
651 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
655 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
656 if [ -n "${unionro1}" ]
658 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
660 if [ -n "${unionro2}" ]
662 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
664 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
669 get_custom_mounts () {
670 # Side-effect: leaves $devices with live.persist mounted in ${rootmnt}/live/persistent
671 # Side-effect: prints info to file $custom_mounts
674 local custom_mounts="${2}" # print result to this file
675 local rootmnt="${3}" # should be set empty post-live-boot
677 local bindings="/bindings.list"
678 local links="/links.list"
679 rm -rf ${bindings} ${links} 2> /dev/null
680 local persistent_backing="${rootmnt}/live/persistent"
682 for device in ${devices}
684 if [ ! -b "${device}" ]
688 local device_name="$(basename ${device})"
689 local backing="${persistent_backing}/${device_name}"
690 mkdir -p "${backing}"
691 local device_fstype="$(get_fstype ${device})"
692 if [ -z "${PERSISTENT_READONLY}" ]
694 device_mount_opts="rw,noatime"
696 device_mount_opts="ro,noatime"
699 mount -t "${device_fstype}" -o "${device_mount_opts}" "${device}" "${backing}"
700 local include_list="${backing}/${persistence_list}"
701 if [ ! -r "${include_list}" ]
708 [ "${DEBUG}" = "Yes" ] && cp ${include_list} ${persistent_backing}/${persistence_list}.${device_name}
709 while read source dest options # < ${include_list}
711 if echo ${source} | grep -qe "^[[:space:]]*\(#.*\)\?$"
713 # skipping empty or commented lines
717 if echo ${dest} | grep -qe "^[^/]"
721 elif [ -z "${dest}" ]
726 if echo ${dest} | grep -qe "^/\+$\|^/\+live\(/.*\)\?$"
728 # mounting on / or /live could cause trouble
729 log_warning_msg "Skipping unsafe custom mount on ${dest}"
733 for opt in $(echo ${options} | tr ',' ' ');
739 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
745 # FIXME: handle case: we already have /a/b in
746 # $bindings added from current $device, but
747 # now we find /a -- /a should replace /a/b in
750 # FIXME: handle case: we have /a in $bindings
751 # from current $device, now we find /a/b, so
754 # ensure that no multiple-/ occur in paths
755 local full_source="$(echo ${backing}/${source}/ | sed -e 's|/\+|/|g')"
756 local full_dest="$(echo ${rootmnt}/${dest}/ | sed -e 's|/\+|/|g')"
758 if echo ${options} | grep -qe "\<linkfiles\>";
760 echo "${full_source} ${full_dest} ${options}" >> ${links}
762 echo "${full_source} ${full_dest} ${options}" >> ${bindings}
764 done < ${include_list}
766 if [ -z "${device_used}" ]
768 # this device was not used for / earlier, or
769 # custom mount point now, so it's useless
775 # We sort the list according to destination so we're sure that
776 # we won't hide a previous mount. We also ignore duplicate
777 # destinations in a more or less arbitrary way.
778 [ -e "${bindings}" ] && sort -k2 -sbu ${bindings} >> ${custom_mounts}
781 # After all mounts are considered we add symlinks so they
782 # won't be hidden by some mount.
783 [ -e "${links}" ] && sort -k2 -sbu ${links} >> ${custom_mounts}
786 rm -f ${bindings} ${links} 2> /dev/null
787 echo ${custom_mounts}
790 do_custom_mounts () {
791 local custom_mounts="${1}" # the ouput from get_custom_mounts()
792 local rootmnt="${2}" # should be set empty post-live-boot
794 while read source dest options # < ${custom_mounts}
796 local opt_linkfiles=""
798 for opt in $(echo ${options} | tr ',' ' ');
810 if mountpoint -q "${dest}";
812 log_warning_msg "Skipping custom mount ${source} on ${dest}: destination is already a mount point"
816 # FIXME: we don't handle already existing
817 # non-directory files in the paths of both $source and
820 if [ ! -d "${dest}" ]
822 # if ${dest} is in /home/$user, try fixing
824 # FIXME: this should really be handled by
825 # live-config since we don't know for sure
826 # which uid a certain user has until then
827 if echo ${dest} | grep -qe "^${rootmnt}/*home/\+[^/]\+"
830 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
836 # assume that the intended user is the first, which is usually the case
837 chown 1000:1000 ${path}
845 # if ${source} doesn't exist on our persistent media
846 # we bootstrap it with $dest from the live filesystem.
847 # this both makes sense and is critical if we're
848 # dealing with /etc or other system dir.
849 if [ ! -d "${source}" ]
851 if [ -n "${PERSISTENT_READONLY}" ] || [ -n "${opt_linkfiles}" ]
854 elif [ -n "${opt_union}" ]
856 # union's don't need to be bootstrapped
859 # ensure that $dest is not copied *into* $source
860 mkdir -p "$(dirname ${source})"
861 cp -a "${dest}" "${source}"
866 for d in ${rootmnt}/live/rofs/*
868 if [ -n "${rootmnt}" ]
870 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
872 rofs_dest_backing="${d}/${dest}"
875 if [ -d "${rofs_dest_backing}" ]
883 if [ -z "${PERSISTENT_READONLY}" ]
885 if [ -n "${opt_linkfiles}" ]
887 links_source="${source}"
889 elif [ -n "${opt_union}" ]
891 do_union ${dest} ${source} ${rofs_dest_backing}
893 mount --bind "${source}" "${dest}"
896 if [ -n "${opt_linkfiles}" ]
899 dest="$(mktemp -d ${persistent_backing}/links_source-XXXXXX)"
900 links_source="${dest}"
902 if [ -n "${rootmnt}" ]
904 cow_dir="$(echo ${dest} | sed -e "s|${rootmnt}|/cow/|")"
906 cow_dir="/live/cow/${dest}"
909 do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
912 if [ -n "${opt_linkfiles}" ]
914 link_files "${links_source}" "${links_dest}" "${rootmnt}"
917 PERSISTENCE_IS_ON="1"
918 export PERSISTENCE_IS_ON
919 done < ${custom_mounts}