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}"
195 for loopdev in ${pattern}
197 if [ "$(cat ${loopdev}/size)" -eq 0 ]
199 dev=$(sys2dev "${loopdev}")
202 if [ -n ${readonly} ]
204 if losetup --help 2>&1 | grep -q -- "-r\b"
206 options="${options} -r"
210 if [ 0 -lt "${offset}" ]
212 options="${options} -o ${offset}"
215 if [ -z "${encryption}" ]
217 losetup ${options} "${dev}" "${fspath}"
219 # Loop AES encryption
224 echo -n "Enter passphrase for root filesystem: " >&6
226 echo "${passphrase}" > /tmp/passphrase
228 exec 9</tmp/passphrase
229 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
232 rm -f /tmp/passphrase
234 if [ 0 -eq ${error} ]
241 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
244 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
257 panic "No loop devices available"
266 old_mountp="$(where_is_mounted ${dev})"
268 if [ -n "${old_mountp}" ]
270 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
271 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
273 mount -t $(get_fstype "${dev}") -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}"
280 cow_backing="/${pers_label}-backing"
282 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
284 for dev in $(subdevices "${sysblock}")
286 devname=$(sys2dev "${dev}")
288 if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
294 case "$(get_fstype ${devname})" in
295 vfat|ext2|ext3|jffs2)
296 mkdir -p "${cow_backing}"
297 try_mount "${devname}" "${cow_backing}" "rw"
299 if [ -f "${cow_backing}/${pers_label}" ]
301 echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
304 umount ${cow_backing}
316 # return the first of ${filenames} found on vfat and ext2/ext3 devices
317 # FIXME: merge with above function
320 snap_backing="/snap-backing"
322 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
324 for dev in $(subdevices "${sysblock}")
326 devname=$(sys2dev "${dev}")
327 devfstype="$(get_fstype ${devname})"
329 if is_supported_fs ${devfstype}
331 mkdir -p "${snap_backing}"
332 try_mount "${devname}" "${snap_backing}" "ro"
334 for filename in ${filenames}
336 if [ -f "${snap_backing}/${filename}" ]
338 echo "${devname} ${snap_backing} ${filename}"
343 umount ${snap_backing}
353 for adaptor in /sys/class/net/*
355 status="$(cat ${adaptor}/iflink)"
357 if [ "${status}" -eq 2 ]
359 mac="$(cat ${adaptor}/address)"
360 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
370 if [ -x /sbin/cryptsetup ]
372 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}