summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--btrfs-functions.sh42
-rw-r--r--btrfs-receive-root.sh45
-rw-r--r--btrfs-send-root.sh4
-rwxr-xr-xxorriso-layer.sh23
4 files changed, 87 insertions, 27 deletions
diff --git a/btrfs-functions.sh b/btrfs-functions.sh
index 3648c24..b83b94d 100644
--- a/btrfs-functions.sh
+++ b/btrfs-functions.sh
@@ -100,28 +100,52 @@ btrfs_show_default_path()
100 mp=$(btrfs_get_mountpoint "$mp") || die # TODO: fix caller? 100 mp=$(btrfs_get_mountpoint "$mp") || die # TODO: fix caller?
101 btrfs_mountpoint "$mp" || die "not a mountpoint: $mp" 101 btrfs_mountpoint "$mp" || die "not a mountpoint: $mp"
102 path=$(btrfs subvolume get-default "$mp"/|sed -n -e 's/.* path //p') 102 path=$(btrfs subvolume get-default "$mp"/|sed -n -e 's/.* path //p')
103 printf '%s\n' "$mp/$path" 103 if [ "$path" ]; then
104 printf '%s\n' "$mp/$path"
105 else
106 printf '%s\n' "$mp"
107 fi
108}
109
110btrfs_show_default_id()
111{
112 $(ARGS_NE mp)
113 local id
114 mp=$(btrfs_get_mountpoint "$mp") || die # TODO: fix caller?
115 btrfs_mountpoint "$mp" || die "not a mountpoint: $mp"
116 id=$(btrfs subvolume get-default "$mp"/|sed -n -e 's/^ID \([^ ]*\) .*/\1/p')
117 [ "$id" ] || return
118 echo $id
104} 119}
105 120
106btrfs_replace_default_subvolume_with() 121btrfs_replace_default_subvolume_with()
107{ 122{
108 $(ARGS_NE new_default) 123 $(ARGS_NE new_default)
109 local old_default subvol_id 124 local old_default old_default_id new_default_id
110 old_default=$(btrfs_show_default_path "$new_default") || die 125 old_default_id=$(btrfs_show_default_id "$new_default") || die
126 new_default_id=$(btrfs_show_subvolume_id "$new_default") || die
127
128 [ "$new_default_id" = "$old_default_id" ] && return
129
130 if [ "$old_default_id" != 5 ]; then
131 old_default=$(btrfs_show_default_path "$new_default") || die
132 else
133 old_default=
134 fi
111 135
112 [ "$new_default" = "$old_default" ] && return 136 btrfs subvolume set-default "$new_default_id" "$new_default" || die
113 137
114 subvol_id=$(btrfs_show_subvolume_id "$new_default") || die 138 if [ "$old_default" ]; then
115 btrfs subvolume set-default "$subvol_id" "$new_default" || die 139 btrfs subvolume delete "$old_default"
116 btrfs subvolume delete "$old_default" 140 sex mv "$new_default" "$old_default"
117 sex mv "$new_default" "$old_default" 141 fi
118} 142}
119 143
120btrfs_show_subvolume_id() 144btrfs_show_subvolume_id()
121{ 145{
122 $(ARGS_NE path) 146 $(ARGS_NE path)
123 local result 147 local result
124 result=$(btrfs subvolume show "$path" | sed -n -e 's/^[ \t]*Subvolume ID:[ \t]*//p') 148 result=$(btrfs subvolume show "$path" | sed -n -e 's/^[ \t]*Subvolume ID:[ \t]*//p; s/.*is toplevel subvolume/5/p')
125 if [ "$result" ] 149 if [ "$result" ]
126 then printf '%s\n' "$result" 150 then printf '%s\n' "$result"
127 else false 151 else false
diff --git a/btrfs-receive-root.sh b/btrfs-receive-root.sh
index 178fd64..175dd49 100644
--- a/btrfs-receive-root.sh
+++ b/btrfs-receive-root.sh
@@ -1,10 +1,7 @@
1#!/bin/sh 1#!/bin/sh
2 2
3layer_dir=/home/d/sami 3. sami/btrfs-functions.sh
4seed_file=debian-live-8.4.0-amd64-standard.btrfs 4. sami/var.sh
5layer_file=debian-live-8.4.0-amd64-standard.layer.$$.btrfs
6layer_size=1000
7mountpoint=layer_dest.$$
8 5
9disable_stdout() { exec 3>&1; exec >&2; } 6disable_stdout() { exec 3>&1; exec >&2; }
10enable_stdout() { exec >&3; } 7enable_stdout() { exec >&3; }
@@ -14,24 +11,38 @@ with_stdout() { enable_stdout; "$@"; disable_stdout; }
14create_layer_filesystem() 11create_layer_filesystem()
15{ 12{
16 [ ! -e "$layer_file" ] || return 13 [ ! -e "$layer_file" ] || return
17 mkdir -p "$mountpoint" && 14 ! mountpoint "$mountpoint" || return
18 dd if=/dev/zero of="$layer_file" bs=1M count="$layer_size" && 15 mkdir -p "$mountpoint" &&
19 mount -o subvol=/,compress "$seed_file" "$mountpoint" && 16 sex dd if=/dev/zero of="$layer_file" bs=1M count="$layer_size" &&
20 layer_dev=$(losetup -f --show "$layer_file") && 17 sex mount -o subvol=/,compress "$seed_file" "$mountpoint" &&
21 btrfs device add "$layer_dev" "$mountpoint" && 18 layer_dev=$(losetup -f --show "$layer_file") &&
19 sex btrfs device add "$layer_dev" "$mountpoint" &&
22 mount -o rw,remount "$mountpoint" 20 mount -o rw,remount "$mountpoint"
23} 21}
24 22
25finish() 23finish()
26{ 24{
27 umount "$mountpoint" && 25 btrfs_replace_default_subvolume_with "$mountpoint"/"$receive_subv" &&
28 btrfstune -S1 "$layer_file" 26 umount "$mountpoint" &&
27 sex btrfstune -S1 "$layer_file" &&
28 losetup -d "$layer_dev"
29} 29}
30 30
31set -ex 31set -e
32disable_stdout
33
34receive_dest=$1
35receive_subv=$2
36
37[ "$receive_subv" ]
38[ "$receive_dest" ]
39
40mountpoint=$(realpath -m --relative-base=. "$receive_dest")
41
42seed_file=sami/debian-live-8.4.0-amd64-standard.btrfs
43layer_file=sami/debian-live-8.4.0-amd64-standard.layer.$receive_subv.btrfs
44layer_size=1000
32 45
33cd "$layer_dir" && 46create_layer_filesystem
34disable_stdout && 47with_stdout sex btrfs receive "$mountpoint"
35create_layer_filesystem &&
36with_stdout btrfs receive "$mountpoint" &&
37finish 48finish
diff --git a/btrfs-send-root.sh b/btrfs-send-root.sh
index 6152db8..25e60f2 100644
--- a/btrfs-send-root.sh
+++ b/btrfs-send-root.sh
@@ -10,7 +10,8 @@ rootfs_uuid ()
10remote_btrfs_receiver() 10remote_btrfs_receiver()
11{ 11{
12# ssh "$BTRFS_RECEIVE_DESTINATION_HOST" -- "sudo btrfs receive $(shellescape "$BTRFS_RECEIVE_DESTINATION_PATH")" 12# ssh "$BTRFS_RECEIVE_DESTINATION_HOST" -- "sudo btrfs receive $(shellescape "$BTRFS_RECEIVE_DESTINATION_PATH")"
13 ssh "$BTRFS_RECEIVE_DESTINATION_HOST" -- "sudo sh sami/btrfs-receive-root.sh" 13 ssh "$BTRFS_RECEIVE_DESTINATION_HOST" -- \
14 "sudo sh sami/btrfs-receive-root.sh $(shellescape "$BTRFS_RECEIVE_DESTINATION_PATH") $(shellescape "$BTRFS_RECEIVE_SUBVOLUME_NAME")"
14} 15}
15 16
16dummy_receiver() 17dummy_receiver()
@@ -32,6 +33,7 @@ push_remote()
32 esac 33 esac
33 local BTRFS_RECEIVE_DESTINATION_PATH="${ssh_dst#*:}" 34 local BTRFS_RECEIVE_DESTINATION_PATH="${ssh_dst#*:}"
34 local BTRFS_RECEIVE_DESTINATION_HOST="${ssh_dst%%:*}" 35 local BTRFS_RECEIVE_DESTINATION_HOST="${ssh_dst%%:*}"
36 local BTRFS_RECEIVE_SUBVOLUME_NAME="${snap_dir#$mnt/}"
35 push_helper false "$snap_dir" "$prev_dir" "$src" remote_btrfs_receiver 37 push_helper false "$snap_dir" "$prev_dir" "$src" remote_btrfs_receiver
36} 38}
37 39
diff --git a/xorriso-layer.sh b/xorriso-layer.sh
new file mode 100755
index 0000000..1afb9f8
--- /dev/null
+++ b/xorriso-layer.sh
@@ -0,0 +1,23 @@
1#!/bin/bash
2
3outdev=debian-live-8.4.0-amd64-standard.btrfs.layered.iso
4indev=debian-live-8.4.0-amd64-standard.btrfs.iso
5volid=SamizdatLive
6btrfs_layer_iso_path=live/layer.btrfs
7btrfs_layer_filesystem=debian-live-8.4.0-amd64-standard.layer.snapshot.2016-04-22.154447.btrfs
8
9die() { printf '%s\n' "$*"; exit 1; }
10
11set -ex
12
13cp --reflink "$indev" "$outdev"~
14time xorriso \
15 -drive_class clear_list all \
16 -outdev "$outdev"~ -indev "$outdev"~ \
17 -report_about mishap \
18 -volid "$volid" \
19 -return_with sorry 0 \
20 -pathspecs on -overwrite on \
21 -add_plainly any \
22 "${btrfs_layer_iso_path}=${btrfs_layer_filesystem}"
23mv -f "$outdev"~ "$outdev"