mirror of
https://github.com/flatcar/scripts.git
synced 2025-11-30 15:02:06 +01:00
Fixed authorized_keys file creation
Fix to create .ssh directory before the authorized_keys file. The option -k renamed to -a to match another scripts. Added verification if the VBoxManage exists.
This commit is contained in:
parent
6bdaf88b80
commit
431d71a90f
@ -1,13 +1,15 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
VERSION_ID=alpha
|
VERSION_ID=alpha
|
||||||
SSH_KEY=""
|
SSH_KEYS=""
|
||||||
|
CORE_UID=500
|
||||||
|
CORE_GID=500
|
||||||
|
|
||||||
USAGE="Usage: $0 [-V version] [-d /target/path] [-k /path/id_rsa.pub]
|
USAGE="Usage: $0 [-V version] [-d /target/path] [-a authorized_keys]
|
||||||
Options:
|
Options:
|
||||||
-d DEST Create CoreOS VDI image to the given path.
|
-d DEST Create CoreOS VDI image to the given path.
|
||||||
-V VERSION Version to install (e.g. alpha) [default: ${VERSION_ID}]
|
-V VERSION Version to install (e.g. alpha) [default: ${VERSION_ID}]
|
||||||
-k SSH_KEY SSH public key file to be added to authorized_keys.
|
-a FILE SSH public keys for login access. [~/.ssh/id_{dsa,rsa}.pub]
|
||||||
-h This help
|
-h This help
|
||||||
|
|
||||||
This tool creates a CoreOS VDI image to be used with VirtualBox.
|
This tool creates a CoreOS VDI image to be used with VirtualBox.
|
||||||
@ -92,12 +94,12 @@ BeiJ6tEeDHDzdA==
|
|||||||
-----END PGP PUBLIC KEY BLOCK-----
|
-----END PGP PUBLIC KEY BLOCK-----
|
||||||
"
|
"
|
||||||
|
|
||||||
while getopts "V:d:k:h" OPTION
|
while getopts "V:d:a:h" OPTION
|
||||||
do
|
do
|
||||||
case $OPTION in
|
case $OPTION in
|
||||||
V) VERSION_ID="$OPTARG" ;;
|
V) VERSION_ID="$OPTARG" ;;
|
||||||
d) DEST="$OPTARG" ;;
|
d) DEST="$OPTARG" ;;
|
||||||
k) SSH_KEY="$OPTARG" ;;
|
a) SSH_KEYS="$OPTARG" ;;
|
||||||
h) echo "$USAGE"; exit;;
|
h) echo "$USAGE"; exit;;
|
||||||
*) exit 1;;
|
*) exit 1;;
|
||||||
esac
|
esac
|
||||||
@ -109,16 +111,25 @@ if [ $(id -u) -ne 0 ]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# SSH key is required
|
# VirtualBox tools required
|
||||||
if [[ -z "${SSH_KEY}" ]]; then
|
which VBoxManage &>/dev/null
|
||||||
echo "$0: No SSH key provided." >&2
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "$0: VBoxManage tool is required to convert image." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ ! -f "${SSH_KEY}" ]]; then
|
# Verify provided keys file
|
||||||
echo "$0: SSH key file (${SSH_KEY}) do not exists." >&2
|
if [[ -n "${SSH_KEYS}" ]]; then
|
||||||
|
if [[ ! -f "${SSH_KEYS}" ]]; then
|
||||||
|
echo "$0: SSH keys file not found: ${SSH_KEYS}." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
# SSH keys file was not provided, setting to default
|
||||||
|
SSH_KEYS=~/.ssh/id_*.pub
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if [[ ! -d "${DEST}" ]]; then
|
if [[ ! -d "${DEST}" ]]; then
|
||||||
echo "$0: Target path (${DEST}) do not exists." >&2
|
echo "$0: Target path (${DEST}) do not exists." >&2
|
||||||
@ -187,15 +198,22 @@ if [[ -z "${PART_OFFSET}" ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
MOUNT_DEST="${WORKDIR}/rootfs"
|
MOUNT_DEST="${WORKDIR}/rootfs"
|
||||||
AUTHORIZED_KEYS="${MOUNT_DEST}/home/core/.ssh/authorized_keys"
|
CORE_SSH_DIR="${MOUNT_DEST}/home/core/.ssh"
|
||||||
|
AUTHORIZED_KEYS="${CORE_SSH_DIR}/authorized_keys"
|
||||||
|
|
||||||
echo "Adding SSH key to authorized keys file..."
|
echo "Adding SSH key to authorized keys file..."
|
||||||
mkdir -p "${MOUNT_DEST}"
|
mkdir -p "${MOUNT_DEST}"
|
||||||
mount -t btrfs -o loop,offset=${PART_OFFSET},subvol=root "${DOWN_IMAGE}" "${MOUNT_DEST}"
|
mount -t btrfs -o loop,offset=${PART_OFFSET},subvol=root "${DOWN_IMAGE}" "${MOUNT_DEST}"
|
||||||
trap "umount '${MOUNT_DEST}' && rm -rf '${WORKDIR}'" EXIT
|
trap "umount '${MOUNT_DEST}' && rm -rf '${WORKDIR}'" EXIT
|
||||||
|
|
||||||
cat "${SSH_KEY}" > ${AUTHORIZED_KEYS}
|
if [ ! -d "${CORE_SSH_DIR}" ]; then
|
||||||
|
mkdir -p ${CORE_SSH_DIR}
|
||||||
|
chmod 0600 ${CORE_SSH_DIR}
|
||||||
|
fi
|
||||||
|
|
||||||
|
cat ${SSH_KEYS} > ${AUTHORIZED_KEYS}
|
||||||
chmod 0600 ${AUTHORIZED_KEYS}
|
chmod 0600 ${AUTHORIZED_KEYS}
|
||||||
|
chown -R $CORE_UID:$CORE_GID "${CORE_SSH_DIR}"
|
||||||
|
|
||||||
umount "${MOUNT_DEST}"
|
umount "${MOUNT_DEST}"
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user