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"
12 echo "/dev/$(udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
20 for dev in "${sysblock}" "${sysblock}"/*
22 if [ -e "${dev}/dev" ]
35 # Validate input first
41 # Try to look if it is already supported by the kernel
42 if grep -q ${fstype} /proc/filesystems
46 # Then try to add support for it the gentle way using the initramfs capabilities
48 if grep -q ${fstype} /proc/filesystems
51 # Then try the hard way if /root is already reachable
53 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
54 if [ -e "${kmodule}" ]
57 if grep -q ${fstype} /proc/filesystems
73 # fstype misreports LUKS devices
76 /lib/udev/vol_id -t ${1} 2>/dev/null
80 eval $(fstype ${1} 2>/dev/null)
82 if [ "${FSTYPE}" != "unknown" ]
88 /lib/udev/vol_id -t ${1} 2>/dev/null
95 if grep -q "^${device} " /proc/mounts
97 # return the first found
98 grep "^${device} " /proc/mounts | cut -f2 -d ' '
115 mounts="$(awk '{print $2}' /proc/mounts)"
116 testpath="$(busybox realpath ${testpath})"
120 if echo "${mounts}" | grep -qs "^${testpath}"
122 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
126 testpath=$(dirname $testpath)
133 # Returns used/free fs kbytes + 5% more
134 # You could pass a block device as ${1} or the mount point as ${2}
140 if [ -z "${mountp}" ]
142 mountp="$(where_is_mounted ${dev})"
144 if [ -z "${mountp}" ]
146 mountp="/mnt/tmp_fs_size"
149 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}" || log_warning_msg "cannot mount -t $(get_fstype ${dev}) -o ro ${dev} ${mountp}"
155 if [ "${used}" = "used" ]
157 size=$(du -ks ${mountp} | cut -f1)
158 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
161 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
164 if [ -n "${doumount}" ]
166 umount "${mountp}" || log_warning_msg "cannot umount ${mountp}"
176 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
178 loadkeys /etc/boottime.kmap.gz
188 local encryption=${5}
191 modprobe -q -b "${module}"
193 if [ -x /sbin/udevadm ]
202 for loopdev in ${pattern}
204 if [ "$(cat ${loopdev}/size)" -eq 0 ]
206 dev=$(sys2dev "${loopdev}")
209 if [ -n "${readonly}" ]
211 if losetup --help 2>&1 | grep -q -- "-r\b"
213 options="${options} -r"
217 if [ 0 -lt "${offset}" ]
219 options="${options} -o ${offset}"
222 if [ -z "${encryption}" ]
224 losetup ${options} "${dev}" "${fspath}"
226 # Loop AES encryption
231 echo -n "Enter passphrase for root filesystem: " >&6
233 echo "${passphrase}" > /tmp/passphrase
235 exec 9</tmp/passphrase
236 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
239 rm -f /tmp/passphrase
241 if [ 0 -eq ${error} ]
248 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
251 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
264 panic "No loop devices available"
274 old_mountp="$(where_is_mounted ${dev})"
276 if [ -n "${old_mountp}" ]
278 if [ "${opts}" != "ro" ]
280 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
283 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
285 if [ -z "${fstype}" ]
287 fstype=$(get_fstype "${dev}")
289 mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}"
295 # Returns a device containing a partition labeled "${pers_label}" or containing a file named the same way
296 # in the latter case the partition containing the file is left mounted
297 # if is not in black_listed_devices
299 cow_backing="/${pers_label}-backing"
300 black_listed_devices="${2}"
302 if [ -z "${PERSISTENT_PATH}" ]
304 pers_fpath=${cow_backing}/${pers_label}
306 pers_fpath=${cow_backing}/${PERSISTENT_PATH}/${pers_label}
309 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
311 for dev in $(subdevices "${sysblock}")
313 devname=$(sys2dev "${dev}")
315 if echo "${black_listed_devices}" | grep -q "${devname}"
317 # skip this device enterely
321 if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
327 if [ "${PERSISTENT}" = "nofiles" ]
329 # do not mount the device to find for image files
334 case "$(get_fstype ${devname})" in
335 vfat|ext2|ext3|jffs2)
336 mkdir -p "${cow_backing}"
337 if ! try_mount "${devname}" "${cow_backing}" "rw"
342 if [ -f "${pers_fpath}" ]
344 echo $(setup_loop "${pers_fpath}" "loop" "/sys/block/loop*")
347 umount ${cow_backing}
359 # return the a string composed by device name, mountpoint an the first of ${filenames} found on a supported partition
360 # FIXME: merge with above function
363 snap_backing="/snap-backing"
364 black_listed_devices="${2}"
366 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
368 for dev in $(subdevices "${sysblock}")
370 devname=$(sys2dev "${dev}")
371 devfstype="$(get_fstype ${devname})"
373 if echo "${black_listed_devices}" | grep -q "${devname}"
375 # skip this device enterely
379 if is_supported_fs ${devfstype}
381 mkdir -p "${snap_backing}"
383 if try_mount "${devname}" "${snap_backing}" "ro" "${devfstype}"
385 for filename in ${filenames}
387 if [ -f "${snap_backing}/${filename}" ]
389 echo "${devname} ${snap_backing} ${filename}"
390 umount ${snap_backing}
396 umount ${snap_backing}
406 for adaptor in /sys/class/net/*
408 status="$(cat ${adaptor}/iflink)"
410 if [ "${status}" -eq 2 ]
412 mac="$(cat ${adaptor}/address)"
413 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
423 if [ -x /sbin/cryptsetup ]
425 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}