1 # live-initramfs helper functions, used by live-initramfs 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-1 no longer provides vol_id:
80 if [ -x /lib/udev/vol_id ]
82 /lib/udev/vol_id -t ${1} 2>/dev/null
84 /sbin/blkid -s TYPE -o value $1 2>/dev/null
92 if grep -q "^${device} " /proc/mounts
94 # return the first found
95 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
112 mounts="$(awk '{print $2}' /proc/mounts)"
113 testpath="$(busybox realpath ${testpath})"
117 if echo "${mounts}" | grep -qs "^${testpath}"
119 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
123 testpath=$(dirname $testpath)
130 # Returns used/free fs kbytes + 5% more
131 # You could pass a block device as ${1} or the mount point as ${2}
137 if [ -z "${mountp}" ]
139 mountp="$(where_is_mounted ${dev})"
141 if [ -z "${mountp}" ]
143 mountp="/mnt/tmp_fs_size"
146 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
152 if [ "${used}" = "used" ]
154 size=$(du -ks ${mountp} | cut -f1)
155 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
158 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
161 if [ -n "${doumount}" ]
163 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
173 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
175 loadkeys /etc/boottime.kmap.gz
185 local encryption=${5}
188 modprobe -q -b "${module}"
192 for loopdev in ${pattern}
194 if [ "$(cat ${loopdev}/size)" -eq 0 ]
196 dev=$(sys2dev "${loopdev}")
199 if [ -n "${readonly}" ]
201 if losetup --help 2>&1 | grep -q -- "-r\b"
203 options="${options} -r"
207 if [ 0 -lt "${offset}" ]
209 options="${options} -o ${offset}"
212 if [ -z "${encryption}" ]
214 losetup ${options} "${dev}" "${fspath}"
216 # Loop AES encryption
221 echo -n "Enter passphrase for root filesystem: " >&6
223 echo "${passphrase}" > /tmp/passphrase
225 exec 9</tmp/passphrase
226 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
229 rm -f /tmp/passphrase
231 if [ 0 -eq ${error} ]
238 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
241 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
254 panic "No loop devices available"
264 old_mountp="$(where_is_mounted ${dev})"
266 if [ -n "${old_mountp}" ]
268 if [ "${opts}" != "ro" ]
270 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
273 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
275 if [ -z "${fstype}" ]
277 fstype=$(get_fstype "${dev}")
279 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || \
280 ( echo "SKIPPING: Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}" > live.log && return 0 )
286 # Returns a device containing a partition labeled "${pers_label}" or containing a file named the same way
287 # in the latter case the partition containing the file is left mounted
288 # if is not in black_listed_devices
290 cow_backing="/${pers_label}-backing"
291 black_listed_devices="${2}"
293 if [ -z "${PERSISTENT_PATH}" ]
295 pers_fpath=${cow_backing}/${pers_label}
297 pers_fpath=${cow_backing}/${PERSISTENT_PATH}/${pers_label}
300 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
302 for dev in $(subdevices "${sysblock}")
304 devname=$(sys2dev "${dev}")
306 if echo "${black_listed_devices}" | grep -q "${devname}"
308 # skip this device enterely
312 # Checking for a luks device
313 if [ "${PERSISTENT}" = "cryptsetup" ] && [ -e /sbin/cryptsetup ] && /sbin/cryptsetup isLuks ${devname}
319 /lib/cryptsetup/askpass "Enter passphrase for ${pers_label} on ${devname}: " | /sbin/cryptsetup -T 1 luksOpen ${devname} $(basename ${devname}) --key-file=-
322 devname="/dev/mapper/$(basename ${devname})"
324 if [ 0 -eq ${error} ]
331 echo -n "There was an error decrypting ${devname} ... Retry? [Y/n] " >&6
334 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
342 # udev >=146-1 no longer provides vol_id:
343 if [ -x /lib/udev/vol_id ]
345 if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
351 if [ "$(/sbin/blkid -s LABEL -o value $devname 2>/dev/null)" = "${pers_label}" ]
358 if [ "${PERSISTENT}" = "nofiles" ]
360 # do not mount the device to find for image files
365 case "$(get_fstype ${devname})" in
366 vfat|ext2|ext3|ext4|jffs2)
367 mkdir -p "${cow_backing}"
368 if try_mount "${devname}" "${cow_backing}" "rw"
370 if [ -f "${pers_fpath}" ]
372 echo $(setup_loop "${pers_fpath}" "loop" "/sys/block/loop*")
375 umount ${cow_backing} > /dev/null 2>&1 || true
389 # return the a string composed by device name, mountpoint an the first of ${filenames} found on a supported partition
390 # FIXME: merge with above function
393 snap_backing="/snap-backing"
394 black_listed_devices="${2}"
396 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
398 for dev in $(subdevices "${sysblock}")
400 devname=$(sys2dev "${dev}")
401 devfstype="$(get_fstype ${devname})"
403 if echo "${black_listed_devices}" | grep -q "${devname}"
405 # skip this device enterely
409 if is_supported_fs ${devfstype}
411 mkdir -p "${snap_backing}"
413 if try_mount "${devname}" "${snap_backing}" "ro" "${devfstype}"
415 for filename in ${filenames}
417 if [ -f "${snap_backing}/${filename}" ]
419 echo "${devname} ${snap_backing} ${filename}"
420 umount ${snap_backing}
426 umount ${snap_backing}
436 for adaptor in /sys/class/net/*
438 status="$(cat ${adaptor}/iflink)"
440 if [ "${status}" -eq 2 ]
442 mac="$(cat ${adaptor}/address)"
443 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
453 if [ -x /sbin/cryptsetup ]
455 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}