summaryrefslogtreecommitdiff
path: root/set-authorized-keys
blob: 2151bc94de71de81a34eb077e4ee5ed3b783ccc6 (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
#!/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"