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 -qw "${fulldevname}" || \
50 [ -n "${white_listed_devices}" ] && \
51 echo "${white_listed_devices}" | grep -vqw "${fulldevname}"
53 # skip this device entirely
57 for dev in $(subdevices "${sysblock}")
59 devname=$(sys2dev "${dev}")
61 if echo "${black_listed_devices}" | grep -qw "${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})"
324 /lib/cryptsetup/askpass "Enter passphrase for ${dev}: " | \
325 /sbin/cryptsetup -T 1 luksOpen ${dev} ${name} ${opts}
329 luks_device="/dev/mapper/${name}"
335 echo -n "There was an error decrypting ${dev} ... Retry? [Y/n] " >&6
338 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
345 find_persistent_media ()
347 # Scans devices for overlays and snapshots, and returns a whitespace
348 # separated list of how to use them. Only overlays with a partition
349 # label or file name in ${overlays} are returned, and ditto for
350 # snapshots with labels in ${snapshots}.
352 # When scanning a LUKS device, the user will be asked to enter the
353 # passphrase; on failure to enter it, or if no persistent partitions
354 # or files were found, the LUKS device is closed.
356 # For a snapshot file the return value is ${label}=${snapdata}", where
357 # ${snapdata} is the parameter used for try_snap().
359 # For all other cases (overlay/snapshot partition and overlay file) the
360 # return value is "${label}=${device}", where ${device} a device that
361 # can mount the content. In the case of an overlay file, the device
362 # containing the file will remain mounted as a side-effect.
364 # No devices in ${black_listed_devices} will be scanned, and if
365 # ${white_list_devices} is non-empty, only devices in it will be
370 black_listed_devices="${3}"
371 white_listed_devices="${4}"
373 for dev in $(storage_devices "${black_listed_devices}" "${white_listed_devices}")
377 # Checking for a luks device
378 if echo ${PERSISTENT_ENCRYPTION} | grep -qw luks && \
379 /sbin/cryptsetup isLuks ${dev}
381 if luks_device=$(open_luks_device "${dev}")
385 # skip $dev since we failed/chose not to open it
388 elif echo ${PERSISTENT_ENCRYPTION} | grep -qwv none
390 # skip $dev since we don't allow unencrypted storage
394 if echo ${PERSISTENT_STORAGE} | grep -qw filesystem
396 for label in ${overlays} ${snapshots}
398 if [ "$(/sbin/blkid -s LABEL -o value $dev 2>/dev/null)" = "${label}" ]
400 overlays=$(echo ${overlays} | sed -e "s|\<${label}\>||")
401 snapshots=$(echo ${snapshots} | sed -e "s|\<${label}\>||")
402 echo "${label}=${dev}"
403 # skip to the next device
409 if echo ${PERSISTENT_STORAGE} | grep -qw file
411 devfstype="$(get_fstype ${dev})"
414 backing="/$(basename ${dev})-backing"
415 mkdir -p "${backing}"
416 if is_supported_fs ${devfstype} && try_mount "${dev}" "${backing}" "rw" "${devfstype}"
418 for label in ${overlays}
420 path=${backing}/${PERSISTENT_PATH}${label}
423 overlays=$(echo ${overlays} | sed -e "s|\<${label}\>||")
425 echo "${label}=$(setup_loop "${path}" "loop" "/sys/block/loop*")"
429 for label in ${snapshots}
431 for ext in squashfs cpio.gz ext2 ext3 ext4 jffs2
433 path="${PERSISTENT_PATH}${label}.${ext}"
434 if [ -f "${backing}/${path}" ]
436 snapshots=$(echo ${snapshots} | sed -e "s|\<${label}\>||")
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}"