Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scripts for creating disk images #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions create-image/create-docker-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash
set -o errexit -o pipefail -o nounset

if [[ $# != 3 ]]; then
echo "Usage: create-docker-image.sh <images,...> <disk-size> <image-name>"
exit 1
fi

if [[ "${2:(-2)}" != "GB" ]] && [[ "${2:(-2)}" != "TB" ]]; then
echo "Disk size should be XXGB or XXTB"
exit 1
fi


ZONE=us-central1-a
SCRIPT=$(mktemp -t "run-script-XXXXX")

echo "VM startup script: ${SCRIPT}"
DATA=/mnt/disks/data
cat <<EOF >${SCRIPT}
set -o errexit -o xtrace
sudo mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard \
/dev/sdb
sudo mkdir -p "${DATA}"
sudo mount -o discard,defaults /dev/sdb "${DATA}"
sudo chmod a+w "${DATA}"

mount --bind "${DATA}" /var/lib/docker/overlay2
mkdir "${DATA}/l"

IFS=","
IMAGES="$1"
for IMAGE in \$IMAGES; do
docker pull "\${IMAGE}"
done

/sbin/poweroff
EOF

DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
. "$DIR/run-script.sh"

run_script "${SCRIPT}" "$2" "${ZONE}" "$3"
rm "${SCRIPT}"

47 changes: 47 additions & 0 deletions create-image/create-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash
set -o errexit -o pipefail -o nounset

if [[ $# != 3 ]]; then
echo "Usage: create-image.sh <gs://source/bucket> <disk-size> <image-name>"
exit 1
fi

if [[ "${1:0:5}" != "gs://" ]]; then
echo "Source must be a Google Storage bucket"
exit 1
fi

if [[ "${2:(-2)}" != "GB" ]] && [[ "${2:(-2)}" != "TB" ]]; then
echo "Disk size should be XXGB or XXTB"
exit 1
fi

echo "Files to copy:"
gsutil ls "${1}"

ZONE=us-central1-a
SCRIPT=$(mktemp -t "run-script-XXXXX")

echo "VM startup script: ${SCRIPT}"
DATA=/mnt/disks/data
cat <<EOF >${SCRIPT}
set -e -x
sudo mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard \
/dev/sdb
sudo mkdir -p "${DATA}"
sudo mount -o discard,defaults /dev/sdb "${DATA}"
sudo chmod a+w "${DATA}"

docker run -v "${DATA}":"${DATA}" \
google/cloud-sdk:slim gsutil cp -r "${1}" "${DATA}"

/sbin/poweroff
EOF

DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
. "$DIR/run-script.sh"

run_script "${SCRIPT}" "$2" "${ZONE}" "$3"
rm "${SCRIPT}"

43 changes: 43 additions & 0 deletions create-image/run-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

#######################################
# Run a script on a VM and create a disk image.
# Arguments:
# Script to run, a path.
# Disk size to pass to GCE, a string.
# Zone to run in, a string.
# Output image name, a string.
# #######################################
function run_script() {
local NAME=$(echo "run-script-${RANDOM}")
local DISK_NAME="${NAME}-disk"
local SCRIPT="$1"
local SIZE="$2"
local ZONE="$3"
local IMAGE="$4"

echo "Creating instance..."
gcloud compute instances create "${NAME}" \
--zone "${ZONE}" --image-family cos-stable --image-project cos-cloud \
--create-disk "name=${DISK_NAME},size=${SIZE},auto-delete=yes" \
--metadata-from-file startup-script="${SCRIPT}"

echo -n "Waiting for VM..."
while true; do
local STATUS="$(gcloud compute instances describe ${NAME} \
--zone ${ZONE} --format='get(status)')"
if [[ "${STATUS}" != "RUNNING" ]]; then
break
fi
sleep 10
echo -n "."
done
echo "Done"

echo "Creating image..."
gcloud compute images create "${IMAGE}" \
--source-disk "${DISK_NAME}" --source-disk-zone "${ZONE}" --force

echo "Deleting VM..."
gcloud compute instances delete "${NAME}" -q --zone "${ZONE}"
}