Last active
November 3, 2023 12:38
-
-
Save mattonit/cebf4c951910adfdef1d9cd99479bc07 to your computer and use it in GitHub Desktop.
vmcreate.sh
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 | |
# Bail out on any error | |
set -e | |
# Check for root privileges | |
if [[ $EUID -ne 0 ]]; then | |
echo "This script must be run as root" | |
exit 1 | |
fi | |
# Usage message | |
USAGE="Usage: $0 <vm_name> <disk_size> <memory_size_in_MB>" | |
# Check if all three arguments are passed | |
if [ "$#" -ne 3 ]; then | |
echo "Error: You must provide exactly 3 arguments." | |
echo $USAGE | |
exit 1 | |
fi | |
# Assign arguments to variables | |
VM_NAME=$1 | |
DISK_SIZE=$2 | |
MEMORY_MB=$3 | |
echo "VM Name: $VM_NAME" | |
echo "Disk Size: $DISK_SIZE" | |
echo "Memory Size: $MEMORY_MB MB" | |
# Calculating RAM size in KiB | |
MEMORY_KiB=$((MEMORY_MB * 1024)) | |
# Base directory for VMs | |
BASE_DIR="/home/sf/virt" | |
# Path to the original image | |
BASE_IMAGE="${BASE_DIR}/images/CentOS-7-x86_64-GenericCloud.qcow2" | |
# Path to the new VM disk image | |
VM_DISK="${BASE_DIR}/vm/${VM_NAME}.qcow2" | |
# Path to the VM definition | |
VM_XML="${BASE_DIR}/defines/${VM_NAME}.xml" | |
# Check if VM already exists | |
if [ -f "$VM_DISK" ]; then | |
echo "Error: A disk for VM name $VM_NAME already exists." | |
exit 1 | |
fi | |
# Copying the base image to create a new VM disk | |
cp "$BASE_IMAGE" "$VM_DISK" | |
# Resizing disk | |
qemu-img resize "$VM_DISK" "$DISK_SIZE" | |
# Changing root password and setting hostname | |
virt-customize -a "$VM_DISK" --hostname "$VM_NAME" --root-password password:Krucjata1 --uninstall cloud-init | |
# Create the VM XML definition | |
cat > "$VM_XML" <<EOF | |
<domain type='kvm'> | |
<name>$VM_NAME</name> | |
<memory unit='KiB'>$MEMORY_KiB</memory> | |
<currentMemory unit='KiB'>$MEMORY_KiB</currentMemory> | |
<vcpu>1</vcpu> | |
<os> | |
<type arch='x86_64'>hvm</type> | |
<boot dev='hd'/> | |
</os> | |
<clock offset='utc'/> | |
<on_poweroff>destroy</on_poweroff> | |
<on_reboot>restart</on_reboot> | |
<on_crash>destroy</on_crash> | |
<devices> | |
<emulator>/usr/libexec/qemu-kvm</emulator> | |
<disk type='file' device='disk'> | |
<driver name='qemu' type='qcow2'/> | |
<source file='$VM_DISK'/> | |
<target dev='vda' bus='virtio'/> | |
</disk> | |
<interface type='bridge'> | |
<source bridge='br1'/> | |
<model type='virtio'/> | |
</interface> | |
<serial type='pty'> | |
<target port='0'/> | |
</serial> | |
<console type='pty'> | |
<target type='serial' port='0'/> | |
</console> | |
</devices> | |
</domain> | |
EOF | |
# Defining VM | |
virsh define "$VM_XML" | |
# Launching VM | |
virsh start "$VM_NAME" | |
echo "$VM_NAME has been created and started." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment