summaryrefslogtreecommitdiffstats
path: root/builder/dnbd3-rootfs/hooks/pre-mount/prepare-root-partition.sh
blob: ade25de0695ed59ca13c80a7c29bbd53b9929c0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
source "/usr/lib/utils.sh"

declare -rg DNBD3_SERVER="gateway"
declare -rg DNBD3_IMAGE="archLinux.qcow2"
declare -rg DNBD3_RID="0"
declare -rg DNBD3_DEVICE="/dev/dnbd0"

# Close previous connections to be able to run this script many times.
dnbd3-client --device /dev/dnbd0 --close && \
dnbd3-client --host "$DNBD3_SERVER" --image "$DNBD3_IMAGE" --device \
    "$DNBD3_DEVICE" --rid "$DNBD3_RID" && \
qemu-nbd --connect=/dev/nbd0 /dev/dnbd0 --read-only && \
local read_only_partition="$(utils_create_partition_via_offset \
    /dev/nbd0 system)" && \
# Make read-only partition writable.
# TODO implement persistent available indicator.
if true; then
    local writable_device="/dev/ram0" && \
    mknod --mode 660 "$writable_device" b 1 1 && \
    chown root.disk "$writable_device"
else
    # Use next free loop device.
    local writable_device="$(losetup -f)" && \
    dd if=/dev/zero of=persistent_storage.img bs=1k count=1000 && \
    losetup "$writable_device" persistent_storage.img
fi
local size="$(blockdev --getsz "$read_only_partition")" && \
local writable_partition_name="root" && \
dmsetup create "$writable_partition_name" --table \
    "0 $partion_size snapshot $read_only_partition $writable_device N 1"