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" ]
37 # fstype misreports LUKS devices
40 /lib/udev/vol_id -t ${1} 2>/dev/null
46 if [ "${FSTYPE}" != "unknown" ]
52 /lib/udev/vol_id -t ${1} 2>/dev/null
59 if grep -q "^${device} " /proc/mounts
61 grep "^${device} " /proc/mounts | read d mountpoint rest
82 mounts="$(awk '{print $2}' /proc/mounts)"
83 testpath="$(busybox realpath ${testpath})"
87 if echo "${mounts}" | grep -qs "^${testpath}"
89 set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
93 testpath=$(dirname $testpath)
100 # Returns used/free fs kbytes + 5% more
101 # You could pass a block device as ${1} or the mount point as ${2}
107 if [ -z "${mountp}" ]
109 mountp=$(where_is_mounted "${dev}")
113 mountp="/mnt/tmp_fs_size"
116 mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}"
122 if [ "${used}" = "used" ]
124 size=$(du -ks ${mountp} | cut -f1)
125 size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
128 size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
131 if [ -n "${doumount}" ]
143 if [ -x /bin/loadkeys -a -r /etc/boottime.kmap.gz ]
145 loadkeys /etc/boottime.kmap.gz
155 local encryption=${5}
157 modprobe -q -b "${module}"
160 for loopdev in ${pattern}
162 if [ "$(cat ${loopdev}/size)" -eq 0 ]
164 dev=$(sys2dev "${loopdev}")
167 if [ 0 -lt "${offset}" ]
169 options="${options} -o ${offset}"
172 if [ -z "${encryption}" ]
174 losetup ${options} "${dev}" "${fspath}"
176 # Loop AES encryption
181 echo -n "Enter passphrase for root filesystem: " >&6
183 echo "${passphrase}" > /tmp/passphrase
185 exec 9</tmp/passphrase
186 /sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
189 rm -f /tmp/passphrase
191 if [ 0 -eq ${error} ]
198 echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
201 if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
214 panic "No loop devices available"
223 if where_is_mounted ${dev} > /dev/null
225 mount -o remount,"${opts}" ${dev} $(where_is_mounted ${dev}) || panic "Remounting failed"
226 mount -o bind $(where_is_mounted ${dev}) ${mountp} || panic "Cannot bind-mount"
228 mount -t $(get_fstype "${dev}") -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}"
235 cow_backing="/${pers_label}-backing"
237 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram)
239 for dev in $(subdevices "${sysblock}")
241 devname=$(sys2dev "${dev}")
243 if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
249 case "$(get_fstype ${devname})" in
250 vfat|ext2|ext3|jffs2)
251 mkdir -p "${cow_backing}"
252 try_mount "${devname}" "${cow_backing}" "rw"
254 if [ -f "${cow_backing}/${pers_label}" ]
256 echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
259 umount ${cow_backing}
271 # return the first of ${filenames} found on vfat and ext2/ext3 devices
272 # FIXME: merge with above function
275 snap_backing="/snap-backing"
277 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram)
279 for dev in $(subdevices "${sysblock}")
281 devname=$(sys2dev "${dev}")
282 devfstype="$(get_fstype ${devname})"
284 case "${devfstype}" in
285 vfat|ext2|ext3|jffs2)
286 # FIXME: all supported block devices should be scanned
287 mkdir -p "${snap_backing}"
288 try_mount "${devname}" "${snap_backing}" "ro"
290 for filename in ${filenames}
292 if [ -f "${snap_backing}/${filename}" ]
294 echo "${devname} ${snap_backing} ${filename}"
299 umount ${snap_backing}
310 for adaptor in /sys/class/net/*
312 status="$(cat ${adaptor}/iflink)"
314 if [ "${status}" -eq 2 ]
316 mac="$(cat ${adaptor}/address)"
317 mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
327 if [ -x /sbin/cryptsetup ]
329 /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}