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 # udev (>= 146) no longer provides vol_id
80 if [ -x /lib/udev/vol_id ]
83 /lib/udev/vol_id -t ${1} 2>/dev/null
86 /sbin/blkid -s TYPE -o value $1 2>/dev/null
94 if grep -q "^${device} " /proc/mounts
96 # return the first found
97 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
114 mounts="$(awk '{print $2}' /proc/mounts)"
115 testpath="$(busybox realpath ${testpath})"
119 if echo "${mounts}" | grep -qs "^${testpath}"
121 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
125 testpath=$(dirname $testpath)
132 # Returns used/free fs kbytes + 5% more
133 # You could pass a block device as ${1} or the mount point as ${2}
139 if [ -z "${mountp}" ]
141 mountp="$(where_is_mounted ${dev})"
143 if [ -z "${mountp}" ]
145 mountp="/mnt/tmp_fs_size"
148 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
154 if [ "${used}" = "used" ]
156 size=$(du -ks ${mountp} | cut -f1)
157 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
160 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
163 if [ -n "${doumount}" ]
165 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
175 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
177 loadkeys /etc/boottime.kmap.gz
187 local encryption=${5}
190 # the output of setup_loop is evaluated in other functions,
191 # modprobe leaks kernel options like "libata.dma=0"
192 # as "options libata dma=0" on stdout, causing serious
193 # problems therefor, so instead always avoid output to stdout
194 modprobe -q -b "${module}" 1>/dev/null
198 for loopdev in ${pattern}
200 if [ "$(cat ${loopdev}/size)" -eq 0 ]
202 dev=$(sys2dev "${loopdev}")
205 if [ -n "${readonly}" ]
207 if losetup --help 2>&1 | grep -q -- "-r\b"
209 options="${options} -r"
213 if [ 0 -lt "${offset}" ]
215 options="${options} -o ${offset}"
218 if [ -z "${encryption}" ]
220 losetup ${options} "${dev}" "${fspath}"
222 # Loop AES encryption
227 echo -n "Enter passphrase for root filesystem: " >&6
229 echo "${passphrase}" > /tmp/passphrase
231 exec 9</tmp/passphrase
232 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
235 rm -f /tmp/passphrase
237 if [ 0 -eq ${error} ]
244 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
247 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
260 panic "No loop devices available"
270 old_mountp="$(where_is_mounted ${dev})"
272 if [ -n "${old_mountp}" ]
274 if [ "${opts}" != "ro" ]
276 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
279 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
281 if [ -z "${fstype}" ]
283 fstype=$(get_fstype "${dev}")
285 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
286 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > live.log && return 0 )
292 # Returns a device containing a partition labeled "${pers_label}" or containing a file named the same way
293 # in the latter case the partition containing the file is left mounted
294 # if is not in black_listed_devices
296 cow_backing="/${pers_label}-backing"
297 black_listed_devices="${2}"
299 if [ -z "${PERSISTENT_PATH}" ]
301 pers_fpath=${cow_backing}/${pers_label}
303 pers_fpath=${cow_backing}/${PERSISTENT_PATH}/${pers_label}
306 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
308 for dev in $(subdevices "${sysblock}")
310 devname=$(sys2dev "${dev}")
312 if echo "${black_listed_devices}" | grep -q "${devname}"
314 # skip this device enterely
318 # Checking for a luks device
319 if [ "${PERSISTENT}" = "cryptsetup" ] && [ -e /sbin/cryptsetup ] && /sbin/cryptsetup isLuks ${devname}
325 /lib/cryptsetup/askpass "Enter passphrase for ${pers_label} on ${devname}: " | /sbin/cryptsetup -T 1 luksOpen ${devname} $(basename ${devname}) --key-file=-
328 devname="/dev/mapper/$(basename ${devname})"
330 if [ 0 -eq ${error} ]
337 echo -n "There was an error decrypting ${devname} ... Retry? [Y/n] " >&6
340 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
348 # udev (>= 146) no longer provides vol_id
349 if [ -x /lib/udev/vol_id ]
352 if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
359 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 # FIXME: merge with above function
401 snap_backing="/snap-backing"
402 black_listed_devices="${2}"
404 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
406 for dev in $(subdevices "${sysblock}")
408 devname=$(sys2dev "${dev}")
409 devfstype="$(get_fstype ${devname})"
411 if echo "${black_listed_devices}" | grep -q "${devname}"
413 # skip this device enterely
417 if is_supported_fs ${devfstype}
419 mkdir -p "${snap_backing}"
421 if try_mount "${devname}" "${snap_backing}" "ro" "${devfstype}"
423 for filename in ${filenames}
425 if [ -f "${snap_backing}/${filename}" ]
427 echo "${devname} ${snap_backing} ${filename}"
428 umount ${snap_backing}
434 umount ${snap_backing}
444 for adaptor in /sys/class/net/*
446 status="$(cat ${adaptor}/iflink)"
448 if [ "${status}" -eq 2 ]
450 mac="$(cat ${adaptor}/address)"
451 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
461 if [ -x /sbin/cryptsetup ]
463 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}