Skip to content

Instantly share code, notes, and snippets.

@dmc5179
Created September 2, 2020 00:55
Show Gist options
  • Save dmc5179/6c84bb080ddb879d5fd3d343e4ceaa16 to your computer and use it in GitHub Desktop.
Save dmc5179/6c84bb080ddb879d5fd3d343e4ceaa16 to your computer and use it in GitHub Desktop.
Importing RedHat CoreOS RAW Disk image into AWS to create an AMI
#!/bin/bash -xe
RHCOS_VERSION="4.5.6"
RHCOS_TMP="/tmp"
S3_BUCKET="mybucket"
# Change to the temporary directory
pushd "${RHCOS_TMP}"
# NOTE: Uncomment the curl command if you need to download the disk
# This script is assuming that has been done and we are now in the disconnected world
# Download the RHCOS metal image tarball
#curl -o https://mirror.openshift.com/pub/openshift-v4/x86_64/dependencies/rhcos/${RHCOS_VERSION:0:3}/${RHCOS_VERSION}/rhcos-${RHCOS_VERSION}-x86_64-metal.x86_64.raw.gz
# Unpack the RHCOS metal image tarball
#gunzip rhcos-${RHCOS_VERSION}-x86_64-metal.x86_64.raw.gz
cat << EOF > ./containers.json
{
"Description": "Red Hat CoreOS ${RHCOS_VERSION} RAW Disk",
"Format": "RAW",
"UserBucket": {
"S3Bucket": "${S3_BUCKET}",
"S3Key": "rhcos-${RHCOS_VERSION}-x86_64-metal.x86_64.raw"
}
}
EOF
# Copy the raw disk to S3
# It is best to just copy the image to the bucket root
# The default roles for snapshot import tend not to work well with sub paths
aws s3 cp "${RHCOS_TMP}/rhcos-${RHCOS_VERSION}-x86_64-metal.x86_64.raw" "s3://${S3_BUCKET}/rhcos-${RHCOS_VERSION}-x86_64-metal.x86_64.raw"
# Import the image as a snapshot
IMPORT_ID=$(aws ec2 import-snapshot \
--description "Red Hat CoreOS ${RHCOS_VERSION} RAW Disk" \
--disk-container "file:///tmp/containers.json" \
--output text --query 'ImportTaskId')
echo "Import ID: ${IMPORT_ID}"
echo "Use the following command to monitor the import process:"
echo "aws ec2 describe-import-snapshot-tasks --import-task-ids ${IMPORT_ID}"
echo "Waiting until the import completes"
STATUS="unknown"
until [[ ${STATUS} == deleted || ${STATUS} == completed ]]
do
STATUS=$(aws ec2 describe-import-snapshot-tasks --import-task-ids ${IMPORT_ID} --output text --query 'ImportSnapshotTasks[*].SnapshotTaskDetail.Status')
echo "Status: ${STATUS}"
sleep 30
done
echo "Registering the image"
# Get the snapshot ID from the import task
SNAPSHOT_ID=$(aws ec2 describe-import-snapshot-tasks --import-task-ids \
${IMPORT_ID} --output text --query 'ImportSnapshotTasks[*].SnapshotTaskDetail.SnapshotId')
# Register the image
aws ec2 register-image \
--name "Red Hat CoreOS ${RHCOS_VERSION} RAW Disk" \
--block-device-mappings \
"[{\"DeviceName\": \"/dev/xvda\",\"Ebs\":{\"VolumeSize\":16,\"VolumeType\":\"gp2\",\"DeleteOnTermination\":true,\"SnapshotId\":\"${SNAPSHOT_ID}\"}}]" \
--root-device-name '/dev/xvda' \
--architecture x86_64 \
--description "Red Hat CoreOS ${RHCOS_VERSION} RAW Disk" \
--virtualization-type hvm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment