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
82 # fstype misreports LUKS devices
85 /lib/udev/vol_id -t ${1} 2>/dev/null
89 eval $(fstype ${1} 2>/dev/null)
91 if [ "${FSTYPE}" != "unknown" ]
97 /lib/udev/vol_id -t ${1} 2>/dev/null
104 if grep -q "^${device} " /proc/mounts
106 # return the first found
107 grep -m1 "^${device} " /proc/mounts | cut -f2 -d ' '
124 mounts="$(awk '{print $2}' /proc/mounts)"
125 testpath="$(busybox realpath ${testpath})"
129 if echo "${mounts}" | grep -qs "^${testpath}"
131 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
135 testpath=$(dirname $testpath)
142 # Returns used/free fs kbytes + 5% more
143 # You could pass a block device as ${1} or the mount point as ${2}
149 if [ -z "${mountp}" ]
151 mountp="$(where_is_mounted ${dev})"
153 if [ -z "${mountp}" ]
155 mountp="/mnt/tmp_fs_size"
158 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
164 if [ "${used}" = "used" ]
166 size=$(du -ks ${mountp} | cut -f1)
167 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
170 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
173 if [ -n "${doumount}" ]
175 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
185 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
187 loadkeys /etc/boottime.kmap.gz
197 local encryption=${5}
200 modprobe -q -b "${module}"
202 if [ -x /sbin/udevadm ]
211 for loopdev in ${pattern}
213 if [ "$(cat ${loopdev}/size)" -eq 0 ]
215 dev=$(sys2dev "${loopdev}")
218 if [ -n "${readonly}" ]
220 if losetup --help 2>&1 | grep -q -- "-r\b"
222 options="${options} -r"
226 if [ 0 -lt "${offset}" ]
228 options="${options} -o ${offset}"
231 if [ -z "${encryption}" ]
233 losetup ${options} "${dev}" "${fspath}"
235 # Loop AES encryption
240 echo -n "Enter passphrase for root filesystem: " >&6
242 echo "${passphrase}" > /tmp/passphrase
244 exec 9</tmp/passphrase
245 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
248 rm -f /tmp/passphrase
250 if [ 0 -eq ${error} ]
257 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
260 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
273 panic "No loop devices available"
283 old_mountp="$(where_is_mounted ${dev})"
285 if [ -n "${old_mountp}" ]
287 if [ "${opts}" != "ro" ]
289 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
292 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
294 if [ -z "${fstype}" ]
296 fstype=$(get_fstype "${dev}")
298 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}"
304 # Returns a device containing a partition labeled "${pers_label}" or containing a file named the same way
305 # in the latter case the partition containing the file is left mounted
306 # if is not in black_listed_devices
308 cow_backing="/${pers_label}-backing"
309 black_listed_devices="${2}"
311 if [ -z "${PERSISTENT_PATH}" ]
313 pers_fpath=${cow_backing}/${pers_label}
315 pers_fpath=${cow_backing}/${PERSISTENT_PATH}/${pers_label}
318 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
320 for dev in $(subdevices "${sysblock}")
322 devname=$(sys2dev "${dev}")
324 if echo "${black_listed_devices}" | grep -q "${devname}"
326 # skip this device enterely
330 # Checking for a luks device
331 if [ -e /sbin/cryptsetup ] && /sbin/cryptsetup isLuks ${devname}
337 echo -n "Enter passphrase for ${pers_label} on ${devname}: " >&6
339 echo "${passphrase}" > /tmp/passphrase
341 exec 9</tmp/passphrase
342 /sbin/cryptsetup luksOpen ${devname} $(basename ${devname})
345 rm -f /tmp/passphrase
347 devname="/dev/mapper/$(basename ${devname})"
349 if [ 0 -eq ${error} ]
356 echo -n "There was an error decrypting ${devname} ... Retry? [Y/n] " >&6
359 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
367 if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
373 if [ "${PERSISTENT}" = "nofiles" ]
375 # do not mount the device to find for image files
380 case "$(get_fstype ${devname})" in
381 vfat|ext2|ext3|ext4|jffs2)
382 mkdir -p "${cow_backing}"
383 if ! try_mount "${devname}" "${cow_backing}" "rw"
388 if [ -f "${pers_fpath}" ]
390 echo $(setup_loop "${pers_fpath}" "loop" "/sys/block/loop*")
393 umount ${cow_backing}
406 # return the a string composed by device name, mountpoint an the first of ${filenames} found on a supported partition
407 # FIXME: merge with above function
410 snap_backing="/snap-backing"
411 black_listed_devices="${2}"
413 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
415 for dev in $(subdevices "${sysblock}")
417 devname=$(sys2dev "${dev}")
418 devfstype="$(get_fstype ${devname})"
420 if echo "${black_listed_devices}" | grep -q "${devname}"
422 # skip this device enterely
426 if is_supported_fs ${devfstype}
428 mkdir -p "${snap_backing}"
430 if try_mount "${devname}" "${snap_backing}" "ro" "${devfstype}"
432 for filename in ${filenames}
434 if [ -f "${snap_backing}/${filename}" ]
436 echo "${devname} ${snap_backing} ${filename}"
437 umount ${snap_backing}
443 umount ${snap_backing}
453 for adaptor in /sys/class/net/*
455 status="$(cat ${adaptor}/iflink)"
457 if [ "${status}" -eq 2 ]
459 mac="$(cat ${adaptor}/address)"
460 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
470 if [ -x /sbin/cryptsetup ]
472 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}