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" ]
44 # Validate input first
50 # Try to look if it is already supported by the kernel
51 if grep -q ${fstype} /proc/filesystems
55 # Then try to add support for it the gentle way using the initramfs capabilities
57 if grep -q ${fstype} /proc/filesystems
60 # Then try the hard way if /root is already reachable
62 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
63 if [ -e "${kmodule}" ]
66 if grep -q ${fstype} /proc/filesystems
79 /sbin/blkid -s TYPE -o value $1 2>/dev/null
86 if grep -q "^${device} " /proc/mounts
88 # return the first found
89 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
106 mounts="$(awk '{print $2}' /proc/mounts)"
107 testpath="$(busybox realpath ${testpath})"
111 if echo "${mounts}" | grep -qs "^${testpath}"
113 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
117 testpath=$(dirname $testpath)
124 # Returns used/free fs kbytes + 5% more
125 # You could pass a block device as ${1} or the mount point as ${2}
131 if [ -z "${mountp}" ]
133 mountp="$(where_is_mounted ${dev})"
135 if [ -z "${mountp}" ]
137 mountp="/mnt/tmp_fs_size"
140 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
146 if [ "${used}" = "used" ]
148 size=$(du -ks ${mountp} | cut -f1)
149 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
152 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
155 if [ -n "${doumount}" ]
157 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
167 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
169 loadkeys /etc/boottime.kmap.gz
179 local encryption=${5}
182 # the output of setup_loop is evaluated in other functions,
183 # modprobe leaks kernel options like "libata.dma=0"
184 # as "options libata dma=0" on stdout, causing serious
185 # problems therefor, so instead always avoid output to stdout
186 modprobe -q -b "${module}" 1>/dev/null
190 for loopdev in ${pattern}
192 if [ "$(cat ${loopdev}/size)" -eq 0 ]
194 dev=$(sys2dev "${loopdev}")
197 if [ -n "${readonly}" ]
199 if losetup --help 2>&1 | grep -q -- "-r\b"
201 options="${options} -r"
205 if [ 0 -lt "${offset}" ]
207 options="${options} -o ${offset}"
210 if [ -z "${encryption}" ]
212 losetup ${options} "${dev}" "${fspath}"
214 # Loop AES encryption
219 echo -n "Enter passphrase for root filesystem: " >&6
221 echo "${passphrase}" > /tmp/passphrase
223 exec 9</tmp/passphrase
224 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
227 rm -f /tmp/passphrase
229 if [ 0 -eq ${error} ]
236 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
239 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
252 panic "No loop devices available"
262 old_mountp="$(where_is_mounted ${dev})"
264 if [ -n "${old_mountp}" ]
266 if [ "${opts}" != "ro" ]
268 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
271 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
273 if [ -z "${fstype}" ]
275 fstype=$(get_fstype "${dev}")
277 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
278 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > live-boot.log && return 0 )
284 # Returns a device containing a partition labeled "${pers_label}" or containing a file named the same way
285 # in the latter case the partition containing the file is left mounted
286 # if is not in black_listed_devices.
287 # Additionally, if the white_listed_devices list is non-empty, the
288 # parent block device of the returned device must be part of this list.
290 cow_backing="/${pers_label}-backing"
291 black_listed_devices="${2}"
292 white_listed_devices="${3}"
294 if [ -z "${PERSISTENT_PATH}" ]
296 pers_fpath=${cow_backing}/${pers_label}
298 pers_fpath=${cow_backing}/${PERSISTENT_PATH}/${pers_label}
301 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
303 fulldevname=$(sys2dev "${sysblock}")
305 if echo "${black_listed_devices}" | grep -q -w "${fulldevname}"
307 # skip this device entirely
311 if [ -n "${white_listed_devices}" ]
313 if echo "${white_listed_devices}" | grep -v -q -w "${fulldevname}"
315 # skip this device entirely
320 for dev in $(subdevices "${sysblock}")
322 devname=$(sys2dev "${dev}")
324 if echo "${black_listed_devices}" | grep -q -w "${devname}"
326 # skip this subdevice
330 # Checking for a luks device
331 if [ "${PERSISTENT}" = "cryptsetup" ] && [ -e /sbin/cryptsetup ] && /sbin/cryptsetup isLuks ${devname}
337 /lib/cryptsetup/askpass "Enter passphrase for ${pers_label} on ${devname}: " | /sbin/cryptsetup -T 1 luksOpen ${devname} $(basename ${devname}) --key-file=-
340 devname="/dev/mapper/$(basename ${devname})"
342 if [ 0 -eq ${error} ]
349 echo -n "There was an error decrypting ${devname} ... Retry? [Y/n] " >&6
352 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
360 if [ "$(/sbin/blkid -s LABEL -o value $devname 2>/dev/null)" = "${pers_label}" ]
366 if [ "${PERSISTENT}" = "nofiles" ]
368 # do not mount the device to find for image files
373 case "$(get_fstype ${devname})" in
374 vfat|ext2|ext3|ext4|jffs2)
375 mkdir -p "${cow_backing}"
376 if try_mount "${devname}" "${cow_backing}" "rw"
378 if [ -f "${pers_fpath}" ]
380 echo $(setup_loop "${pers_fpath}" "loop" "/sys/block/loop*")
383 umount ${cow_backing} > /dev/null 2>&1 || true
397 # return the a string composed by device name, mountpoint an the first of ${filenames} found on a supported partition
398 # if is not in black_listed_devices.
399 # Additionally, if the white_listed_devices list is non-empty, the
400 # parent block device of the returned device must be part of this list.
401 # FIXME: merge with above function
404 snap_backing="/snap-backing"
405 black_listed_devices="${2}"
406 white_listed_devices="${3}"
408 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
410 fulldevname=$(sys2dev "${sysblock}")
412 if echo "${black_listed_devices}" | grep -q -w "${fulldevname}"
414 # skip this device entirely
418 if [ -n "${white_listed_devices}" ]
420 if echo "${white_listed_devices}" | grep -v -q -w "${fulldevname}"
422 # skip this device entirely
427 for dev in $(subdevices "${sysblock}")
429 devname=$(sys2dev "${dev}")
430 devfstype="$(get_fstype ${devname})"
432 if echo "${black_listed_devices}" | grep -q -w "${devname}"
434 # skip this subdevice
438 if is_supported_fs ${devfstype}
440 mkdir -p "${snap_backing}"
442 if try_mount "${devname}" "${snap_backing}" "ro" "${devfstype}"
444 for filename in ${filenames}
446 if [ -f "${snap_backing}/${filename}" ]
448 echo "${devname} ${snap_backing} ${filename}"
449 umount ${snap_backing}
455 umount ${snap_backing}
465 for adaptor in /sys/class/net/*
467 status="$(cat ${adaptor}/iflink)"
469 if [ "${status}" -eq 2 ]
471 mac="$(cat ${adaptor}/address)"
472 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
482 if [ -x /sbin/cryptsetup ]
484 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
498 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
501 if [ "$(cat ${sysblock}/removable)" = "1" ]
503 if [ -z "${want_usb}" ]
507 if readlink ${sysblock} | grep -q usb
514 if [ "${dev_ok}" = "yes" ]
516 case "${output_format}" in
518 ret="${ret} ${sysblock}"
521 devname=$(sys2dev "${sysblock}")
522 ret="${ret} ${devname}"
535 removable_dev "${output_format}" "want_usb"
543 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|dm-|fd)")
545 if [ "$(cat ${sysblock}/removable)" = "0" ]
547 case "${output_format}" in
549 ret="${ret} ${sysblock}"
552 devname=$(sys2dev "${sysblock}")
553 ret="${ret} ${devname}"