summaryrefslogtreecommitdiff
path: root/btrfs-receive-root.sh
blob: 175dd498b94b9759633e4b9f7a33648a156e1fd7 (plain)
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
#!/bin/sh

. sami/btrfs-functions.sh
. sami/var.sh

disable_stdout() { exec 3>&1; exec >&2; }
enable_stdout() { exec >&3; }

with_stdout() { enable_stdout; "$@"; disable_stdout; }

create_layer_filesystem()
{
    [ ! -e "$layer_file" ] || return
    ! mountpoint "$mountpoint" || return
    mkdir -p "$mountpoint"                                         &&
    sex dd if=/dev/zero of="$layer_file" bs=1M count="$layer_size" &&
    sex mount -o subvol=/,compress "$seed_file" "$mountpoint"      &&
    layer_dev=$(losetup -f --show "$layer_file")                   &&
    sex btrfs device add "$layer_dev" "$mountpoint"                &&
    mount -o rw,remount "$mountpoint"
}

finish()
{
    btrfs_replace_default_subvolume_with "$mountpoint"/"$receive_subv" &&
    umount "$mountpoint"                                               &&
    sex btrfstune -S1 "$layer_file"                                    &&
    losetup -d "$layer_dev"
}

set -e
disable_stdout

receive_dest=$1
receive_subv=$2

[ "$receive_subv" ]
[ "$receive_dest" ]

mountpoint=$(realpath -m --relative-base=. "$receive_dest")

seed_file=sami/debian-live-8.4.0-amd64-standard.btrfs
layer_file=sami/debian-live-8.4.0-amd64-standard.layer.$receive_subv.btrfs
layer_size=1000

create_layer_filesystem
with_stdout sex btrfs receive "$mountpoint"
finish