summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xset-authorized-keys31
1 files changed, 31 insertions, 0 deletions
diff --git a/set-authorized-keys b/set-authorized-keys
new file mode 100755
index 0000000..2151bc9
--- /dev/null
+++ b/set-authorized-keys
@@ -0,0 +1,31 @@
+#!/bin/bash
+
+set -eu -o pipefail
+
+die() {
+ echo "ERROR: $*" 1>&2
+ exit 1
+}
+
+cleanup() {
+ umount "$drive" || true
+ rmdir "$mnt"
+}
+
+trap cleanup EXIT
+
+drive="$1"
+pubkey="$2"
+
+[ -e "$drive" ] || die "$drive does not exist"
+[ -e "$pubkey" ] || die "$pubkey does not exist"
+
+mnt="$(mktemp -d)"
+mount "$drive" "$mnt"
+if [ ! -e "$mnt/root/.ssh" ]; then
+ install -d "$mnt/root/.ssh"
+fi
+
+authz="$mnt/root/.ssh/authorized_keys"
+[ ! -e "$authz" ] || die "$authz already exists"
+install -m 0600 "$pubkey" "$authz"