From 7dbcb516a338b9c660b752a2d9e858390d3aa2f7 Mon Sep 17 00:00:00 2001 From: crt Date: Sat, 29 Jun 2024 04:54:18 +0200 Subject: [PATCH] Actually made progress --- JOURNAL.md | 9 +++- README.md | 29 ++++++++++++ image-builder/image_builder.sh | 80 ++++++++++++++++++++++++++++++++++ image-builder/params.conf | 42 ++++++++++++++++++ image-builder/script.sh | 22 ++++++++++ 5 files changed, 180 insertions(+), 2 deletions(-) create mode 100755 image-builder/image_builder.sh create mode 100644 image-builder/params.conf create mode 100644 image-builder/script.sh diff --git a/JOURNAL.md b/JOURNAL.md index a880181..27f3723 100644 --- a/JOURNAL.md +++ b/JOURNAL.md @@ -17,8 +17,7 @@ - NGINX - Jellyfin - Vaultwarden - - Gitea - - (Optional for now) Mailcow + - Mailcow ## 2024-02-23 - Time mostly spent on Getting my new Laptop up and running due to the previous ones Motherboard failing. - Set up Proxmox Cluster for testing and Deploying above mentioned services in LXQ and started configuration of VPN gateway for remote access. @@ -29,3 +28,9 @@ - Goals for next lesson : - Figure out how to load balance with nginx and how to create my own LXQ Templates - Finish setting up VPN Gateway + +## 2024-06-28 +- Waking up from my Coma +- Most of the project is done but undocumented +- Overdose on Caffeine and Write Documentation + diff --git a/README.md b/README.md index e9ff84a..782a4f4 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,32 @@ ## Journal See here : [JOURNAL.md](JOURNAL.md) + +## What this Project covers? +- Making Custom LXQ Container +- Managing a Proxmox VE Environement +- Demonstrating example services + +## The Basics +Covers the basics neede to know to make sense of this + +### What is Proxmox VE +Proxmox VE is an open-source server management platform designed for enterprise virtualisation. +It allows you to create and manage virtual machines and containers using a pretty UI either on a single server over over a cluster of servers. + +### What is an LXC Container? +An LXC Container in Proxmox VE is a lighteweight virtualization method that allow you to run multiple isolated Linux Systems (Containers) on a single Host using the shared Linux Kernel. + +## Getting started + +### What you will need +- 1x or more Proxmox Node(s) +- Linux Computer +- Incase you wish to publicly offer your services a virtual router + +### How to use image_builder.sh +image_builder.sh is a bash script written by me that helps making custom proxmox container templates. + +A preconofigured version can be found in /image_builder in this repo + +# Todo document this (its 5am and i want to sleep rn) diff --git a/image-builder/image_builder.sh b/image-builder/image_builder.sh new file mode 100755 index 0000000..c8222fb --- /dev/null +++ b/image-builder/image_builder.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +# Load parameters from params.conf +source params.conf + +# Function to find the lowest free container ID starting from MIN_ID +find_lowest_free_id() { + local id=$MIN_ID + while pct status $id &>/dev/null || qm status $id &>/dev/null; do + ((id++)) + done + echo $id +} + +# Get the container ID +if [ -z "$CTID" ]; then + CTID=$(find_lowest_free_id) +fi + +# Generate disk name +DISK_NAME="vm-${CTID}-disk-0" + +# Determine the current directory +CURRENT_DIR=$(pwd) + +# Set default values for EXPORT_PATH and SCRIPT if not set in params.conf +EXPORT_PATH=${EXPORT_PATH:-$CURRENT_DIR} +SCRIPT=${SCRIPT:-$CURRENT_DIR/script.sh} +STORAGE=${STORAGE:-local-btrfs} +DISK_SIZE=${DISK_SIZE:-8} + +# Full rootfs parameter +ROOTFS="${STORAGE}:${DISK_SIZE}" + +# Check if the script file exists +if [ ! -f "$SCRIPT" ]; then + echo "Script file $SCRIPT does not exist." + exit 1 +fi + +# Check if the container already exists on the local node +if pct status $CTID &>/dev/null; then + echo "VM $CTID already exists on this node." + exit 1 +fi + +# Create the container with the correct storage format +if ! pct create $CTID $TEMPLATE --rootfs $ROOTFS --cores $CORES --memory $RAM --swap $SWAP --net0 name=eth0,bridge=$BRIDGE,ip=$IP --hostname $HOSTNAME; then + echo "Failed to create the container." + exit 1 +fi + +# Start the container +if ! pct start $CTID; then + echo "Failed to start the container." + exit 1 +fi + +# Run the shell script in the container +if ! pct exec $CTID -- /bin/bash -c "$(cat $SCRIPT)"; then + echo "Failed to run the script inside the container." + pct stop $CTID + exit 1 +fi + +# Stop the container +if ! pct stop $CTID; then + echo "Failed to stop the container." + exit 1 +fi + +# Export the container as a tar.gz file +EXPORT_NAME=${EXPORT_NAME:-vzdump-lxc-$CTID} +BACKUP_FILE=$EXPORT_PATH/$EXPORT_NAME.tar.gz +if ! vzdump $CTID --dumpdir $EXPORT_PATH --compress gzip; then + echo "Failed to export the container." + exit 1 +fi + +echo "Container $CTID has been created, configured, and exported to $BACKUP_FILE" diff --git a/image-builder/params.conf b/image-builder/params.conf new file mode 100644 index 0000000..ae4273f --- /dev/null +++ b/image-builder/params.conf @@ -0,0 +1,42 @@ + +TEMPLATE=adastor:vztmpl/debian-12-standard_12.2-1_amd64.tar.zst +# Set your Baseimage here + +RAM=1024 +# Define RAM during setup + +SWAP=512 +# Define SWAP during setup + +CORES=2 +# Define Cores during setup + +BRIDGE=vmbr0 +# Define Network interface during Installation + +IP=dhcp +# Define IP During installation (use DHCP, proper static V4 not implemented) + +CTID= +# Manually Override CTID, not needed usually + +HOSTNAME=nginx-debian +# Set Temporary Hostname + +EXPORT_NAME=mycontainer_backup +# Doesnt work anyways so yea + +EXPORT_PATH= +# Leave this empty to use the current directory + +SCRIPT= +# Leave this empty to use script.sh in the current directory + +MIN_ID=200 +# Define minimum ID for CT to avoid conflicts with other nodes if in a cluster + +STORAGE=local-btrfs +# Set what storage pool to use + +DISK_SIZE=8 +# Set the default disk size for the container diff --git a/image-builder/script.sh b/image-builder/script.sh new file mode 100644 index 0000000..a21e106 --- /dev/null +++ b/image-builder/script.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +# Ensure the locale environment variables are set correctly +export LANGUAGE=en_US.UTF-8 +export LANG=en_US.UTF-8 +export LC_ALL=en_US.UTF-8 + +# Install locales and configure locale settings +apt-get update +apt-get install -y locales +locale-gen en_US.UTF-8 + +# Update and install packages for Debian +apt-get update +apt-get upgrade -y +apt-get install -y nginx + +# Enable nginx service +systemctl enable nginx + +# Clean up +apt-get clean