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 grep "^${device} " /proc/mounts | read d mountpoint rest
96 mounts="$(awk '{print $2}' /proc/mounts)"
97 testpath="$(busybox realpath ${testpath})"
101 if echo "${mounts}" | grep -qs "^${testpath}"
103 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
107 testpath=$(dirname $testpath)
114 # Returns used/free fs kbytes + 5% more
115 # You could pass a block device as ${1} or the mount point as ${2}
121 if [ -z "${mountp}" ]
123 mountp=$(where_is_mounted "${dev}")
127 mountp="/mnt/tmp_fs_size"
130 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}"
136 if [ "${used}" = "used" ]
138 size=$(du -ks ${mountp} | cut -f1)
139 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
142 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
145 if [ -n "${doumount}" ]
157 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
159 loadkeys /etc/boottime.kmap.gz
169 local encryption=${5}
172 modprobe -q -b "${module}"
175 for loopdev in ${pattern}
177 if [ "$(cat ${loopdev}/size)" -eq 0 ]
179 dev=$(sys2dev "${loopdev}")
182 if [ -n ${readonly} ]
184 if /sbin/losetup --help 2>&1 | grep -q -- "-r\b"
186 options="${options} -r"
190 if [ 0 -lt "${offset}" ]
192 options="${options} -o ${offset}"
195 if [ -z "${encryption}" ]
197 losetup ${options} "${dev}" "${fspath}"
199 # Loop AES encryption
204 echo -n "Enter passphrase for root filesystem: " >&6
206 echo "${passphrase}" > /tmp/passphrase
208 exec 9</tmp/passphrase
209 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
212 rm -f /tmp/passphrase
214 if [ 0 -eq ${error} ]
221 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
224 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
237 panic "No loop devices available"
246 if where_is_mounted ${dev} > /dev/null
248 mount -o remount,"${opts}" ${dev} $(where_is_mounted ${dev}) || panic "Remounting failed"
249 mount -o bind $(where_is_mounted ${dev}) ${mountp} || panic "Cannot bind-mount"
251 mount -t $(get_fstype "${dev}") -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}"
258 cow_backing="/${pers_label}-backing"
260 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
262 for dev in $(subdevices "${sysblock}")
264 devname=$(sys2dev "${dev}")
266 if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
272 case "$(get_fstype ${devname})" in
273 vfat|ext2|ext3|jffs2)
274 mkdir -p "${cow_backing}"
275 try_mount "${devname}" "${cow_backing}" "rw"
277 if [ -f "${cow_backing}/${pers_label}" ]
279 echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
282 umount ${cow_backing}
294 # return the first of ${filenames} found on vfat and ext2/ext3 devices
295 # FIXME: merge with above function
298 snap_backing="/snap-backing"
300 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
302 for dev in $(subdevices "${sysblock}")
304 devname=$(sys2dev "${dev}")
305 devfstype="$(get_fstype ${devname})"
307 if is_supported_fs ${devfstype}
309 mkdir -p "${snap_backing}"
310 try_mount "${devname}" "${snap_backing}" "ro"
312 for filename in ${filenames}
314 if [ -f "${snap_backing}/${filename}" ]
316 echo "${devname} ${snap_backing} ${filename}"
321 umount ${snap_backing}
331 for adaptor in /sys/class/net/*
333 status="$(cat ${adaptor}/iflink)"
335 if [ "${status}" -eq 2 ]
337 mac="$(cat ${adaptor}/address)"
338 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
348 if [ -x /sbin/cryptsetup ]
350 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}