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 echo "${label}=${dev}"
405 # skip to the next device
411 if echo ${PERSISTENT_STORAGE} | grep -qe "\<file\>"
413 devfstype="$(get_fstype ${dev})"
416 backing="/$(basename ${dev})-backing"
417 mkdir -p "${backing}"
418 if is_supported_fs ${devfstype} && try_mount "${dev}" "${backing}" "rw" "${devfstype}"
420 for label in ${overlays}
422 path=${backing}/${PERSISTENT_PATH}${label}
426 echo "${label}=$(setup_loop "${path}" "loop" "/sys/block/loop*")"
430 for label in ${snapshots}
432 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
434 path="${PERSISTENT_PATH}${label}.${ext}"
435 if [ -f "${backing}/${path}" ]
437 snapshot_on_dev="yes"
438 echo "${label}=${dev}:${backing}:${path}"
443 if [ -z "${overlay_on_dev}" ]
445 umount ${backing} > /dev/null 2>&1 || true
446 if [ -z "${snapshot_on_dev}" ] && [ -n "${luks_device}" ] && /sbin/cryptsetup status "${luks_device}" 1> /dev/null
448 /sbin/cryptsetup luksClose "${luks_device}"
459 for adaptor in /sys/class/net/*
461 status="$(cat ${adaptor}/iflink)"
463 if [ "${status}" -eq 2 ]
465 mac="$(cat ${adaptor}/address)"
466 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
476 if [ -x /sbin/cryptsetup ]
478 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
492 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
495 if [ "$(cat ${sysblock}/removable)" = "1" ]
497 if [ -z "${want_usb}" ]
501 if readlink ${sysblock} | grep -q usb
508 if [ "${dev_ok}" = "yes" ]
510 case "${output_format}" in
512 ret="${ret} ${sysblock}"
515 devname=$(sys2dev "${sysblock}")
516 ret="${ret} ${devname}"
529 removable_dev "${output_format}" "want_usb"
537 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
539 if [ "$(cat ${sysblock}/removable)" = "0" ]
541 case "${output_format}" in
543 ret="${ret} ${sysblock}"
546 devname=$(sys2dev "${sysblock}")
547 ret="${ret} ${devname}"
558 # create source's directory structure in dest, and recursively
559 # create symlinks in dest to to all files in source. if mask
560 # is non-empty, remove mask from all source paths when
561 # creating links (will be necessary if we change root, which
562 # live-boot normally does (into $rootmnt)).
564 # remove multiple /:s and ensure ending on /
565 local src_dir="$(echo "${1}"/ | sed -e 's|/\+|/|g')"
566 local dest_dir="$(echo "${2}"/ | sed -e 's|/\+|/|g')"
567 local src_mask="${3}"
569 # This check can only trigger on the inital, non-recursive call since
570 # we create the destination before recursive calls
571 if [ ! -d "${dest_dir}" ];
573 log_warning_msg "Must link_files into a directory"
577 find "${src_dir}" -mindepth 1 -maxdepth 1 | while read x; do
579 local dest="${dest_dir}$(basename "${x}")"
582 if [ -z "$(ls -A "${src}")" ];
586 if [ ! -d "${dest}" ];
589 prev="$(dirname "${dest}")"
590 chown $(stat -c %u:%g "${prev}") "${dest}"
591 chmod $(stat -c %a "${prev}") "${dest}"
593 link_files "${src}" "${dest}" "${src_mask}"
595 if [ -n "${src_mask}" ]
597 src="$(echo ${src} | sed "s|^${src_mask}||")"
599 rm -rf "${dest}" 2> /dev/null
600 ln -s "${src}" "${dest}"
606 local unionmountpoint="${1}" # directory where the union is mounted
607 local unionrw="${2}" # branch where the union changes are stored
608 local unionro1="${3}" # first underlying read-only branch (optional)
609 local unionro2="${4}" # second underlying read-only branch (optional)
611 if [ "${UNIONTYPE}" = "aufs" ]
616 elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
625 case "${UNIONTYPE}" in
627 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
628 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}"
629 if [ -n "${unionro1}" ]
631 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
633 if [ -n "${unionro2}" ]
635 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
637 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
638 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
639 ( mkdir -p /run/sendsigs.omit.d
640 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
644 # XXX: can unionro2 be used? (overlayfs only handles two dirs, but perhaps they can be chained?)
645 # XXX: and can unionro1 be optional? i.e. can overlayfs skip lowerdir?
646 unionmountopts="-o noatime,lowerdir=${unionro1},upperdir=${unionrw}"
647 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
651 unionmountopts="-o noatime,${noxino_opt},dirs=${unionrw}=${rw_opt}"
652 if [ -n "${unionro1}" ]
654 unionmountopts="${unionmountopts}:${unionro1}=${ro_opt}"
656 if [ -n "${unionro2}" ]
658 unionmountopts="${unionmountopts}:${unionro2}=${ro_opt}"
660 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
665 get_custom_mounts () {
666 # Side-effect: leaves $devices with live.persist mounted in ${rootmnt}/live/persistent
667 # Side-effect: prints info to file $custom_mounts
670 local custom_mounts="${2}" # print result to this file
671 local rootmnt="${3}" # should be set empty post-live-boot
673 local bindings="/bindings.list"
674 local links="/links.list"
675 rm -rf ${bindings} ${links} 2> /dev/null
676 local persistent_backing="${rootmnt}/live/persistent"
678 for device in ${devices}
680 if [ ! -b "${device}" ]
684 local device_name="$(basename ${device})"
685 local backing="${persistent_backing}/${device_name}"
686 mkdir -p "${backing}"
687 local device_fstype="$(get_fstype ${device})"
688 if [ -z "${PERSISTENT_READONLY}" ]
690 device_mount_opts="rw,noatime"
692 device_mount_opts="ro,noatime"
695 mount -t "${device_fstype}" -o "${device_mount_opts}" "${device}" "${backing}"
696 local include_list="${backing}/${persistence_list}"
697 if [ ! -r "${include_list}" ]
704 [ "${DEBUG}" = "Yes" ] && cp ${include_list} ${persistent_backing}/${persistence_list}.${device_name}
705 while read source dest options # < ${include_list}
707 if echo ${source} | grep -qe "^[[:space:]]*\(#.*\)\?$"
709 # skipping empty or commented lines
713 if echo ${dest} | grep -qe "^[^/]"
717 elif [ -z "${dest}" ]
722 if echo ${dest} | grep -qe "^/\+$\|^/\+live\(/.*\)\?$"
724 # mounting on / or /live could cause trouble
725 log_warning_msg "Skipping unsafe custom mount on ${dest}"
729 for opt in $(echo ${options} | tr ',' ' ');
735 log_warning_msg "Skipping custom mount with unkown option: ${opt}"
741 # FIXME: handle case: we already have /a/b in
742 # $bindings added from current $device, but
743 # now we find /a -- /a should replace /a/b in
746 # FIXME: handle case: we have /a in $bindings
747 # from current $device, now we find /a/b, so
750 # ensure that no multiple-/ occur in paths
751 local full_source="$(echo ${backing}/${source}/ | sed -e 's|/\+|/|g')"
752 local full_dest="$(echo ${rootmnt}/${dest}/ | sed -e 's|/\+|/|g')"
754 if echo ${options} | grep -qe "\<linkfiles\>";
756 echo "${full_source} ${full_dest} ${options}" >> ${links}
758 echo "${full_source} ${full_dest} ${options}" >> ${bindings}
760 done < ${include_list}
762 if [ -z "${device_used}" ]
764 # this device was not used for / earlier, or
765 # custom mount point now, so it's useless
771 # We sort the list according to destination so we're sure that
772 # we won't hide a previous mount. We also ignore duplicate
773 # destinations in a more or less arbitrary way.
774 [ -e "${bindings}" ] && sort -k2 -sbu ${bindings} >> ${custom_mounts}
777 # After all mounts are considered we add symlinks so they
778 # won't be hidden by some mount.
779 [ -e "${links}" ] && sort -k2 -sbu ${links} >> ${custom_mounts}
782 rm -f ${bindings} ${links} 2> /dev/null
783 echo ${custom_mounts}
786 do_custom_mounts () {
787 local custom_mounts="${1}" # the ouput from get_custom_mounts()
788 local rootmnt="${2}" # should be set empty post-live-boot
790 while read source dest options # < ${custom_mounts}
792 local opt_linkfiles=""
794 for opt in $(echo ${options} | tr ',' ' ');
806 if mountpoint -q "${dest}";
808 log_warning_msg "Skipping custom mount ${source} on ${dest}: destination is already a mount point"
812 # FIXME: we don't handle already existing
813 # non-directory files in the paths of both $source and
816 if [ ! -d "${dest}" ]
818 # if ${dest} is in /home/$user, try fixing
820 # FIXME: this should really be handled by
821 # live-config since we don't know for sure
822 # which uid a certain user has until then
823 if echo ${dest} | grep -qe "^${rootmnt}/*home/\+[^/]\+"
826 for dir in $(echo ${dest} | sed -e 's|/\+| |g')
832 # assume that the intended user is the first, which is usually the case
833 chown 1000:1000 ${path}
841 # if ${source} doesn't exist on our persistent media
842 # we bootstrap it with $dest from the live filesystem.
843 # this both makes sense and is critical if we're
844 # dealing with /etc or other system dir.
845 if [ ! -d "${source}" ]
847 if [ -n "${PERSISTENT_READONLY}" ] || [ -n "${opt_linkfiles}" ]
850 elif [ -n "${opt_union}" ]
852 # union's don't need to be bootstrapped
855 # ensure that $dest is not copied *into* $source
856 mkdir -p "$(dirname ${source})"
857 cp -a "${dest}" "${source}"
862 for d in ${rootmnt}/live/rofs/*
864 if [ -n "${rootmnt}" ]
866 rofs_dest_backing="${d}/$(echo ${dest} | sed -e "s|${rootmnt}||")"
868 rofs_dest_backing="${d}/${dest}"
871 if [ -d "${rofs_dest_backing}" ]
879 if [ -z "${PERSISTENT_READONLY}" ]
881 if [ -n "${opt_linkfiles}" ]
883 links_source="${source}"
885 elif [ -n "${opt_union}" ]
887 do_union ${dest} ${source} ${rofs_dest_backing}
889 mount --bind "${source}" "${dest}"
892 if [ -n "${opt_linkfiles}" ]
895 dest="$(mktemp -d ${persistent_backing}/links_source-XXXXXX)"
896 links_source="${dest}"
898 if [ -n "${rootmnt}" ]
900 cow_dir="$(echo ${dest} | sed -e "s|${rootmnt}|/cow/|")"
902 cow_dir="/live/cow/${dest}"
905 do_union ${dest} ${cow_dir} ${source} ${rofs_dest_backing}
908 if [ -n "${opt_linkfiles}" ]
910 link_files "${links_source}" "${links_dest}" "${rootmnt}"
913 PERSISTENCE_IS_ON="1"
914 export PERSISTENCE_IS_ON
915 done < ${custom_mounts}