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}"
601 src="$(echo ${src} | sed "s|^${src_mask}||")"
603 rm -rf "${dest}" 2> /dev/null
604 ln -s "${src}" "${dest}"
613 unionmountpoint="${4}"
615 if [ "${UNIONTYPE}" = "aufs" ]
620 elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
629 case "${UNIONTYPE}" in
631 # FIXME: handle PERSISTENT_READONLY
632 unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
633 unionmountopts="${unionmountopts} ${unionrw}=${rw_opt}:${unionro}=${ro_opt}"
634 ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
635 unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
636 ( mkdir -p /run/sendsigs.omit.d
637 pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
641 # FIXME: is PERSISTENT_READONLY possible? (overlayfs only handles two dirs, but perhaps they can be chained?)
642 unionmountopts="-o noatime,lowerdir=${unionro},upperdir=${unionrw}"
643 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
647 if [ -n "${PERSISTENT_READONLY}" ]
649 mount -t tmpfs -o rw,noatime,mode=755 tmpfs "${unionrw}"
650 unionmountopts="-o noatime,${noxino_opt}dirs=${unionrw}=${rw_opt}:${root_backing}=${ro_opt}:${unionro}=${ro_opt}"
652 unionmountopts="-o noatime,${noxino_opt}dirs=${unionrw}=${rw_opt}:${unionro}=${ro_opt}"
654 mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"