aboutsummaryrefslogtreecommitdiff
path: root/upload-machine
diff options
context:
space:
mode:
Diffstat (limited to 'upload-machine')
-rwxr-xr-xupload-machine54
1 files changed, 37 insertions, 17 deletions
diff --git a/upload-machine b/upload-machine
index 556f824..b726429 100755
--- a/upload-machine
+++ b/upload-machine
@@ -2,24 +2,27 @@
# Upload new or upgrade existing machine subvolume on a Build OS build host.
#
-# -k - keep the old subvolume on the 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')
+# <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>]"
-machines=/build/machines/default
-
owd="$(pwd)"
trap "{ cd '$owd'; exit 1; }" ERR
-set -o errtrace # Trap in functions.
+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
@@ -27,6 +30,16 @@ while [ "$#" -gt 0 ]; do
keep=true
shift
;;
+ -u)
+ shift
+ user="$1"
+ shift
+ ;;
+ -d)
+ shift
+ machines="$1"
+ shift
+ ;;
-*)
error "unknown option: $1"
;;
@@ -44,7 +57,7 @@ if [ -z "$host" -o -z "$newsv" ]; then
error "$usage"
fi
-host="build@$host"
+host="$user@$host"
newsv_name="$(sed -n -re 's%^(.*/)?([^/]+)$%\2%p' <<<"$newsv")"
oldsv_name="$(sed -n -re 's%^(.*/)?([^/]+)$%\2%p' <<<"$oldsv")"
@@ -66,32 +79,31 @@ if [ -z "$mlink" -o -z "$mname" ]; then
error "unable to extract machine link/name from '$newsv'"
fi
-# Subvolume paths on build host.
+# 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>
+function make_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
+ info "subvolume '$1' is read-write, changing to read-only"
+ btrfs property set -ts "$1" ro true
fi
}
-check_ro "$newsv"
+make_ro "$newsv"
if [ -n "$oldsv" ]; then
- check_ro "$oldsv"
+ make_ro "$oldsv"
fi
# btrfs send command
#
-send=(sudo btrfs send)
+send=(btrfs send)
if [ -n "$oldsv" ]; then
send+=(-p "$oldsv")
fi
@@ -109,9 +121,17 @@ 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 build:build "$newsv_host"
-ssh "$host" sudo chown build:build "$newsv_host/*"
+# Recent btrfs-progs require the force flag (-f) (Debian bug #1019377).
+# Turns out btrfs now strips the subvolume uuid if we make it rw, which
+# will prevent it from being used as a base for incremental send.
+#
+# @@ Maybe we should just keep the original as is (for incremental send)
+# and make another copy where we change the ownership? Note: will also
+# need to update remove-machine.
+#
+ssh "$host" sudo btrfs property set -f -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.