Created
December 18, 2024 17:47
-
-
Save MattCurryCom/59f82f46f5c802a5f2935cfcad684cb3 to your computer and use it in GitHub Desktop.
converts multi part ami's into ova
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Ensure the script exits on error | |
set -e | |
# Function to check if a command exists | |
function command_exists() { | |
command -v "$1" &> /dev/null | |
} | |
# Variables | |
OVFTOOL_URL="https://github.com/rgl/ovftool-binaries/raw/main/archive/VMware-ovftool-4.6.0-21452615-lin.x86_64.zip" | |
OVFTOOL_ZIP="VMware-ovftool.zip" | |
INSTALL_DIR="/usr/local/ovftool" | |
AMI_ID="$1" | |
OUTPUT_OVF="output.ova" | |
WORK_DIR="/tmp/ami_conversion" | |
# Ensure an AMI ID is provided | |
if [ -z "$AMI_ID" ]; then | |
echo "Usage: $0 <AMI_ID>" | |
exit 1 | |
fi | |
# Detect OS | |
if [ -f /etc/os-release ]; then | |
. /etc/os-release | |
OS="$ID" | |
else | |
echo "Unable to detect operating system. Exiting." | |
exit 1 | |
fi | |
echo "Detected operating system: $OS" | |
# Update and install dependencies | |
case "$OS" in | |
"ubuntu") | |
echo "Updating packages for Ubuntu..." | |
sudo apt update -y | |
sudo apt upgrade -y | |
echo "Installing dependencies for Ubuntu..." | |
sudo apt install -y awscli qemu-utils tar wget unzip jq | |
;; | |
"amzn" | "amazon") | |
echo "Updating packages for Amazon Linux..." | |
sudo yum update -y | |
echo "Installing dependencies for Amazon Linux..." | |
sudo yum install -y aws-cli qemu-img tar wget unzip jq | |
;; | |
*) | |
echo "Unsupported operating system: $OS" | |
exit 1 | |
;; | |
esac | |
# Download and install OVF Tool | |
if ! command_exists ovftool; then | |
echo "Downloading OVF Tool from $OVFTOOL_URL..." | |
wget -O "$OVFTOOL_ZIP" "$OVFTOOL_URL" | |
echo "Extracting OVF Tool..." | |
sudo mkdir -p "$INSTALL_DIR" | |
sudo unzip -o "$OVFTOOL_ZIP" -d "$INSTALL_DIR" | |
rm -f "$OVFTOOL_ZIP" | |
echo "Adding OVF Tool to PATH..." | |
if ! grep -q "export PATH=\$PATH:$INSTALL_DIR" ~/.bashrc; then | |
echo "export PATH=\$PATH:$INSTALL_DIR" >> ~/.bashrc | |
source ~/.bashrc | |
fi | |
else | |
echo "VMware OVFTool is already installed." | |
fi | |
# Prepare working directory | |
mkdir -p "$WORK_DIR" | |
cd "$WORK_DIR" | |
# Get AMI information | |
echo "Fetching AMI information..." | |
INSTANCE_ID=$(aws ec2 run-instances --image-id "$AMI_ID" --count 1 --instance-type t2.micro --query 'Instances[0].InstanceId' --output text) | |
echo "Launched instance: $INSTANCE_ID" | |
echo "Waiting for instance to initialize..." | |
aws ec2 wait instance-running --instance-ids "$INSTANCE_ID" | |
# Attach volumes to instance | |
echo "Fetching block device mappings..." | |
BLOCK_DEVICES=$(aws ec2 describe-instances --instance-ids "$INSTANCE_ID" --query 'Reservations[0].Instances[0].BlockDeviceMappings[*].Ebs.VolumeId' --output text) | |
for VOLUME_ID in $BLOCK_DEVICES; do | |
echo "Attaching volume: $VOLUME_ID" | |
aws ec2 detach-volume --volume-id "$VOLUME_ID" | |
aws ec2 attach-volume --volume-id "$VOLUME_ID" --instance-id "$INSTANCE_ID" --device /dev/sdf | |
done | |
# Create disk images | |
DISK_INDEX=1 | |
for DEVICE in $(ls /dev/xvd* | grep -v xvda); do | |
echo "Creating disk image for $DEVICE..." | |
qemu-img convert -O raw "$DEVICE" "disk$DISK_INDEX.raw" | |
DISK_INDEX=$((DISK_INDEX + 1)) | |
done | |
# Convert raw disks to OVF | |
echo "Converting disks to OVF..." | |
ovftool --diskMode=monolithicSparse --name="AMI_OVF" *.raw "$OUTPUT_OVF" | |
# Cleanup | |
echo "Terminating instance..." | |
aws ec2 terminate-instances --instance-ids "$INSTANCE_ID" | |
echo "Conversion complete. OVF saved as $OUTPUT_OVF." |
Notes:
- Ensure the AWS CLI is configured with appropriate permissions to run EC2 instances and manage volumes.
- Modify the instance type (t2.micro) in the script as needed based on your AMI requirements.
- The script assumes all volumes are accessible and uses /dev/xvd* for disk devices. Adjust as needed for your environment.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Features: