summaryrefslogtreecommitdiff
path: root/btrfs-receive-root.sh
blob: f553c2cb571e80ccbd4b2dc4d655fa02cd7cd013 (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
49
50
51
52
53
54
55
#!/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()
{
    local subv_id
    sex mv "$mountpoint"/ROOT "$mountpoint"/ROOT.old                              || return
    sex btrfs subvolume snapshot "$mountpoint"/"$receive_subv" "$mountpoint"/ROOT || return
    subv_id=$(btrfs_show_subvolume_id "$mountpoint"/ROOT)                         || return
    sex btrfs subvolume set-default "$subv_id" "$mountpoint"                      || return
    sex btrfs subvolume delete "$mountpoint"/ROOT.old                             || return
    umount "$mountpoint"                                                          || return
    sex btrfstune -S1 "$layer_file"                                               || return
    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_FINAL=sami/debian-live-8.4.0-amd64-standard.layer.$receive_subv.btrfs
layer_file=$layer_file_FINAL.part
layer_size=1000

create_layer_filesystem
with_stdout sex btrfs receive "$mountpoint"
finish
mv "$layer_file" "$layer_file_FINAL"