blob: 8d6e73077cc318419b8f3ef287e6c9ffb1c711f4 (
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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#! /usr/bin/env bash
# Upload new or upgrade existing machine subvolume on a Build OS build host.
#
# -k - keep the old subvolume on the target host
# -u <user> - target user instead of build
# -d <dir> - target machines directory instead of /build/machines/default
#
# <host> - build host to upload to (as user build by default)
# <new-subvol> - machine subvolume to upload
# <old-subvol> - previous machine subvolume (btrfs send -p)
#
usage="usage: $0 [<options>] <host> <new-subvol> [<old-subvol>]"
owd="$(pwd)"
trap "{ cd '$owd'; exit 1; }" ERR
set -o errtrace -o pipefail # Trap in functions.
# Fail if any pipe command fails.
function info () { echo "$*" 1>&2; }
function error () { info "$*"; exit 1; }
keep=false
user=build
machines=/build/machines/default
while [ "$#" -gt 0 ]; do
case "$1" in
-k)
keep=true
shift
;;
-u)
shift
user="$1"
shift
;;
-d)
shift
machines="$1"
shift
;;
-*)
error "unknown option: $1"
;;
*)
break
;;
esac
done
host="$1"
newsv="${2%/}"
oldsv="${3%/}"
if [ -z "$host" -o -z "$newsv" ]; then
error "$usage"
fi
host="$user@$host"
newsv_name="$(sed -n -re 's%^(.*/)?([^/]+)$%\2%p' <<<"$newsv")"
oldsv_name="$(sed -n -re 's%^(.*/)?([^/]+)$%\2%p' <<<"$oldsv")"
if [ -z "$newsv_name" ]; then
error "unable to extract subvolume name from '$newsv'"
fi
if [ -n "$oldsv" -a -z "$oldsv_name" ]; then
error "unable to extract subvolume name from '$oldsv'"
fi
# Get the machine link (<name>-<P>) and name.
#
mlink="$(sed -n -re 's/^(.+-[0-9]+)\.[0-9]+$/\1/p' <<<"$newsv_name")"
mname="$(sed -n -re 's/^(.+)-[0-9]+$/\1/p' <<<"$mlink")"
if [ -z "$mlink" -o -z "$mname" ]; then
error "unable to extract machine link/name from '$newsv'"
fi
# Subvolume paths on target host.
#
newsv_host="$machines/$mname/$newsv_name"
oldsv_host="$machines/$mname/$oldsv_name"
# Make sure subvolumes are read-only.
#
function check_ro () # <subvol>
{
local r;
r="$(btrfs property get -ts "$1" ro)"
if [ "$r" != "ro=true" ]; then
info "subvolume '$1' is not read-only; to change, run:"
info " btrfs property set -ts $1 ro true"
exit 1
fi
}
check_ro "$newsv"
if [ -n "$oldsv" ]; then
check_ro "$oldsv"
fi
# btrfs send command
#
send=(sudo btrfs send)
if [ -n "$oldsv" ]; then
send+=(-p "$oldsv")
fi
send+=("$newsv")
set -x
# Make sure the machine directory exists.
#
ssh "$host" mkdir -p "$machines/$mname"
# Send the snapshot over.
#
sudo "${send[@]}" | ssh "$host" sudo btrfs receive "$machines/$mname/"
# Adjust machine ownership.
#
ssh "$host" sudo btrfs property set -ts "$newsv_host" ro false
ssh "$host" sudo chown "$user:$user" "$newsv_host"
ssh "$host" sudo chown "$user:$user" "$newsv_host/*"
ssh "$host" btrfs property set -ts "$newsv_host" ro true
# Atomically switch the current machine.
#
ssh "$host" "cd $machines/$mname && ln -s $newsv_name new-$mlink"
ssh "$host" "cd $machines/$mname && mv -T new-$mlink $mlink"
# Remove the old machine subvolume.
#
{ set +x; } 2>/dev/null
if [ -z "$oldsv" -o "$keep" = true ]; then
exit 0
fi
set -x
ssh "$host" btrfs property set -ts "$oldsv_host" ro false
ssh "$host" btrfs subvolume delete "$oldsv_host"
|