Skip to content

Instantly share code, notes, and snippets.

@privsim
Last active November 28, 2024 11:52
Show Gist options
  • Select an option

  • Save privsim/908bc3409bdaa8d952fd410a0d323f11 to your computer and use it in GitHub Desktop.

Select an option

Save privsim/908bc3409bdaa8d952fd410a0d323f11 to your computer and use it in GitHub Desktop.
Creates a Fedora Server VM on Proxmox with security verification
#!/usr/bin/env bash
# Fedora Server VM creation script for Proxmox
# Version: 1.0
# Description: Creates a Fedora Server VM with security verification
# Usage: bash -c "$(wget -qLO - https://gist.githubusercontent.com/privsim/908bc3409bdaa8d952fd410a0d323f11/raw/fedora-vm.sh)"
set -e
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
function error_handler() {
local exit_code="$?"
local line_number="$1"
local command="$2"
echo -e "\n\033[01;31m[ERROR]\033[m in line ${line_number}: exit code ${exit_code} while executing command ${command}"
cleanup_vmid
}
function cleanup_vmid() {
if qm status $VMID &>/dev/null; then
qm stop $VMID &>/dev/null
qm destroy $VMID &>/dev/null
fi
}
function header_info {
clear
cat <<"EOF"
____ __
/ __/__ ___ ____/ /__ _________ _
/ _// -_) _ \/ __/ / _ \/ __/ __ /
/_/ /\__/_//_/\__/_/\___/_/ \_,_/
Proxmox Fedora VM Creator
EOF
}
# Color codes
YW="\033[33m"
BL="\033[36m"
RD="\033[01;31m"
BGN="\033[4;92m"
GN="\033[1;92m"
DGN="\033[32m"
CL="\033[m"
BFR="\\r\\033[K"
HOLD="-"
CM="${GN}✓${CL}"
CROSS="${RD}✗${CL}"
# Message functions
function msg_info() { echo -ne " ${HOLD} ${YW}$1...${CL}"; }
function msg_ok() { echo -e "${BFR} ${CM} ${GN}$1${CL}"; }
function msg_error() { echo -e "${BFR} ${CROSS} ${RD}$1${CL}"; }
# Check requirements
function check_requirements() {
if [[ "$(id -u)" -ne 0 ]]; then
msg_error "Please run as root"
exit 1
fi
if ! command -v pveversion >/dev/null 2>&1; then
msg_error "Not running on Proxmox VE"
exit 1
fi
if ! pveversion | grep -Eq "pve-manager/8.[0-9]"; then
msg_error "This script requires Proxmox VE 8.x"
exit 1
fi
}
# Main setup
msg_info "Checking requirements"
check_requirements
msg_ok "Requirements met"
# Get next VM ID
VMID=$(pvesh get /cluster/nextid)
msg_ok "Using VM ID: $VMID"
# Base configuration
HN="fedora"
CORE_COUNT="2"
RAM_SIZE="2048"
BRG="vmbr0"
MAC=$(echo '02:' ; openssl rand -hex 5 | sed 's/\(..\)/\1:/g; s/.$//')
msg_info "Downloading and verifying Fedora Server image"
TEMP_DIR=$(mktemp -d)
cd $TEMP_DIR
wget -q --show-progress https://download.fedoraproject.org/pub/fedora/linux/releases/41/Server/x86_64/images/Fedora-Server-KVM-41-1.4.x86_64.qcow2
wget -q https://fedoraproject.org/fedora.gpg
wget -q https://download.fedoraproject.org/pub/fedora/linux/releases/41/Server/x86_64/images/Fedora-Server-41-1.4-x86_64-CHECKSUM
if ! gpgv --keyring ./fedora.gpg Fedora-Server-41-1.4-x86_64-CHECKSUM; then
msg_error "GPG verification failed"
exit 1
fi
if ! sha256sum -c <(grep qcow2 Fedora-Server-41-1.4-x86_64-CHECKSUM); then
msg_error "Checksum verification failed"
exit 1
fi
msg_ok "Image verified"
# Create VM
msg_info "Creating Fedora VM"
qm create $VMID \
-agent 1 \
-tablet 0 \
-localtime 1 \
-bios ovmf \
-cores $CORE_COUNT \
-memory $RAM_SIZE \
-name $HN \
-net0 virtio,bridge=$BRG,macaddr=$MAC \
-onboot 1 \
-ostype l26 \
-scsihw virtio-scsi-pci
# Storage setup
STORAGE=$(pvesm status -content images | awk 'NR>1 {print $1; exit}')
DISK0=vm-${VMID}-disk-0.qcow2
DISK1=vm-${VMID}-disk-1.qcow2
pvesm alloc $STORAGE $VMID $DISK0 4M 1>&/dev/null
qm importdisk $VMID Fedora-Server-KVM-41-1.4.x86_64.qcow2 $STORAGE -format qcow2 1>&/dev/null
qm set $VMID \
-efidisk0 ${STORAGE}:$VMID/$DISK0,efitype=4m \
-scsi0 ${STORAGE}:$VMID/$DISK1,size=4G \
-boot order=scsi0 \
-serial0 socket \
-description "Fedora Server 41 VM created via gist"
msg_ok "Created Fedora Server VM (${HN})"
# Start VM
msg_info "Starting Fedora Server VM"
qm start $VMID
msg_ok "Started Fedora Server VM"
# Cleanup
cd /
rm -rf $TEMP_DIR
msg_ok "Setup completed successfully!"
echo -e "\nFedora Server VM $VMID is now running!"
echo -e "Access the console through the Proxmox web interface\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment