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" ]
34 # FIXME: do something better like the scan of supported filesystems
38 vfat|iso9660|udf|ext2|ext3|ntfs|jffs2)
51 # fstype misreports LUKS devices
54 /lib/udev/vol_id -t ${1} 2>/dev/null
60 if [ "${FSTYPE}" != "unknown" ]
66 /lib/udev/vol_id -t ${1} 2>/dev/null
73 if grep -q "^${device} " /proc/mounts
75 # return the first found
76 grep "^${device} " /proc/mounts | cut -f2 -d ' '
93 mounts="$(awk '{print $2}' /proc/mounts)"
94 testpath="$(busybox realpath ${testpath})"
98 if echo "${mounts}" | grep -qs "^${testpath}"
100 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
104 testpath=$(dirname $testpath)
111 # Returns used/free fs kbytes + 5% more
112 # You could pass a block device as ${1} or the mount point as ${2}
118 if [ -z "${mountp}" ]
120 mountp="$(where_is_mounted ${dev})"
122 if [ -z "${mountp}" ]
124 mountp="/mnt/tmp_fs_size"
127 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}"
133 if [ "${used}" = "used" ]
135 size=$(du -ks ${mountp} | cut -f1)
136 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
139 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
142 if [ -n "${doumount}" ]
154 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
156 loadkeys /etc/boottime.kmap.gz
166 local encryption=${5}
169 modprobe -q -b "${module}"
172 for loopdev in ${pattern}
174 if [ "$(cat ${loopdev}/size)" -eq 0 ]
176 dev=$(sys2dev "${loopdev}")
179 if [ -n ${readonly} ]
181 if /sbin/losetup --help 2>&1 | grep -q -- "-r\b"
183 options="${options} -r"
187 if [ 0 -lt "${offset}" ]
189 options="${options} -o ${offset}"
192 if [ -z "${encryption}" ]
194 losetup ${options} "${dev}" "${fspath}"
196 # Loop AES encryption
201 echo -n "Enter passphrase for root filesystem: " >&6
203 echo "${passphrase}" > /tmp/passphrase
205 exec 9</tmp/passphrase
206 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
209 rm -f /tmp/passphrase
211 if [ 0 -eq ${error} ]
218 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
221 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
234 panic "No loop devices available"
243 old_mountp="$(where_is_mounted ${dev})"
245 if [ -n "${old_mountp}" ]
247 mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
248 mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
250 mount -t $(get_fstype "${dev}") -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}"
257 cow_backing="/${pers_label}-backing"
259 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
261 for dev in $(subdevices "${sysblock}")
263 devname=$(sys2dev "${dev}")
265 if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
271 case "$(get_fstype ${devname})" in
272 vfat|ext2|ext3|jffs2)
273 mkdir -p "${cow_backing}"
274 try_mount "${devname}" "${cow_backing}" "rw"
276 if [ -f "${cow_backing}/${pers_label}" ]
278 echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
281 umount ${cow_backing}
293 # return the first of ${filenames} found on vfat and ext2/ext3 devices
294 # FIXME: merge with above function
297 snap_backing="/snap-backing"
299 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
301 for dev in $(subdevices "${sysblock}")
303 devname=$(sys2dev "${dev}")
304 devfstype="$(get_fstype ${devname})"
306 if is_supported_fs ${devfstype}
308 mkdir -p "${snap_backing}"
309 try_mount "${devname}" "${snap_backing}" "ro"
311 for filename in ${filenames}
313 if [ -f "${snap_backing}/${filename}" ]
315 echo "${devname} ${snap_backing} ${filename}"
320 umount ${snap_backing}
330 for adaptor in /sys/class/net/*
332 status="$(cat ${adaptor}/iflink)"
334 if [ "${status}" -eq 2 ]
336 mac="$(cat ${adaptor}/address)"
337 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
347 if [ -x /sbin/cryptsetup ]
349 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}