#!/bin/sh
# live-initramfs helper functions, used by live-initramfs on boot and by live-snapshot
-if [ "${BUILD_SYSTEM}" = "Ubuntu" ]
-then
- MP_QUIET="-Q"
-elif [ "${BUILD_SYSTEM}" = "Debian" ]
-then
- MP_QUIET="-q"
-else
- MP_QUIET=""
-fi
-
if [ ! -x "/bin/fstype" ]
then
# klibc not in path -> not in initramfs
subdevices ()
{
- sysblock=$1
+ sysblock=${1}
r=""
for dev in "${sysblock}" "${sysblock}"/*
local FSTYPE
local FSSIZE
- eval $(fstype < $1)
+ # fstype misreports LUKS devices
+ if is_luks "${1}"
+ then
+ /lib/udev/vol_id -t ${1} 2>/dev/null
+ return
+ fi
+
+ eval $(fstype < ${1})
- if [ "$FSTYPE" != "unknown" ]
+ if [ "${FSTYPE}" != "unknown" ]
then
- echo $FSTYPE
+ echo ${FSTYPE}
return 0
fi
- /lib/udev/vol_id -t $1 2>/dev/null
+ /lib/udev/vol_id -t ${1} 2>/dev/null
}
where_is_mounted ()
{
- device=$1
+ device=${1}
- if grep -q "^$device " /proc/mounts
+ if grep -q "^${device} " /proc/mounts
then
- grep "^$device " /proc/mounts | read d mountpoint rest
- echo $mountpoint
+ grep "^${device} " /proc/mounts | read d mountpoint rest
+ echo ${mountpoint}
return 0
fi
do
if echo "${mounts}" | grep -qs "^${testpath}"
then
- set -- `echo "${mounts}" | grep "^${testpath}" | lastline`
+ set -- $(echo "${mounts}" | grep "^${testpath}" | lastline)
echo ${1}
break
else
- testpath=`dirname $testpath`
+ testpath=$(dirname $testpath)
fi
done
}
fs_size ()
{
# Returns used/free fs kbytes + 5% more
- # You could pass a block device as $1 or the mount point as $2
+ # You could pass a block device as ${1} or the mount point as ${2}
dev="${1}"
mountp="${2}"
then
mountp=$(where_is_mounted "${dev}")
- if [ "$?" -gt 0 ]
+ if [ "${?}" -gt 0 ]
then
mountp="/mnt/tmp_fs_size"
setup_loop ()
{
- local fspath=$1
- local module=$2
- local pattern=$3
- local offset=$4
- local encryption=$5
+ local fspath=${1}
+ local module=${2}
+ local pattern=${3}
+ local offset=${4}
+ local encryption=${5}
- modprobe ${MP_QUIET} -b "$module"
+ modprobe -q -b "${module}"
udevsettle
- for loopdev in $pattern
+ for loopdev in ${pattern}
do
- if [ "$(cat $loopdev/size)" -eq 0 ]
+ if [ "$(cat ${loopdev}/size)" -eq 0 ]
then
dev=$(sys2dev "${loopdev}")
options=''
do
load_keymap
- echo -n "Enter passphrase for ${fspath}: " >&6
+ echo -n "Enter passphrase for root filesystem: " >&6
read -s passphrase
echo "${passphrase}" > /tmp/passphrase
+ unset passphrase
exec 9</tmp/passphrase
/sbin/losetup ${options} -e "${encryption}" -p 9 "${dev}" "${fspath}"
- error=$?
+ error=${?}
exec 9<&-
rm -f /tmp/passphrase
break
fi
- echo -n "Something went wrong... Retry? [YES/no] " >&6
+ echo
+ echo -n "There was an error decrypting the root filesystem ... Retry? [Y/n] " >&6
read answer
- if [ 'no' = "${answer}" ]
+ if [ "$(echo "${answer}" | cut -b1 | tr A-Z a-z)" = "n" ]
then
unset answer
break
done
fi
- echo "$dev"
+ echo "${dev}"
return 0
fi
done
pers_label="${1}"
cow_backing="/${pers_label}-backing"
- for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop)
+ for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram)
do
for dev in $(subdevices "${sysblock}")
do
devname=$(sys2dev "${dev}")
- if [ "$(/lib/udev/vol_id -l $devname 2>/dev/null)" = "${pers_label}" ]
+ if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
then
- echo "$devname"
+ echo "${devname}"
return
- elif [ "$(get_fstype ${devname})" = "vfat" ]
- then
- # FIXME: all supported block devices should be scanned
- mkdir -p "${cow_backing}"
- try_mount "${devname}" "${cow_backing}" "rw"
-
- if [ -e "${cow_backing}/${pers_label}" ]
- then
- echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
- return 0
- else
- umount ${cow_backing}
- fi
fi
+
+ case "$(get_fstype ${devname})" in
+ vfat|ext2|ext3|jffs2)
+ mkdir -p "${cow_backing}"
+ try_mount "${devname}" "${cow_backing}" "rw"
+
+ if [ -f "${cow_backing}/${pers_label}" ]
+ then
+ echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
+ return 0
+ else
+ umount ${cow_backing}
+ fi
+ ;;
+ *)
+ ;;
+ esac
done
done
}
find_files ()
{
- # return the first of $filenames found on vfat and ext2/ext3 devices
+ # return the first of ${filenames} found on vfat and ext2/ext3 devices
# FIXME: merge with above function
filenames="${1}"
snap_backing="/snap-backing"
- for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop)
+ for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram)
do
for dev in $(subdevices "${sysblock}")
do
devname=$(sys2dev "${dev}")
devfstype="$(get_fstype ${devname})"
- if [ "${devfstype}" = "vfat" ] || [ "${devfstype}" = "ext2" ] || [ "${devfstype}" = "ext3" ]
- then
- # FIXME: all supported block devices should be scanned
- mkdir -p "${snap_backing}"
- try_mount "${devname}" "${snap_backing}" "ro"
+ case "${devfstype}" in
+ vfat|ext2|ext3|jffs2)
+ # FIXME: all supported block devices should be scanned
+ mkdir -p "${snap_backing}"
+ try_mount "${devname}" "${snap_backing}" "ro"
+
+ for filename in ${filenames}
+ do
+ if [ -f "${snap_backing}/${filename}" ]
+ then
+ echo "${devname} ${snap_backing} ${filename}"
+ return 0
+ fi
+ done
+
+ umount ${snap_backing}
+ ;;
+ esac
+ done
+ done
+}
- for filename in ${filenames}
- do
- if [ -e "${snap_backing}/${filename}" ]
- then
- echo "${devname} ${snap_backing} ${filename}"
- return 0
- fi
- done
+get_mac ()
+{
+ mac=""
- umount ${snap_backing}
- fi
- done
+ for adaptor in /sys/class/net/*
+ do
+ status="$(cat ${adaptor}/iflink)"
+
+ if [ "${status}" -eq 2 ]
+ then
+ mac="$(cat ${adaptor}/address)"
+ mac="$(echo ${mac} | sed 's/:/-/g' | tr '[a-z]' '[A-Z]')"
+ fi
done
+
+ echo ${mac}
+}
+
+is_luks()
+{
+ devname="${1}"
+ if [ -x /sbin/cryptsetup ]
+ then
+ /sbin/cryptsetup isLuks "${devname}" 2>/dev/null || ret=${?}
+ return ${ret}
+ else
+ return 1
+ fi
+
}