2 # live-initramfs helper functions, used by live-initramfs on boot and by live-snapshot
4 if [ ! -x "/bin/fstype" ]
6 # klibc not in path -> not in initramfs
7 export PATH="${PATH}:/usr/lib/klibc/bin"
13 echo "/dev/$(udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
21 for dev in "${sysblock}" "${sysblock}"/*
23 if [ -e "${dev}/dev" ]
36 # Validate input first
42 # Try to look if it is already supported by the kernel
43 if grep -q ${fstype} /proc/filesystems
47 # Then try to add support for it the gentle way using the initramfs capabilities
49 if grep -q ${fstype} /proc/filesystems
52 # Then try the hard way if /root is already reachable
54 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
55 if [ -e "${kmodule}" ]
58 if grep -q ${fstype} /proc/filesystems
74 # fstype misreports LUKS devices
77 /lib/udev/vol_id -t ${1} 2>/dev/null
83 if [ "${FSTYPE}" != "unknown" ]
89 /lib/udev/vol_id -t ${1} 2>/dev/null
96 if grep -q "^${device} " /proc/mounts
98 # return the first found
99 grep "^${device} " /proc/mounts | cut -f2 -d ' '
116 mounts="$(awk '{print $2}' /proc/mounts)"
117 testpath="$(busybox realpath ${testpath})"
121 if echo "${mounts}" | grep -qs "^${testpath}"
123 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
127 testpath=$(dirname $testpath)
134 # Returns used/free fs kbytes + 5% more
135 # You could pass a block device as ${1} or the mount point as ${2}
141 if [ -z "${mountp}" ]
143 mountp="$(where_is_mounted ${dev})"
145 if [ -z "${mountp}" ]
147 mountp="/mnt/tmp_fs_size"
150 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}"
156 if [ "${used}" = "used" ]
158 size=$(du -ks ${mountp} | cut -f1)
159 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
162 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
165 if [ -n "${doumount}" ]
177 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
179 loadkeys /etc/boottime.kmap.gz
189 local encryption=${5}
192 modprobe -q -b "${module}"
194 if [ -x /sbin/udevadm ]
203 for loopdev in ${pattern}
205 if [ "$(cat ${loopdev}/size)" -eq 0 ]
207 dev=$(sys2dev "${loopdev}")
210 if [ -n ${readonly} ]
212 if losetup --help 2>&1 | grep -q -- "-r\b"
214 options="${options} -r"
218 if [ 0 -lt "${offset}" ]
220 options="${options} -o ${offset}"
223 if [ -z "${encryption}" ]
225 losetup ${options} "${dev}" "${fspath}"
227 # Loop AES encryption
232 echo -n "Enter passphrase for root filesystem: " >&6
234 echo "${passphrase}" > /tmp/passphrase
236 exec 9</tmp/passphrase
237 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
240 rm -f /tmp/passphrase
242 if [ 0 -eq ${error} ]
249 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
252 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
265 panic "No loop devices available"
274 old_mountp="$(where_is_mounted ${dev})"
276 if [ -n "${old_mountp}" ]
278 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 mount -t $(get_fstype "${dev}") -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}"
288 cow_backing="/${pers_label}-backing"
290 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
292 for dev in $(subdevices "${sysblock}")
294 devname=$(sys2dev "${dev}")
296 if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
302 case "$(get_fstype ${devname})" in
303 vfat|ext2|ext3|jffs2)
304 mkdir -p "${cow_backing}"
305 try_mount "${devname}" "${cow_backing}" "rw"
307 if [ -f "${cow_backing}/${pers_label}" ]
309 echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
312 umount ${cow_backing}
324 # return the first of ${filenames} found on vfat and ext2/ext3 devices
325 # FIXME: merge with above function
328 snap_backing="/snap-backing"
330 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
332 for dev in $(subdevices "${sysblock}")
334 devname=$(sys2dev "${dev}")
335 devfstype="$(get_fstype ${devname})"
337 if is_supported_fs ${devfstype}
339 mkdir -p "${snap_backing}"
340 try_mount "${devname}" "${snap_backing}" "ro"
342 for filename in ${filenames}
344 if [ -f "${snap_backing}/${filename}" ]
346 echo "${devname} ${snap_backing} ${filename}"
351 umount ${snap_backing}
361 for adaptor in /sys/class/net/*
363 status="$(cat ${adaptor}/iflink)"
365 if [ "${status}" -eq 2 ]
367 mac="$(cat ${adaptor}/address)"
368 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
378 if [ -x /sbin/cryptsetup ]
380 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}