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 # Try to look if it is already supported by the kernel
37 if grep -q ${fstype} /proc/filesystems
41 # Then try to add support for it the gentle way using the initramfs capabilities
43 if grep -q ${fstype} /proc/filesystems
46 # Then try the hard way if /root is already reachable
48 kmodule="/root/lib/modules/`uname -r`/${fstype}/${fstype}.ko"
49 if [ -e "${kmodule}" ]
52 if grep -q ${fstype} /proc/filesystems
68 # fstype misreports LUKS devices
71 /lib/udev/vol_id -t ${1} 2>/dev/null
77 if [ "${FSTYPE}" != "unknown" ]
83 /lib/udev/vol_id -t ${1} 2>/dev/null
90 if grep -q "^${device} " /proc/mounts
92 # return the first found
93 grep "^${device} " /proc/mounts | cut -f2 -d ' '
110 mounts="$(awk '{print $2}' /proc/mounts)"
111 testpath="$(busybox realpath ${testpath})"
115 if echo "${mounts}" | grep -qs "^${testpath}"
117 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
121 testpath=$(dirname $testpath)
128 # Returns used/free fs kbytes + 5% more
129 # You could pass a block device as ${1} or the mount point as ${2}
135 if [ -z "${mountp}" ]
137 mountp="$(where_is_mounted ${dev})"
139 if [ -z "${mountp}" ]
141 mountp="/mnt/tmp_fs_size"
144 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}"
150 if [ "${used}" = "used" ]
152 size=$(du -ks ${mountp} | cut -f1)
153 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
156 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
159 if [ -n "${doumount}" ]
171 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
173 loadkeys /etc/boottime.kmap.gz
183 local encryption=${5}
186 modprobe -q -b "${module}"
189 for loopdev in ${pattern}
191 if [ "$(cat ${loopdev}/size)" -eq 0 ]
193 dev=$(sys2dev "${loopdev}")
196 if [ -n ${readonly} ]
198 if losetup --help 2>&1 | grep -q -- "-r\b"
200 options="${options} -r"
204 if [ 0 -lt "${offset}" ]
206 options="${options} -o ${offset}"
209 if [ -z "${encryption}" ]
211 losetup ${options} "${dev}" "${fspath}"
213 # Loop AES encryption
218 echo -n "Enter passphrase for root filesystem: " >&6
220 echo "${passphrase}" > /tmp/passphrase
222 exec 9</tmp/passphrase
223 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
226 rm -f /tmp/passphrase
228 if [ 0 -eq ${error} ]
235 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
238 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
251 panic "No loop devices available"
260 old_mountp="$(where_is_mounted ${dev})"
262 if [ -n "${old_mountp}" ]
264 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
265 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
267 mount -t $(get_fstype "${dev}") -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}"
274 cow_backing="/${pers_label}-backing"
276 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
278 for dev in $(subdevices "${sysblock}")
280 devname=$(sys2dev "${dev}")
282 if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
288 case "$(get_fstype ${devname})" in
289 vfat|ext2|ext3|jffs2)
290 mkdir -p "${cow_backing}"
291 try_mount "${devname}" "${cow_backing}" "rw"
293 if [ -f "${cow_backing}/${pers_label}" ]
295 echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
298 umount ${cow_backing}
310 # return the first of ${filenames} found on vfat and ext2/ext3 devices
311 # FIXME: merge with above function
314 snap_backing="/snap-backing"
316 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
318 for dev in $(subdevices "${sysblock}")
320 devname=$(sys2dev "${dev}")
321 devfstype="$(get_fstype ${devname})"
323 if is_supported_fs ${devfstype}
325 mkdir -p "${snap_backing}"
326 try_mount "${devname}" "${snap_backing}" "ro"
328 for filename in ${filenames}
330 if [ -f "${snap_backing}/${filename}" ]
332 echo "${devname} ${snap_backing} ${filename}"
337 umount ${snap_backing}
347 for adaptor in /sys/class/net/*
349 status="$(cat ${adaptor}/iflink)"
351 if [ "${status}" -eq 2 ]
353 mac="$(cat ${adaptor}/address)"
354 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
364 if [ -x /sbin/cryptsetup ]
366 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}