summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan Bauer2019-01-09 12:55:21 +0100
committerJonathan Bauer2019-01-09 12:55:21 +0100
commit0c0cf4a218fcb834bf82c6197cd99375e108eb7e (patch)
tree0cf80cadf5b8c0c0cd1beaeabc359b8ebe1ec16e
parent[slx-addon] fix addon-required test (diff)
downloadsystemd-init-0c0cf4a218fcb834bf82c6197cd99375e108eb7e.tar.gz
systemd-init-0c0cf4a218fcb834bf82c6197cd99375e108eb7e.tar.xz
systemd-init-0c0cf4a218fcb834bf82c6197cd99375e108eb7e.zip
[slx-partitioner] save dmsetup state for later
+ random small fixes and renaming of some scripts
-rwxr-xr-xbuilder/modules.d/slx-partitioner/module-setup.sh19
-rwxr-xr-xbuilder/modules.d/slx-partitioner/scripts/dmsetup-slx-device (renamed from builder/modules.d/slx-partitioner/scripts/slx_partitioner)72
-rw-r--r--builder/modules.d/slx-partitioner/scripts/generate-fstab-persistent.sh52
-rw-r--r--builder/modules.d/slx-partitioner/scripts/generate-fstab-swap.sh2
-rwxr-xr-xbuilder/modules.d/slx-partitioner/scripts/get-partitions-by-id (renamed from builder/modules.d/slx-partitioner/scripts/get_partitions_by_id)3
5 files changed, 117 insertions, 31 deletions
diff --git a/builder/modules.d/slx-partitioner/module-setup.sh b/builder/modules.d/slx-partitioner/module-setup.sh
index f4f833ef..45ad4f05 100755
--- a/builder/modules.d/slx-partitioner/module-setup.sh
+++ b/builder/modules.d/slx-partitioner/module-setup.sh
@@ -6,20 +6,13 @@ depends() {
echo ""
}
install() {
- inst "$moddir/scripts/get_partitions_by_id" "/usr/bin/get_partitions_by_id"
- inst "$moddir/scripts/slx_partitioner" "/usr/bin/slx_partitioner"
+ # install helpers
+ inst "$moddir/scripts/get-partitions-by-id" "/usr/bin/get-partitions-by-id"
+ inst "$moddir/scripts/dmsetup-slx-device" "/usr/bin/dmsetup-slx-device"
+ # install hooks
inst_hook pre-pivot 10 "$moddir/scripts/generate-fstab-swap.sh"
- inst_multiple \
- blockdev \
- cut \
- fsck.ext4 \
- fsck.xfs \
- head \
- mkfs.ext4 \
- mkfs.xfs \
- xxd
-# inst_hook pre-pivot 10 "$moddir/scripts/gen-fstab-persistent"
+ inst_multiple mkfs.ext4 mkfs.xfs fsck.ext4 fsck.xfs blockdev xxd
}
installkernel() {
- instmods dm-thin-pool dm-snapshot dm-crypt crc32c ext4 xfs # btrfs
+ instmods dm-thin-pool dm-snapshot dm-crypt crc32c ext4 xfs
}
diff --git a/builder/modules.d/slx-partitioner/scripts/slx_partitioner b/builder/modules.d/slx-partitioner/scripts/dmsetup-slx-device
index db17399b..832073a6 100755
--- a/builder/modules.d/slx-partitioner/scripts/slx_partitioner
+++ b/builder/modules.d/slx-partitioner/scripts/dmsetup-slx-device
@@ -10,13 +10,16 @@
# thin-snapshot root 10G 1
# thin-volume tmp 20G 0
# linear data0 5-10G 1
-# linear data1 1-50G 1
+# linear data1 1-50% 1
#
# NOTE: Encrypting thin-snapshot will actually encrypt the
# entire pool data device used for the pool.
# TODO: Support external keys
type -p emergency_shell || . /lib/dracut-lib.sh
+
+# for debugging purposes
+set -x
exec &> /run/openslx/dmsetup.log
# read-only device to prepare for CoW
@@ -143,23 +146,53 @@ ramdisk_fallback() {
emergency_shell "CRITICAL: failed to setup RAMdisk fallback."
exit 1
fi
- finish_setup "/dev/mapper/$cow_device_candidate"
+ finish_setup "$cow_device_candidate" "0" "$size"
}
-# finish_setup <device>
-# Set the resulting writable dnbd3 device in the global config and exit
+# finish_setup <device> <type> [<size>]
+# <device> is the device name only, /dev/mapper will be prepended automatically.
+# <type> denotes if the created device lies in a RAMdisk (0) or is backed by a disk (1).
+# <size> is given in sectors.
finish_setup() {
- if [ -z "$1" ] || [ ! -b "$1" ]; then
- emergency_shell "'$1' not a block device. Failed to setup CoW layer."
+ if [ -z "$1" ] || [ ! -b "/dev/mapper/$1" ]; then
+ emergency_shell "'/dev/mapper/$1' not a block device. Failed to setup CoW layer."
exit 1
fi
+ if ! [[ "$2" =~ ^[0-9]$ ]]; then
+ emergency_shell "'$2' not a valid type, 0 or 1 expected."
+ fi
+ # <size> optional?
(
echo "# Generated by '$0'."
- echo "SLX_DNBD3_DEVICE_COW=$1"
+ echo "SLX_DNBD3_DEVICE_COW=/dev/mapper/$1"
) >> /etc/openslx
+ save_partition_info "$1" "/" "$2" "$3"
exit 0
}
+# path to save the achieved setup to
+declare -rg partitions_config="/run/openslx/dmsetup.state"
+cat <<-EOF > "$partitions_config"
+# Generated by '$0'.
+# Format: <device_mapper_dev> <mount_point> <options>
+# Options can be:
+# * type -> CoW layer type: 0 is RAMdisk, 1 is disk, 2 is network
+# * size -> in 512 byte sectors
+EOF
+
+# save_partition_info <dm_dev> <mount_point> <type> [<size>]
+save_partition_info() {
+ [ -b "/dev/mapper/$1" ] || return 1
+ [ -n "$2" ] || return 1
+ [[ "$3" =~ ^[0-9]$ ]] || return 1
+ local opts="type=$3"
+ # plain size given
+ [[ "$4" =~ ^[0-9]+$ ]] && opts="$opts,physical_size=$4"
+ # <physical_backing_dev_size>-<virtual_size>
+ [[ "$4" =~ ^[0-9]+-[0-9]+$ ]] && opts="$opts,shared_physical_size=${4%-*},virtual_size=${4#*-}"
+ echo "/dev/mapper/$1 $2 ${opts}" >> "$partitions_config"
+}
+
###
## MAIN
###
@@ -169,7 +202,7 @@ finish_setup() {
declare -g writable_device=
if [ -n "$SLX_WRITABLE_DEVICE_IDENTIFIER" ]; then
# only first one for now TODO create linear devices of all ID44s
- writable_device="$(get_partitions_by_id "$SLX_WRITABLE_DEVICE_IDENTIFIER" | head -n 1)"
+ writable_device="$(get-partitions-by-id "$SLX_WRITABLE_DEVICE_IDENTIFIER" | head -n 1)"
fi
if [ -z "$writable_device" ]; then
echo "$0: Could not find writable device with id '$SLX_WRITABLE_DEVICE_IDENTIFIER'."
@@ -179,7 +212,6 @@ fi
# NOTE: from here on out, every value related to size is in 512 bytes sectors!
declare -g writable_device_size="$(blockdev --getsz $writable_device)"
-
# If SLX_WRITABLE_DEVICE_PARTITION_TABLE is not set, just do
# regular thin-snapshot for the CoW layer, else parse it.
if [ -n "$SLX_WRITABLE_DEVICE_PARTITION_TABLE" ]; then
@@ -187,7 +219,7 @@ if [ -n "$SLX_WRITABLE_DEVICE_PARTITION_TABLE" ]; then
fi
# Default to thin-snapshot, if none were configured
if [ -z "$snapshot" ] && [ -z "$thin_snapshot" ]; then
- parse_config "thin-snapshot root 10G 0"
+ parse_config "thin-snapshot root 100% 0"
fi
# Sanity checks for weird configurations
@@ -229,16 +261,19 @@ else
fi
# ... and slice it from the start of the writable device (for performance).
-if ! dmsetup_create_noudevsync "scratch" \
+declare -g scratch_device="/dev/mapper/scratch"
+if ! dmsetup_create_noudevsync "${scratch_device##*/}" \
"0 $scratch_device_size linear $writable_device $writable_device_allocated"; then
echo "$0: Failed to create scratch space for the CoW layer."
+ # TODO do not bail directly, but try to to create the linear devices at least?
ramdisk_fallback
fi
-scratch_device="/dev/mapper/scratch"
+save_partition_info "$scratch_device" "*" "1" "$scratch_device_size"
+
# encrypt the scratch device, if configured
if [ "$crypt" -ne 0 ] && encrypt_device \
- "/dev/mapper/scratch" "scratch-crypt" "$scratch_device_size"; then
- scratch_device="/dev/mapper/scratch-crypt"
+ "$scratch_device" "${scratch_device##*/}-crypt" "$scratch_device_size"; then
+ scratch_device="${scratch_device##*/}-crypt"
fi
writable_device_allocated="$scratch_device_size"
@@ -260,6 +295,8 @@ for i in ${!linear[@]}; do
echo "$0: Failed to create linear device: ${linear[$i]}"
continue
fi
+ # TODO sane?
+ save_partition_info "$name" "*" "1" "$to_allocate"
if [ "$crypt" -ne 0 ] && \
! encrypt_device "/dev/mapper/$name" "${name}-crypt" "$to_allocate"; then
echo "$0: Failed to encrypt '$name'."
@@ -349,7 +386,7 @@ if [ -n "$thin_snapshot" ] || [ -n "$thin_volume" ]; then
fi
# the order in which pool devices are created does not matter
# so start with thin volumes starting with id 2 and end with
- # the thin-snapshot with id 1
+ # the thin-snapshot with id 1 which needs to call finish_setup.
volume_id=2
# go over thin-volumes
for i in ${!thin_volume[@]}; do
@@ -360,6 +397,7 @@ if [ -n "$thin_snapshot" ] || [ -n "$thin_volume" ]; then
if ! create_volume "$name $(( volume_id++ )) $max"; then
echo "Failed to create thin volume '$name'."
fi
+ save_partition_info "$name" "*" "1" "${writable_device_size}-${max}"
if [ "$crypt" -ne 0 ] && ! encrypt_device \
"/dev/mapper/$name" "$name-crypt" "$max"; then
echo "Failed to encrypt thin volume '$name'."
@@ -374,7 +412,7 @@ if [ -n "$thin_snapshot" ] || [ -n "$thin_volume" ]; then
echo "Failed to create external snapshot for '$read_only_device'."
ramdisk_fallback
fi
- finish_setup "/dev/mapper/$name"
+ finish_setup "$name" "1" "$writable_device_size"
fi
fi
@@ -387,7 +425,7 @@ if [ -n "$snapshot" ]; then
echo "Failed to create regular snapshot for '$read_only_device' on '$writable_device'."
ramdisk_fallback
fi
- finish_setup "/dev/mapper/$name"
+ finish_setup "$name" "1" "$writable_device_size"
fi
# ultimate fallback
diff --git a/builder/modules.d/slx-partitioner/scripts/generate-fstab-persistent.sh b/builder/modules.d/slx-partitioner/scripts/generate-fstab-persistent.sh
new file mode 100644
index 00000000..5c6f2b82
--- /dev/null
+++ b/builder/modules.d/slx-partitioner/scripts/generate-fstab-persistent.sh
@@ -0,0 +1,52 @@
+#!/usr/bin/env bash
+#
+# Hook to generate stage4's fstab entries for persistent partitions
+#
+# Persistent identifiers (MBR types, GPT partition labels)
+# are expected to be specified in the OpenSLX config
+# as 'SLX_PERSISTENT_DEVICE_IDENTIFIER' and their filesystem
+# as 'SLX_PERSISTENT_DEVICE_FILESYSTEM', e.g ext4 or xfs.
+# If not specified, will default to 'auto' but will not
+# active systemd's features 'x-systemd.makefs' and 'x-systemd.growfs'
+
+. /etc/openslx
+
+type -p emergency_shell >/dev/null 2>&1 || source /lib/dracut-lib.sh
+
+# NOTE: systemd makes the mount point path automatically.
+# if multiple exists, take the biggest one (first in the list)
+if [ -n "$SLX_PERSISTENT_DEVICE_IDENTIFIER" ]; then
+ declare -a persistent_dev_list
+ for persistent_dev in \
+ $(get-partitions-by-id ${SLX_PERSISTENT_DEVICE_IDENTIFIER//,/ /}); do
+ [ -z "$persistent_dev" ] && continue
+ persistent_dev_list+=("$persistent_dev")
+ done
+ if [ "${#persistent_dev[@]}" -gt 0 ]; then
+ if [ "${#persistent_dev[@]}" -gt 1 ]; then
+ warn "$0: More than one persistent device found."
+ warn "$0: Will use the biggest one: ${persistent_dev[0]}"
+ fi
+ persistent_dev_systemd_name="$( tr '/' '-' <<< ${persistent_dev[0]:1})"
+ persistent_mount_opts="nofail"
+ if [ -n "$SLX_PERSISTENT_DEVICE_FILESYSTEM" ]; then
+ #persistent_mount_opts+=",x-systemd.requires=ensure-fs@${persistent_dev_systemd_name}"
+ persistent_mount_opts+=",x-systemd.after=ensure-fs@${persistent_dev_systemd_name}"
+ fi
+ (
+ echo -ne "${persistent_dev[0]}\t"
+ echo -ne "${SLX_PERSISTENT_DEVICE_MOUNT_POINT:-/opt/openslx/persistent}\t"
+ echo -ne "${SLX_PERSISTENT_DEVICE_FILESYSTEM:-auto}\t"
+ echo -ne "${persistent_mount_opts}\t0\t2"
+ ) >> "$NEWROOT/etc/fstab"
+
+ # drop-in to create filesystem if needed and
+ #persistent_dev_systemd_name="$( tr '/' '-' <<< ${persistent_dev[0]:1})"
+ #mkdir -p "$NEWROOT/etc/systemd/system/${persistent_dev_systemd_name}"
+ #cat <<- EOF > "$NEWROOT"
+ #EOF
+ else
+ warn "$0: No device with ID '$SLX_PERSISTENT_DEVICE_IDENTIFIER' found."
+ fi
+fi
+true
diff --git a/builder/modules.d/slx-partitioner/scripts/generate-fstab-swap.sh b/builder/modules.d/slx-partitioner/scripts/generate-fstab-swap.sh
index 5330730a..092fb97f 100644
--- a/builder/modules.d/slx-partitioner/scripts/generate-fstab-swap.sh
+++ b/builder/modules.d/slx-partitioner/scripts/generate-fstab-swap.sh
@@ -8,7 +8,7 @@
. /etc/openslx
for swap_dev in \
- $(get_partitions_by_id "82" "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"); do
+ $(get-partitions-by-id "82" "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"); do
[ -z "$swap_dev" ] && continue
diff --git a/builder/modules.d/slx-partitioner/scripts/get_partitions_by_id b/builder/modules.d/slx-partitioner/scripts/get-partitions-by-id
index 986001a1..2fe5ce7a 100755
--- a/builder/modules.d/slx-partitioner/scripts/get_partitions_by_id
+++ b/builder/modules.d/slx-partitioner/scripts/get-partitions-by-id
@@ -22,6 +22,9 @@ get_partitions_by_id () {
target="$1"'*'
shift
fi
+ # support commas and pipes to separate identifiers
+ local args=$@
+ set -- ${args//[,|]/ }
while [ $# -gt 0 ]; do
ID=$1
shift