Skip to content

Instantly share code, notes, and snippets.

@smarteist
Created February 6, 2026 16:58
Show Gist options
  • Select an option

  • Save smarteist/6406a8f6c315694c046726cb383ae3a6 to your computer and use it in GitHub Desktop.

Select an option

Save smarteist/6406a8f6c315694c046726cb383ae3a6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
# Configuration Variables
VM_NAME="VM1"
CPU_CORES="2"
RAM_SIZE="2048" # VirtualBox requires RAM in MB
VRAM_SIZE="16" # VRAM in MB
DISK_SIZE="20G"
USERNAME="root"
PASSWORD="111111"
UBUNTU_VERSION="22.04"
ARCH="amd64"
IMG_NAME="ubuntu-${UBUNTU_VERSION}-server-cloudimg-${ARCH}.img"
VDI_IMG="${VM_NAME}.vdi"
CLOUD_INIT_CFG="cloud-init.yaml"
CLOUD_INIT_ISO="cloud-init.iso"
# Port Mappings (Host:Guest)
PORT_MAPPINGS=(
"2222:22"
"3333:3306"
"4443:443"
"3003:3003"
"3128:3128"
"1500:500"
"4500:4500"
"1701:1701"
"1194:1194"
)
# URLs
BASE_URL="https://cloud-images.ubuntu.com/releases/${UBUNTU_VERSION}/release"
IMG_URL="${BASE_URL}/${IMG_NAME}"
# Check dependencies
if ! command -v VBoxManage &> /dev/null; then
echo "Error: VirtualBox is not installed."
exit 1
fi
# Download Ubuntu Cloud Image
if [ ! -f "$IMG_NAME" ]; then
echo "Downloading Ubuntu image..."
wget -q --show-progress "$IMG_URL"
fi
# Resize and Convert Image
if [ ! -f "$VDI_IMG" ]; then
echo "Preparing disk image..."
# Create a temp copy to avoid modifying the original download
cp "$IMG_NAME" temp.img
# Resize the raw image first (reliable)
qemu-img resize temp.img "$DISK_SIZE"
# Convert the resized image to VDI
echo "Converting to VDI..."
qemu-img convert -O vdi temp.img "$VDI_IMG"
# Cleanup
rm temp.img
fi
# Generate Cloud-Init ISO
if [ ! -f "$CLOUD_INIT_ISO" ]; then
echo "Generating Cloud-Init ISO..."
cat >$CLOUD_INIT_CFG <<EOF
#cloud-config
system_info:
default_user:
name: ${USERNAME}
password: ${PASSWORD}
chpasswd: { expire: False }
hostname: ${VM_NAME}
ssh_pwauth: True
package_update: true
package_upgrade: true
runcmd:
- sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
- systemctl restart ssh
EOF
cloud-localds "$CLOUD_INIT_ISO" "$CLOUD_INIT_CFG"
fi
# Clean up existing VM if needed
if VBoxManage showvminfo "$VM_NAME" &> /dev/null; then
echo "Warning: VM '$VM_NAME' already exists. Stop and delete it to recreate."
exit 1
fi
# Create VM
echo "Creating VirtualBox VM..."
VBoxManage createvm --name "$VM_NAME" --ostype "Ubuntu_64" --register
# Configure System Settings
VBoxManage modifyvm "$VM_NAME" \
--cpus "$CPU_CORES" \
--memory "$RAM_SIZE" \
--vram "$VRAM_SIZE" \
--nic1 nat \
--audio none \
--rtcuseutc on
# Configure Port Forwarding
echo "Configuring network rules..."
for mapping in "${PORT_MAPPINGS[@]}"; do
HOST_PORT=${mapping%%:*}
GUEST_PORT=${mapping##*:}
RULE_NAME="rule_${HOST_PORT}_${GUEST_PORT}"
VBoxManage modifyvm "$VM_NAME" --natpf1 "${RULE_NAME},tcp,,${HOST_PORT},,${GUEST_PORT}"
done
# Storage Controller (SATA)
VBoxManage storagectl "$VM_NAME" --name "SATA Controller" --add sata --controller IntelAHCI
# Attach OS Disk (Port 0)
VBoxManage storageattach "$VM_NAME" --storagectl "SATA Controller" \
--port 0 --device 0 --type hdd --medium "$VDI_IMG"
# Attach Cloud-Init ISO (Port 1)
VBoxManage storageattach "$VM_NAME" --storagectl "SATA Controller" \
--port 1 --device 0 --type dvddrive --medium "$CLOUD_INIT_ISO"
# Start VM
echo "Starting VM (Headless)..."
VBoxManage startvm "$VM_NAME" --type headless
echo "VM is booting. Connect via: ssh -p 2222 ${USERNAME}@localhost"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment