mirror of
https://github.com/Security-Onion-Solutions/securityonion.git
synced 2025-12-06 09:12:45 +01:00
improvements to createvm
This commit is contained in:
@@ -1,23 +1,69 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Ensure /root/create_vm/var/lib/libvirt/images exists
|
||||
# Place this script in /root/create_vm
|
||||
# Download OL9U5_x86_64-kvm-b253.qcow2 from https://yum.oracle.com/oracle-linux-templates.html, place in /root/create_vm/
|
||||
|
||||
# These steps will be removed from the process to create the final image and is being used for development
|
||||
# This is used for the user-data auth portion of cloud-init
|
||||
# Create passwd hash:
|
||||
# python3 -c 'import crypt; print(crypt.crypt("YOUR_PASSWD_HERE", crypt.mksalt(crypt.METHOD_SHA512)))'
|
||||
# Create ssh keypair:
|
||||
# ssh-keygen -t ed25519 -C "soqemussh" -f ~/.ssh/soqemussh
|
||||
|
||||
# Run the script: createvm.sh coreol9Small 205G
|
||||
# IP options may be removed for final version
|
||||
|
||||
# After running the script, the following will be output:
|
||||
#[root@jppvirtman create_vm]# ll var/lib/libvirt/images/coreol9Small/
|
||||
#total 610376
|
||||
#-rw-r--r--. 1 root root 380928 Dec 20 14:33 coreol9Small-cidata.iso
|
||||
#-rw-r--r--. 1 root root 624623616 Dec 20 14:33 coreol9Small.qcow2
|
||||
#-rw-r--r--. 1 root root 55 Dec 20 14:32 meta-data
|
||||
#-rw-r--r--. 1 root root 333 Dec 20 14:32 network-config
|
||||
#-rw-r--r--. 1 root root 1047 Dec 20 14:32 user-data
|
||||
|
||||
# These files are now scp to a hypervisor node
|
||||
# Place the files in /var/lib/libvirt/images/coreol9Small (or whatever is the same as the vm name)
|
||||
# Create your storage pool as instructed by the script. this is only needed if one doesn't already exist
|
||||
# Run the virt-install command as instructed by the script
|
||||
|
||||
# Could add the following to the final runcmd in the user-data to fill the disk to avoid the cons of thin provisioning the disk
|
||||
# - dd if=/dev/zero of=/tmp/fill bs=1M || true
|
||||
# - rm -f /tmp/fill
|
||||
|
||||
# Exit on any error
|
||||
set -e
|
||||
|
||||
# Set variables and defaults
|
||||
VM=${1:-"small-vm"} # VM name
|
||||
IP=${2:-"192.168.1.10"} # IP address
|
||||
GATEWAY=${3:-"192.168.1.1"} # Gateway
|
||||
DNS=${4:-"192.168.1.1"} # Comma-separated list of DNS servers
|
||||
DISK_SIZE=${2:-"205G"} # Disk size with unit (default 205G)
|
||||
IP=${3:-"192.168.1.10"} # IP address
|
||||
GATEWAY=${4:-"192.168.1.1"} # Gateway
|
||||
DNS=${5:-"192.168.1.1"} # Comma-separated list of DNS servers
|
||||
MAC_ADDRESS="52:54:00:f2:c3:df" # Default MAC - will be overridden if found
|
||||
|
||||
# Show usage if help is requested
|
||||
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
|
||||
echo "Usage: $0 <vm_name> <ip> <gateway> <dns_servers>"
|
||||
echo "Example: $0 myvm 192.168.1.50 192.168.1.1 8.8.8.8,8.8.4.4"
|
||||
echo "Usage: $0 <vm_name> <disk_size> <ip> <gateway> <dns_servers>"
|
||||
echo "Example: $0 myvm 100G 192.168.1.50 192.168.1.1 8.8.8.8,8.8.4.4"
|
||||
echo "Parameters:"
|
||||
echo " vm_name : Name of the VM (default: small-vm)"
|
||||
echo " disk_size : Size of the disk with unit G/M (default: 205G)"
|
||||
echo " ip : IP address (default: 192.168.1.10)"
|
||||
echo " gateway : Gateway address (default: 192.168.1.1)"
|
||||
echo " dns_servers: Comma-separated DNS servers (default: 192.168.1.1)"
|
||||
echo "All parameters are optional and will use defaults if not specified"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Validate disk size format
|
||||
if ! [[ $DISK_SIZE =~ ^[0-9]+[GM]$ ]]; then
|
||||
echo "Error: Disk size must be a number followed by G (gigabytes) or M (megabytes)"
|
||||
echo "Example: 100G or 51200M"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Convert comma-separated DNS servers to yaml format
|
||||
format_dns() {
|
||||
local IFS=','
|
||||
@@ -61,12 +107,6 @@ ethernets:
|
||||
$(format_dns "$DNS")
|
||||
EOF
|
||||
|
||||
# Generate password hash using:
|
||||
# Create passwd hash
|
||||
### python3 -c 'import crypt; print(crypt.crypt("YOUR_PASSWD_HERE", crypt.mksalt(crypt.METHOD_SHA512)))'
|
||||
# Create ssh keypair
|
||||
### ssh-keygen -t ed25519 -C "soqemussh" -f ~/.ssh/soqemussh
|
||||
|
||||
# Create user-data with network configuration
|
||||
cat > user-data << EOF
|
||||
#cloud-config
|
||||
@@ -83,7 +123,7 @@ users:
|
||||
lock_passwd: false
|
||||
passwd: $(echo '___YOUR_HASH_HERE___')
|
||||
ssh-authorized-keys:
|
||||
- ssh-ed25519 ___YOUR_PUB_KEY_HERE___
|
||||
- ssh-ed25519 ___YOUR_PUB_KEY_HERE___ soqemussh
|
||||
|
||||
# Configure where output will go
|
||||
output:
|
||||
@@ -95,12 +135,6 @@ ssh_genkeytypes: ['ed25519', 'rsa']
|
||||
# set timezone for VM
|
||||
timezone: UTC
|
||||
|
||||
# Don't preallocate the entire disk space
|
||||
#resize_rootfs: true
|
||||
#growpart:
|
||||
# mode: auto
|
||||
# devices: ['/']
|
||||
|
||||
# Install QEMU guest agent. Enable and start the service
|
||||
packages:
|
||||
- qemu-guest-agent
|
||||
@@ -115,26 +149,29 @@ runcmd:
|
||||
- xfs_growfs /dev/vg_main/lv_root
|
||||
EOF
|
||||
|
||||
# First, copy the base image
|
||||
# First, copy the base image with progress
|
||||
echo "Creating base VM image..."
|
||||
cp -v /root/create_vm/OL9U5_x86_64-kvm-b253.qcow2 $VM.qcow2
|
||||
rsync --progress /root/create_vm/OL9U5_x86_64-kvm-b253.qcow2 $VM.qcow2
|
||||
|
||||
# First resize the image to our desired size
|
||||
echo "Resizing image..."
|
||||
qemu-img resize $VM.qcow2 205G
|
||||
# Resize the image to specified size
|
||||
echo "Resizing image to $DISK_SIZE..."
|
||||
echo "Current image size: $(qemu-img info $VM.qcow2 | grep 'virtual size' | cut -d':' -f2 | cut -d'(' -f1 | tr -d ' ')"
|
||||
qemu-img resize -f qcow2 $VM.qcow2 $DISK_SIZE
|
||||
echo "New image size: $(qemu-img info $VM.qcow2 | grep 'virtual size' | cut -d':' -f2 | cut -d'(' -f1 | tr -d ' ')"
|
||||
|
||||
# Now compress it
|
||||
# Now compress it with progress
|
||||
echo "Compressing image..."
|
||||
qemu-img convert -p -O qcow2 -c $VM.qcow2 $VM-compressed.qcow2
|
||||
mv -v $VM-compressed.qcow2 $VM.qcow2
|
||||
|
||||
# Create a cloud-init ISO with network config
|
||||
# Create a cloud-init ISO with network config and progress indication
|
||||
echo "Creating cloud-init ISO..."
|
||||
mkisofs -output $VM-cidata.iso -volid CIDATA -rock user-data meta-data network-config
|
||||
mkisofs -output $VM-cidata.iso -volid CIDATA -rock -verbose user-data meta-data network-config
|
||||
|
||||
# Echo the configuration for verification
|
||||
echo "Creating VM with the following network configuration:"
|
||||
echo "Creating VM with the following configuration:"
|
||||
echo "VM Name: $VM"
|
||||
echo "Disk Size: $DISK_SIZE"
|
||||
echo "IP Address: $IP"
|
||||
echo "Gateway: $GATEWAY"
|
||||
echo "DNS Servers: $DNS"
|
||||
|
||||
Reference in New Issue
Block a user