Skip to content

Instantly share code, notes, and snippets.

@JamesOBenson
Last active May 12, 2023 18:08
Show Gist options
  • Save JamesOBenson/5c045b91c7f6443c8ec30212cc9f29c9 to your computer and use it in GitHub Desktop.
Save JamesOBenson/5c045b91c7f6443c8ec30212cc9f29c9 to your computer and use it in GitHub Desktop.
Automated way to convert the latest Amazon Linux 2 image to an OpenStack compatible image
#!/bin/bash
# This idea was from Ales Nosek at https://alesnosek.com/blog/2018/04/21/booting-amazon-linux-2-on-openstack/
# This is an automated way to download Amazon's Linux 2 image and import it into openstack.
local_file=$(find . -name "*openrc.sh" -type f -print -quit)
# If not found, search in /etc/kolla/
if [ -z "$local_file" ]; then
kolla_file=$(find /etc/kolla/ -name "*openrc.sh" -type f -print -quit)
fi
# Source the file if found
if [ -n "$local_file" ]; then
source "$local_file"
echo "sourced $local_file"
elif [ -n "$kolla_file" ]; then
source "$kolla_file"
echo "sourced $kolla_file"
else
echo "openrc not found."
exit 0
fi
if python3 -c 'import pkgutil; exit(not pkgutil.find_loader("openstackclient"))'; then
echo 'Openstack client found'
else
echo "Openstack client not found"
pip3 install python-openstackclient
fi
echo "Downloading AWS AMI image...."
url="https://cdn.amazonlinux.com/os-images/latest/"
title=$(curl -Ls "$url" | grep -oP '<title>Index of /os-images/\K[^<]+')
version_url="https://cdn.amazonlinux.com/os-images/$title/kvm/"
link=$(curl -s "$version_url" | grep -Eo 'href="[^\"]+\.qcow2"' | cut -d'"' -f2)
curl --progress-bar "$version_url""$link" -o ec2.qcow 2>&1
sudo apt install -y qemu-utils
echo "Converting qcow to raw file...."
qemu-img convert -f qcow2 -O raw ec2.qcow amzn2-kvm.raw
fdisk -l amzn2-kvm.raw
sector_size="$(fdisk -l amzn2-kvm.raw | grep 'Sector size' | awk '{print $4}')"
starting_sector="$(fdisk -l amzn2-kvm.raw | grep 'Linux' | awk '{print $2}')"
offset=$((sector_size * starting_sector))
sudo mount -o loop,offset="$offset" amzn2-kvm.raw /mnt
file="/mnt/etc/cloud/cloud.cfg"
line="datasource_list: [ NoCloud, AltCloud, ConfigDrive, OVF, None ]"
echo "Updating line in file..."
if grep -Fxq "$line" "$file"; then
# Update the line with the OpenStack at the beginning of the list
awk '/datasource_list: \[ NoCloud, AltCloud, ConfigDrive, OVF, None \]/{sub(/NoCloud/, "OpenStack, NoCloud")}1' $file >temp.txt
sudo mv temp.txt $file
echo "Line modified successfully."
else
echo "Line not found in the file."
fi
grep datasource_list "$file" | grep --color -i OpenStack
echo "Check line above has OpenStack in it!"
sleep 5
sudo umount /mnt
echo "Converting raw file to qcow2"
qemu-img convert -f raw -O qcow2 amzn2-kvm.raw amzn2-kvm.qcow2
openstack image create --progress --min-disk 25 --min-ram 512 --public --container-format bare --disk-format qcow2 --property os_type=linux --property os_version="$title" --property architecture=x86_64 --file amzn2-kvm.qcow2 Amazon_Linux_2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment