summaryrefslogtreecommitdiffstats
path: root/builder/dnbd3-rootfs/hooks/pre-mount/prepare-root-partition.sh
blob: 2492e4932a1e610adf2abbbb51382018bb024c8a (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
source "/usr/lib/rebash/core.sh"
core.import utils
core.import logging
logging.set_commands_log_level debug
logging.set_log_level debug

# Close previous connections to be able to run this script many times.
dnbd3-client --device "$SLX_DNBD3_DEVICE" --close
local IFS_backup="$IFS"
IFS=","
local host
for host in ${SLX_DNBD3_SERVERS}; do
    info "Trying host \"$host\"."
    systemd-preserve-process-marker dnbd3-client --host "$host" --image \
        "${SLX_SERVER_BASE#/}${SLX_DNBD3_IMAGE}" --device "$SLX_DNBD3_DEVICE" \
        --rid "$SLX_DNBD3_RID"
    local return_code="$?"
    [[ $return_code == 0 ]] && break
    continue
done
IFS="$IFS_backup"
if [[ $return_code != 0 ]]; then
    warn "Failed to connect \"${SLX_SERVER_BASE#/}${SLX_DNBD3_IMAGE}\" from one of \"$SLX_DNBD3_SERVERS\" to \"$SLX_DNBD3_DEVICE\"."
    emergency_shell -n "Error in $0"
    return 1
fi
local nbd_device='/dev/nbd0'
systemd-preserve-process-marker qemu-nbd --connect="$nbd_device" \
    "$SLX_DNBD3_DEVICE" --read-only

# TODO make nicer :)
touch $nbd_device
udevadm settle

local read_only_partition=$(utils.find_block_device \
    "$SLX_SYSTEM_PARTITION_IDENTIFIER")
if [ -z $read_only_partition ]; then
    warn no partition found on device $nbd_device
    emergency_shell
fi
# Make read-only partition writable.
## detect partition for persistent storage
local persistent_device=$(utils.find_block_device \
    "$SLX_WRITABLE_DEVICE_IDENTIFIER")
# TODO move somewhere else
#local tmp_device=$(utils.find_block_device \
    #'$SLX_TMP_PARTITION_IDENTIFIER')

#if [ -n $tmp_device ]; then
    #mount -t auto "$tmp_device" $NEWROOT/tmp
#fi

local persistent="N" # 'P' for persistent storage, 'N' for not persistent
local writable_device
if [ -n "$persistent_device" ]; then
    writable_device=$persistent_device
    if [ "$SLX_WRITABLE_DEVICE_PERSISTENT" = "yes" ]; then
        persistent="P"
    fi
else
    local ramdisk_size="$SLX_RAMDISK_SIZE"
    if [ "ramdisk_size" = "" ]; then
        ramdisk_size=$(cat /proc/meminfo | awk '/MemTotal/ {print $2}')
    fi
    modprobe brd max_part=1 rd_size=$ramdisk_size
    writable_device="/dev/ram0"
    persistent="N"
fi

# persistent storage on a filesystem (e.g. usb stick)
# writable_device="$(losetup -f)"
#dd if=/dev/zero of=persistent_storage.img bs=1k count=1000
#losetup "$writable_device" persistent_storage.img
#persistent="P"

local partition_size="$(blockdev --getsz "$read_only_partition")"
local writable_partition_name="root"
logging.info "Using writable device $writable_device, persistency: $persistent"
local chunksize="1"
modprobe dm_snapshot
dmsetup create "$writable_partition_name" --noudevsync --table \
    "0 $partition_size snapshot $read_only_partition $writable_device $persistent $chunksize"
dmsetup mknodes --noudevsync "$writable_partition_name"