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 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 if where_is_mounted ${dev} > /dev/null
245 mount -o remount,"${opts}" ${dev} $(where_is_mounted ${dev}) || panic "Remounting failed"
246 mount -o bind $(where_is_mounted ${dev}) ${mountp} || panic "Cannot bind-mount"
248 mount -t $(get_fstype "${dev}") -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}"
255 cow_backing="/${pers_label}-backing"
257 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
259 for dev in $(subdevices "${sysblock}")
261 devname=$(sys2dev "${dev}")
263 if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
269 case "$(get_fstype ${devname})" in
270 vfat|ext2|ext3|jffs2)
271 mkdir -p "${cow_backing}"
272 try_mount "${devname}" "${cow_backing}" "rw"
274 if [ -f "${cow_backing}/${pers_label}" ]
276 echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
279 umount ${cow_backing}
291 # return the first of ${filenames} found on vfat and ext2/ext3 devices
292 # FIXME: merge with above function
295 snap_backing="/snap-backing"
297 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
299 for dev in $(subdevices "${sysblock}")
301 devname=$(sys2dev "${dev}")
302 devfstype="$(get_fstype ${devname})"
304 if is_supported_fs ${devfstype}
306 mkdir -p "${snap_backing}"
307 try_mount "${devname}" "${snap_backing}" "ro"
309 for filename in ${filenames}
311 if [ -f "${snap_backing}/${filename}" ]
313 echo "${devname} ${snap_backing} ${filename}"
318 umount ${snap_backing}
328 for adaptor in /sys/class/net/*
330 status="$(cat ${adaptor}/iflink)"
332 if [ "${status}" -eq 2 ]
334 mac="$(cat ${adaptor}/address)"
335 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
345 if [ -x /sbin/cryptsetup ]
347 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}