summaryrefslogtreecommitdiffstats
path: root/dev-tools/snippets.sh
blob: 6f4f21cc81596f99a5f01a149ab303cebaf911ed (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
su nobody -s /bin/bash
yaourt -S multipath-tools
exit

# close running connection
./dnbd3-client -d /dev/dnbd0 -c

# 1. connect to dnbd3 server
cd dnbd3/build
insmod dnbd3.ko
./dnbd3-client -h gateway -i archLinux.qcow2 -d /dev/dnbd0

# 2. make available partition in qemu container
modprobe nbd
qemu-nbd --connect=/dev/nbd0 /dev/dnbd0 --read-only

# 2. make available partition in qemu container (writable)
# qemu-nbd --connect=/dev/nbd0 /dev/dnbd0 --snapshot
# todo where is the cow device?
# todo why does --partition not work?
# todo use qemu-img instead?

# 3. mount container partition
modprobe dm_multipath
kpartx -av /dev/nbd0 # or use fdisk -l and mount offset
mount /dev/mapper/nbd0p2 /mnt/

# 5. cleanup
umount /mnt/
kpartx -d /dev/nbd0
qemu-nbd --disconnect /dev/nbd0


# mount container partition directly
# yaourt -S libguestfs
guestmount -a /dev/dnbd0 -m /dev/sda2 --ro /mnt -v


# make read-only partition writable
mknod -m 660 /dev/ram0 b 1 1
chown root.disk /dev/ram0
DEV=/dev/mapper/nbd0p2
SIZE=`blockdev --getsz $DEV`
dmsetup create sandbox --table "0 $SIZE snapshot $DEV /dev/ram0 N 1"
mount /dev/mapper/sandbox /mnt
umount /mnt
dmsetup remove sandbox

modprobe loop
dd if=/dev/zero of=persistent_storage.img bs=1k count=1000
losetup /dev/loop0 persistent_storage.img

# Mount second partion example.
fdisk -lu
losetup -o $((1050624*512)) /dev/loop0 /dev/nbd0 -v
mount /dev/loop0 /mnt/

function create_partition_via_offset() {
    local device="$1"
    local nameOrUUID="$2"
    local loopDevice=$(losetup -f)

    #local sector_size=$(fdisk -l -u $device | grep "Sector size" | cut -d' ' -f4)
    local sectorSize=$(blockdev --getbsz $device)
    echo sector size: $sectorSize
    local partitionInfo=$(partx --raw --noheadings --output START,NAME,UUID /dev/nbd0 2>/dev/null| grep $nameOrUUID)
    local offsetSectors=$(echo $partitionInfo | cut -d' ' -f1)
    if [ -z "$offsetSectors" ]; then
        build_initramfs_log 'error' \
            "could not find partition with label/uuid '$nameOrUUID' on device $device"
        return 1
    fi
    #echo $(($offsetSectors*512)) # could overflow on 32bit systems
    offsetBytes=$(echo $| awk -v x=$offsetSectors -v y=$sectorSize '{print x * y}')
    echo $offsetBytes

    # test if mount works directly (problem with btrfs device id)
    #mount -v -o loop,offset=$offsetBytes $device $mountPoint
    losetup -v -o $offsetBytes $loopDevice $device
    echo $loopDevice
}
function make_partition_writable() {
    local deviceName="$1"
    local partition="$2"
    local writable_device="$3"

    local size=$(blockdev --getsz $partition)
    dmsetup create "$deviceName" --table \
        "0 $size snapshot $partition $writable_device N 1"
    mount /dev/mapper/sandbox /mnt
}
function make_ram_device() {
    mknod -m 660 /dev/ram0 b 1 1
    chown root.disk /dev/ram0
    echo /dev/ram0
}
function make_device_from_file() {
    local file="$1"
    local sizeInMegaByte="$2"
    #modprobe loop
    local loopDevice=$(losetup -f)
    dd if=/dev/zero of="$file" bs=1M count=$sizeInMegaByte
    losetup "$loopDevice" "$file"
    mkfs.ext4 "$loopDevice"
    echo $loopDevice
}

local partition=$(create_partition_via_offset "/dev/nbd0" "system")

local writableDevice=$(make_device_from_file "persistent_storage.img" "50")
local writableDevice=$(make_ram_device)
make_partition_writable "sandbox" "$partition" "$writableDevice"
mount /dev/mapper/sandbox $mountPoint