diff options
Diffstat (limited to 'others/vm3')
| -rwxr-xr-x | others/vm3/.config/cloud-init-generator/debian12.sh | 67 | ||||
| -rwxr-xr-x | others/vm3/.config/cloud-init-generator/fedora40.sh | 105 | ||||
| -rwxr-xr-x | others/vm3/.config/cloud-init-generator/freebsd14.0.sh | 75 | ||||
| -rwxr-xr-x | others/vm3/.config/cloud-init-generator/freebsd14.0.shbackup | 59 | ||||
| -rw-r--r-- | others/vm3/.config/cloud-init-generator/rocky9.0.sh | 53 | ||||
| -rwxr-xr-x | others/vm3/.config/cloud-init-generator/ubuntu24.04.sh | 62 | ||||
| -rwxr-xr-x | others/vm3/.config/init/install_packages.sh | 12 | ||||
| -rwxr-xr-x | others/vm3/.config/init/make_rootkey.sh | 4 | ||||
| -rwxr-xr-x | others/vm3/compute/create.sh | 123 | ||||
| -rwxr-xr-x | others/vm3/compute/ls.sh | 45 | ||||
| -rwxr-xr-x | others/vm3/compute/rm.sh | 27 | ||||
| -rwxr-xr-x | others/vm3/compute/shutdown.sh | 6 | ||||
| -rwxr-xr-x | others/vm3/compute/start.sh | 20 | ||||
| -rw-r--r-- | others/vm3/default.xml | 13 | ||||
| -rw-r--r-- | others/vm3/docs | 121 | ||||
| -rw-r--r-- | others/vm3/meta-data | 2 | ||||
| -rw-r--r-- | others/vm3/user-data | 27 | ||||
| -rwxr-xr-x | others/vm3/vm.sh | 119 |
18 files changed, 0 insertions, 940 deletions
diff --git a/others/vm3/.config/cloud-init-generator/debian12.sh b/others/vm3/.config/cloud-init-generator/debian12.sh deleted file mode 100755 index 6bfa54b..0000000 --- a/others/vm3/.config/cloud-init-generator/debian12.sh +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/bash -# generates user data, meta data, and seed.iso for cloud init. for debian -# requires the name of the vm as an argument -# -# osinfo-query os to list all available vm types to deploy. debian is debian12 -# use no cloud and some other tools because cloud init does not work -# -#qemu-img resize debian-11-generic-amd64.qcow2 +10G # try this if you want to install something big like kde -# sudo apt update -# sudo apt install kde-plasma-desktop - -# -image_url="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-nocloud-amd64.qcow2" -sshkeysdir="/root/k" -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir -dir_path="/var/lib/libvirt/images/.image_store" -src_file="${dir_path}/debian12.qcow2" -config_dir="/var/lib/libvirt/images/.temp" -user_data="${config_dir}/user-data" -meta_data="${config_dir}/meta-data" -seed_iso="${config_dir}/seed.iso" -# note that cloud init specifically looks for "user-data" and "meta-data" in the seed.iso disk. do not deviate from the naming convention. -if [ $# -ne 1 ]; then - echo "Usage: $0 <vm-name>" - exit 1 -fi -sudo mkdir -p "$dir_path" # make image store dir -sudo mkdir -p "$config_dir" - -[ ! -f "$src_file" ] && echo "source image does not exist! downloading..." && sudo wget -O "$src_file" "$image_url" - -# generate all the lines from the file -SSH_INJECT_OPTS=$(find ${sshkeysdir} -name "*.pub" -exec echo "--ssh-inject user:file:{}" \;) - -flag_file="${dir_path}/debian12_customized" - -# NOTE to delete customfile if debqcow2 is deleted. otherwise it will be ambiguous -# this to check if image is already customised -if [ -f "$flag_file" ]; then - echo "Image already customized, skipping customization..." -else - echo "Customizing image..." - # to install virt customise tool - sudo dnf install -y libguestfs libguestfs-tools libvirt virt-install virt-manager guestfs-tools - export LIBGUESTFS_BACKEND=direct - sudo -E virt-customize -a /var/lib/libvirt/images/.image_store/debian12.qcow2 \ - --run-command 'useradd -m -s /bin/bash user' \ - $SSH_INJECT_OPTS \ - --update \ - --install openssh-server,git,cloud-guest-utils,tmux \ - --run-command 'echo "user ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers.d/user' \ - --run-command 'sed -i "s/PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config' \ - --run-command 'sed -i "s/#PasswordAuthentication yes/PasswordAuthentication no/" /etc/ssh/sshd_config' \ - --run-command 'cd /home/user && git clone https://git.0nom.ch/keys && chown -R user:user keys && sudo -u user ./keys/add-ssh-keys.sh' \ - --run-command 'tee -a /etc/bash.bashrc > /dev/null << "EOF" -LS_COLORS=$LS_COLORS:'\''di=38;5;135:ex=00;32:'\'' ; export LS_COLORS -PS1='\''[\[\033[01;32m\]\u\[\033[00m\]@\h \[\033[38;5;135m\]\W\[\033[00m\]]\$ '\'' -EOF' \ - --run-command 'tee -a /home/user/.bashrc > /dev/null << "EOF" -LS_COLORS=$LS_COLORS:'\''di=38;5;135:ex=00;32:'\'' ; export LS_COLORS -PS1='\''[\[\033[01;32m\]\u\[\033[00m\]@\h \[\033[38;5;135m\]\W\[\033[00m\]]\$ '\'' -EOF' \ - --run-command 'echo "sudo growpart /dev/vda 1 && sudo resize2fs /dev/vda1 && source /etc/bash.bashrc" > /home/user/start.sh && chmod +x /home/user/start.sh' && \ - - # Create flag file after successful customization - sudo touch "$flag_file" -fi diff --git a/others/vm3/.config/cloud-init-generator/fedora40.sh b/others/vm3/.config/cloud-init-generator/fedora40.sh deleted file mode 100755 index bd3e0c3..0000000 --- a/others/vm3/.config/cloud-init-generator/fedora40.sh +++ /dev/null @@ -1,105 +0,0 @@ -#!/bin/bash - -# generates user data, meta data, and seed.iso for cloud init. for fedora -# requires the name of the vm as an argument -# -# osinfo-query os to list all available vm types to deploy. fedora is fedora 40 - - -image_url="https://download.fedoraproject.org/pub/fedora/linux/releases/41/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-41-1.4.x86_64.qcow2" - -sshkeysdir="/root/k" - -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir -dir_path="/var/lib/libvirt/images/.image_store" -src_file="${dir_path}/fedora40.qcow2" -config_dir="/var/lib/libvirt/images/.temp" -user_data="${config_dir}/user-data" -meta_data="${config_dir}/meta-data" -seed_iso="${config_dir}/seed.iso" -# note that cloud init specifically looks for "user-data" and "meta-data" in the seed.iso disk. do not deviate from the naming convention. - -if [ $# -ne 1 ]; then - echo "Usage: $0 <vm-name>" - exit 1 -fi - -sudo mkdir -p "$dir_path" -sudo mkdir -p "$config_dir" - -[ ! -f "$src_file" ] && echo "source image does not exist! downloading..." && sudo wget -O "$src_file" "$image_url" - -cat > "$user_data" << EOF -#cloud-config -users: - - name: user - ssh-authorized-keys: -EOF - -for key in $sshkeysdir/*.pub; do - echo " - $(cat "$key")" >> $user_data -done - - - - - - - - - -cat >> "$user_data" << 'EOF' - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - groups: wheel - shell: /bin/bash -runcmd: - - sudo growpart /dev/sda 1 - - sudo xfs_growfs / - - | - cat > /etc/yum.repos.d/local-fedora.repo << 'FOE' - [local-fedora] - name=Local Fedora Mirror - baseurl=http://192.168.124.227/fedora-base - enabled=1 - gpgcheck=0 - priority=1 - [local-updates] - name=Local Fedora Updates - baseurl=http://192.168.124.227/fedora-updates - enabled=1 - gpgcheck=0 - priority=1 - [local-updates-testing] - name=Local Fedora Updates Testing - baseurl=http://192.168.124.227/fedora-updates-testing - enabled=1 - gpgcheck=0 - priority=1 - FOE - - sudo sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/fedora.repo - - sudo sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/fedora-updates.repo - - sudo sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/fedora-updates-testing.repo - - sudo dnf install -y vim git - - cd /home/user - - git clone https://git.0nom.ch/setup - - sudo ./setup/setup.sh - - touch /home/user/runcmd_done -EOF - - - - - - - - - -cat > "$meta_data" << EOF -instance-id: vm_id -local-hostname: $1 -EOF - -genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; } - -echo "Configuration files generated successfully" - diff --git a/others/vm3/.config/cloud-init-generator/freebsd14.0.sh b/others/vm3/.config/cloud-init-generator/freebsd14.0.sh deleted file mode 100755 index c498efa..0000000 --- a/others/vm3/.config/cloud-init-generator/freebsd14.0.sh +++ /dev/null @@ -1,75 +0,0 @@ -#!/bin/bash -# generates configuration for FreeBSD VM deployment -# requires the name of the vm as an argument -image_url="https://download.freebsd.org/releases/VM-IMAGES/14.2-RELEASE/amd64/Latest/FreeBSD-14.2-RELEASE-amd64-BASIC-CLOUDINIT.zfs.qcow2.xz" -sshkeysdir="/root/k" -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -dir_path="/var/lib/libvirt/images/.image_store" -src_file="${dir_path}/freebsd14.0.qcow2" -config_dir="/var/lib/libvirt/images/.temp" -user_data="${config_dir}/user-data" -meta_data="${config_dir}/meta-data" -seed_iso="${config_dir}/seed.iso" - -if [ $# -ne 1 ]; then - echo "Usage: $0 <vm-name>" - exit 1 -fi - -sudo mkdir -p "$dir_path" -sudo mkdir -p "$config_dir" - -# Download and extract FreeBSD image if it doesn't exist -if [ ! -f "$src_file" ]; then - echo "source image does not exist! downloading..." - sudo wget -O "${src_file}.xz" "$image_url" - sudo xz -d "${src_file}.xz" -fi - -cat > "$user_data" << EOF -#cloud-config -users: - - name: user - passwd: 'yourpassword' - lock_passwd: false - ssh-authorized-keys: -EOF - -for key in $sshkeysdir/*.pub; do - echo " - $(cat "$key")" >> $user_data -done - -cat >> "$user_data" << 'EOF' - groups: wheel - shell: /bin/tcsh - -# FreeBSD specific configuration -package_update: true -package_upgrade: true - -packages: - - vim-console - - git - - doas - -write_files: - - path: /usr/local/etc/doas.conf - content: | - permit nopass :wheel - permissions: '0600' - -runcmd: - - pw usermod user -s /bin/tcsh - - env ASSUME_ALWAYS_YES=YES pkg update - - env ASSUME_ALWAYS_YES=YES pkg upgrade - - env ASSUME_ALWAYS_YES=YES pkg install vim-console git doas - - touch /home/user/runcmd_done -EOF - -cat > "$meta_data" << EOF -instance-id: vm_id -local-hostname: $1 -EOF - -genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; } -echo "Configuration files generated successfully" diff --git a/others/vm3/.config/cloud-init-generator/freebsd14.0.shbackup b/others/vm3/.config/cloud-init-generator/freebsd14.0.shbackup deleted file mode 100755 index 81fcc72..0000000 --- a/others/vm3/.config/cloud-init-generator/freebsd14.0.shbackup +++ /dev/null @@ -1,59 +0,0 @@ -#!/bin/bash -# generates configuration for FreeBSD VM deployment -# requires the name of the vm as an argument - -image_url="https://download.freebsd.org/releases/VM-IMAGES/14.2-RELEASE/amd64/Latest/FreeBSD-14.2-RELEASE-amd64-BASIC-CLOUDINIT.zfs.qcow2.xz" -sshkeysdir="/root/k" -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -dir_path="/var/lib/libvirt/images/.image_store" -src_file="${dir_path}/freebsd14.0.qcow2" -config_dir="/var/lib/libvirt/images/.temp" -user_data="${config_dir}/user-data" -meta_data="${config_dir}/meta-data" -seed_iso="${config_dir}/seed.iso" - -if [ $# -ne 1 ]; then - echo "Usage: $0 <vm-name>" - exit 1 -fi - -sudo mkdir -p "$dir_path" -sudo mkdir -p "$config_dir" - -# Download and extract FreeBSD image if it doesn't exist -if [ ! -f "$src_file" ]; then - echo "source image does not exist! downloading..." - sudo wget -O "${src_file}.xz" "$image_url" - sudo xz -d "${src_file}.xz" -fi - -# Note: FreeBSD uses a different user setup mechanism -cat > "$user_data" << EOF -#cloud-config -users: - - name: user - ssh-authorized-keys: -EOF - -for key in $sshkeysdir/*.pub; do - echo " - $(cat "$key")" >> $user_data -done - -cat >> "$user_data" << 'EOF' - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - groups: wheel - shell: /bin/sh -runcmd: - - pw usermod user -s /bin/sh - - pkg update -f - - pkg install -y vim git sudo - - touch /home/user/runcmd_done -EOF - -cat > "$meta_data" << EOF -instance-id: vm_id -local-hostname: $1 -EOF - -genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; } -echo "Configuration files generated successfully" diff --git a/others/vm3/.config/cloud-init-generator/rocky9.0.sh b/others/vm3/.config/cloud-init-generator/rocky9.0.sh deleted file mode 100644 index c880206..0000000 --- a/others/vm3/.config/cloud-init-generator/rocky9.0.sh +++ /dev/null @@ -1,53 +0,0 @@ - -#!/bin/bash -# generates user data, meta data, and seed.iso for cloud init. for rocky -# requires the name of the vm as an argument -# -# osinfo-query os to list all available vm types to deploy. rocky is rocky9.0 or similar -image_url="https://dl.rockylinux.org/pub/rocky/9/images/x86_64/Rocky-9-GenericCloud-Base.latest.x86_64.qcow2" -sshkeysdir="/root/k" -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir -dir_path="/var/lib/libvirt/images/.image_store" -src_file="${dir_path}/rocky9.0.qcow2" -config_dir="/var/lib/libvirt/images/.temp" -user_data="${config_dir}/user-data" -meta_data="${config_dir}/meta-data" -seed_iso="${config_dir}/seed.iso" -# note that cloud init specifically looks for "user-data" and "meta-data" in the seed.iso disk. do not deviate from the naming convention. -if [ $# -ne 1 ]; then - echo "Usage: $0 <vm-name>" - exit 1 -fi -sudo mkdir -p "$dir_path" -sudo mkdir -p "$config_dir" -[ ! -f "$src_file" ] && echo "source image does not exist! downloading..." && sudo wget -O "$src_file" "$image_url" -cat > "$user_data" << EOF -#cloud-config -users: - - name: user - ssh-authorized-keys: -EOF -for key in $sshkeysdir/*.pub; do - echo " - $(cat "$key")" >> $user_data -done -cat >> "$user_data" << 'EOF' - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - groups: wheel - shell: /bin/bash -runcmd: - - sudo systemctl enable --now serial-getty@ttyS0.service - - sudo growpart /dev/sda 1 - - sudo xfs_growfs / - - sudo dnf install -y vim git - - cd /home/user - - git clone https://git.0nom.ch/setup - - touch /home/user/runcmd_done -EOF -cat > "$meta_data" << EOF -instance-id: vm_id -local-hostname: $1 -EOF -genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; } -echo "Configuration files generated successfully" - - diff --git a/others/vm3/.config/cloud-init-generator/ubuntu24.04.sh b/others/vm3/.config/cloud-init-generator/ubuntu24.04.sh deleted file mode 100755 index 11e0a98..0000000 --- a/others/vm3/.config/cloud-init-generator/ubuntu24.04.sh +++ /dev/null @@ -1,62 +0,0 @@ -#!/bin/bash -# generates user data, meta data, and seed.iso for cloud init. for Ubuntu -# requires the name of the vm as an argument - -image_url="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img" -sshkeysdir="/root/k" -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -dir_path="/var/lib/libvirt/images/.image_store" -src_file="${dir_path}/ubuntu24.04.qcow2" -config_dir="/var/lib/libvirt/images/.temp" -user_data="${config_dir}/user-data" -meta_data="${config_dir}/meta-data" -seed_iso="${config_dir}/seed.iso" -temp_img="${dir_path}/ubuntu_temp.img" - -if [ $# -ne 1 ]; then - echo "Usage: $0 <vm-name>" - exit 1 -fi - -sudo mkdir -p "$dir_path" -sudo mkdir -p "$config_dir" - -if [ ! -f "$src_file" ]; then - echo "source image does not exist! downloading..." - sudo wget -O "$temp_img" "$image_url" - echo "Converting to qcow2 format..." - sudo qemu-img convert -f raw -O qcow2 "$temp_img" "$src_file" - sudo rm "$temp_img" -fi - -cat > "$user_data" << EOF -#cloud-config -users: - - name: ubuntu - ssh-authorized-keys: -EOF - -for key in $sshkeysdir/*.pub; do - echo " - $(cat "$key")" >> $user_data -done - -cat >> "$user_data" << 'EOF' - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - groups: sudo - shell: /bin/bash -runcmd: - - sudo growpart /dev/sda 1 - - sudo resize2fs /dev/sda1 - - sudo apt-get update - - sudo apt-get install -y vim git - - cd /home/ubuntu - - touch /home/ubuntu/runcmd_done -EOF - -cat > "$meta_data" << EOF -instance-id: vm_id -local-hostname: $1 -EOF - -genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; } -echo "Configuration files generated successfully" diff --git a/others/vm3/.config/init/install_packages.sh b/others/vm3/.config/init/install_packages.sh deleted file mode 100755 index d5ca81f..0000000 --- a/others/vm3/.config/init/install_packages.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -#install packages -packages=("nc" "htop" "wireguard-tools" "bind-utils" "tmux" "net-tools" "curl" "mlocate" "dnsmasq" "qemu-kvm" "libvirt" "libvirt-daemon-kvm" "virt-install" "virt-manager" "genisoimage" "bc") - -for package in "${packages[@]}"; do - if ! rpm -q "$package" &> /dev/null; then - sudo dnf install -y "$package" - fi -done - -sudo systemctl enable --now libvirtd diff --git a/others/vm3/.config/init/make_rootkey.sh b/others/vm3/.config/init/make_rootkey.sh deleted file mode 100755 index f725985..0000000 --- a/others/vm3/.config/init/make_rootkey.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -sudo mkdir -p /root/k -ssh-keygen -t ed25519 -f /root/k/k1 -N "" diff --git a/others/vm3/compute/create.sh b/others/vm3/compute/create.sh deleted file mode 100755 index ab481cd..0000000 --- a/others/vm3/compute/create.sh +++ /dev/null @@ -1,123 +0,0 @@ -#!/bin/bash - -# takes in vm name, os type, vcpu, ram, disk as argument -# takes in already generated seed iso and downloaded vm.iso file - -# default values -vcpu=8 -ram_gb=8 -disk_gb=64 -os="fedora40" -ostype="linux" - -# parse arguments -while [[ $# -gt 0 ]]; do - case $1 in - --vcpu) - vcpu="$2" - shift 2 - ;; - --ram) - ram_gb="$2" - shift 2 - ;; - --disk-size) - disk_gb="$2" - shift 2 - ;; - --image) - os="$2" - shift 2 - ;; - *) - # Handle positional arguments (vmname and os) - if [ -z "$vmname" ]; then - vmname="$1" - else - echo "Unknown argument: $1" - exit 1 - fi - shift - ;; - esac -done - -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir -workingdir="/var/lib/libvirt/images/.temp" -seed_iso="${workingdir}/seed.iso" -xml="${workingdir}/xml" - -image_dir_path="/var/lib/libvirt/images/.image_store" -src_file="${image_dir_path}/${os}.qcow2" -new_vm_config_dir="/var/lib/libvirt/images/${vmname}.config" -new_vm="/var/lib/libvirt/images/${vmname}.qcow2" - -# Check mandatory arguments, basicaly checking for initial 1 argument. if it dont exist, vmname will be null -# image is os!! -if [ -z "$vmname" ] ; then - echo "" - echo "Usage: $0 <vm-name> [--image <os>] [--vcpu N] [--ram N] [--disk-size N]" - echo "seed.iso and image file have to be present! Default os is fedora" - echo "" - echo "Available images:" - sudo ls -1 /var/lib/libvirt/images/.image_store | sed 's/\.qcow2$//' - echo "" - echo "Available images to download:" - sudo ls -1 "${scriptdir}/../.config/cloud-init-generator/" | sed 's/\.sh$//' - exit 1 -fi - -# run the script to make the cloud init files -sudo bash "${scriptdir}/../.config/cloud-init-generator/${os}.sh" "${vmname}" - -if [ ! -f "${src_file}" ]; then - echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}" - exit 1 -fi - -sudo mkdir -p $new_vm_config_dir - -sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; } - -if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then - echo -e "\n$vmname already exist. Delete it before using the same name." - exit 1 -fi - -if [[ ${os,,} == *"freebsd"* ]]; then - ostype="generic" -fi - -# Define the disk options based on OS type -if [ "$os" = "debian12" ]; then - disk_opts="--disk path=${new_vm},format=qcow2" - virt-customize -a ${new_vm} --run-command "rm -f /etc/machine-id && dbus-uuidgen --ensure=/etc/machine-id && echo ${vmname} > /etc/hostname" -else - disk_opts="--disk path=${new_vm},format=qcow2 --disk path=$seed_iso,device=cdrom" -fi - -generate_mac() { - printf "52:54:00:%02x:%02x:%02x\n" $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)) -} - -# Use the conditional disk options in virt-install -sudo virt-install --name $vmname \ - --vcpus $vcpu \ - --memory "$((ram_gb * 1024))" \ - $disk_opts \ - --os-type $ostype \ - --os-variant $os \ - --virt-type kvm \ - --graphics none \ - --network bridge=virbr0,model=virtio,mac=$(generate_mac) \ - --print-xml > $xml || { sudo rm -rf $new_vm; exit 1; } -# if you want this in a new storage pool, move it to a new storage pool after initialisation - -sudo virsh define $xml #&> /dev/null || { echo "Failed to define the new VM."; exit 1; } - -sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null - -sudo virsh start $vmname - -sudo rm "${workingdir}"/* - diff --git a/others/vm3/compute/ls.sh b/others/vm3/compute/ls.sh deleted file mode 100755 index b821fd1..0000000 --- a/others/vm3/compute/ls.sh +++ /dev/null @@ -1,45 +0,0 @@ - -#!/bin/bash -printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "Network" "IP" "State" "vCPUs" "RAM(GB)" "Disk(GB)" "Name" -printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "----------" "---------------" "--------" "-----" "-------" "-----------" "----------" - -# Get all VMs -vms=$(sudo virsh list --name --all) - -# Cache the network leases once -default_leases=$(sudo virsh net-dhcp-leases default 2>/dev/null) - -for vm in $vms; do - # Get XML once and use it multiple times - xml=$(sudo virsh dumpxml "$vm" 2>/dev/null) - - # Extract all data from the cached XML - mac=$(echo "$xml" | grep "mac address" | awk -F\' '{ print $2}') - net=$(echo "$xml" | grep "<source network" | awk -F\' '{print $2}') - if [ -z "$net" ]; then - net="default" - fi - - # Use cached leases - ip=$(echo "$default_leases" | grep "$mac" | awk '{print $5}' | cut -f1 -d'/') - - # Run commands in background and save to temp files - sudo virsh domstate "$vm" 2>/dev/null > /tmp/state.$$ & - echo "$xml" | grep "<vcpu" | awk -F'[<>]' '{print $3}' > /tmp/vcpus.$$ & - echo "$xml" | grep "<memory" | awk -F'[<>]' '{print $3}' | awk '{ printf "%.2f", $1/1048576 }' > /tmp/ram.$$ & - sudo du -sk "/var/lib/libvirt/images/${vm}.qcow2" 2>/dev/null | awk '{ printf "%.2f", $1/1024/1024 }' > /tmp/disk.$$ & - - wait - - # Read from temp files - state=$(cat /tmp/state.$$ 2>/dev/null) - vcpus=$(cat /tmp/vcpus.$$ 2>/dev/null) - ram=$(cat /tmp/ram.$$ 2>/dev/null) - disk=$(cat /tmp/disk.$$ 2>/dev/null) - - # Clean up temp files - rm -f /tmp/state.$$ /tmp/vcpus.$$ /tmp/ram.$$ /tmp/disk.$$ - - printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" \ - "$net" "$ip" "$state" "$vcpus" "$ram" "$disk" "$vm" -done diff --git a/others/vm3/compute/rm.sh b/others/vm3/compute/rm.sh deleted file mode 100755 index f77831c..0000000 --- a/others/vm3/compute/rm.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -# Check if VMs are provided -if [ $# -eq 0 ]; then - echo "Error: No VM names provided. Usage: $0 vm1 vm2 vm3" - exit 1 -fi - -# List VMs to be removed -echo "The following VMs will be permanently destroyed:" -for vm in "$@"; do - echo "- $vm" -done - -# Confirmation prompt -read -p "Are you sure you want to remove these VMs? (y/N): " confirmation -if [[ ! "$confirmation" =~ ^[Yy]$ ]]; then - echo "Operation cancelled." - exit 1 -fi - -for vm in "$@"; do - sudo virsh destroy "$vm" &>/dev/null - sudo virsh undefine "$vm" --remove-all-storage &>/dev/null - #sudo rm -rf "/var/lib/libvirt/images/${vm}" &>/dev/null - printf "%-50s%10s\n" "Removing $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") -done diff --git a/others/vm3/compute/shutdown.sh b/others/vm3/compute/shutdown.sh deleted file mode 100755 index 081499b..0000000 --- a/others/vm3/compute/shutdown.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -for vm in "$@"; do - sudo virsh shutdown "$vm" > /dev/null 2>&1 - printf "%-50s%10s\n" "Shutting down $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") -done diff --git a/others/vm3/compute/start.sh b/others/vm3/compute/start.sh deleted file mode 100755 index d64dbb1..0000000 --- a/others/vm3/compute/start.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash - -# starts vm -# remove seed.iso first if vm have the seed iso - -vmname=$1 - -# Get the target device for seed.iso -target_dev=$(sudo virsh domblklist $vmname | grep 'seed.iso' | awk '{print $1}') - -# If seed.iso is found, detach it, -# --config means it is persistent but do not affect the state now. -# --config --live means online remove -# no option means that the changes are not persistent -# -if [ ! -z "$target_dev" ]; then - sudo virsh detach-disk $vmname $target_dev --config -fi - -sudo virsh start $vmname diff --git a/others/vm3/default.xml b/others/vm3/default.xml deleted file mode 100644 index bf71f7c..0000000 --- a/others/vm3/default.xml +++ /dev/null @@ -1,13 +0,0 @@ -<network> - <name>default</name> - <uuid>3d6341fb-fad2-49c1-b54a-41d2027f56cc</uuid> - <forward mode='nat'/> - <bridge name='virbr0' stp='on' delay='0'/> - <mac address='52:54:00:73:4c:22'/> - <ip address='192.168.124.1' netmask='255.255.255.0'> - <dhcp> - <range start='192.168.124.2' end='192.168.124.254'/> - <option opt='61' value='ignore'/> - </dhcp> - </ip> -</network> diff --git a/others/vm3/docs b/others/vm3/docs deleted file mode 100644 index eea91d1..0000000 --- a/others/vm3/docs +++ /dev/null @@ -1,121 +0,0 @@ - -todo - make theseediso cloud init stuff happen inside the vm storage directory so that multiple poeple can use it ast the same time on the same system - fedora debian freebsd opensuse - change deployment for non-fedora to manually make a base image and copy ssh stuff over, then duplicate it however you want - - - -vm compute create name1 -os fedora -vcpu 1 -ram 1 -disk 10 -s.pool storage1 -add-disk 200 storage2 -add-network nw3 # default fedora 4,4,20 - # fedora not available. available os: listed down - # storage not available. please create it - # nw3 not available. create y/n? -vm network create <name> 192.168.100.1 /32 -vm network create <name> (auto) -vm storage create <name> /dev/ -vm storage create <name2> /dir/ - - -vm/ - ./vm.sh # Single entry point. one vm at a time(except for compute delete-all) - .config/ # all config files - init/ # run this everytime. try to think of a way to set a flag that says this device has been initialised - install_packages.sh - make_rootkey.sh # in /root/k/k1. can also add keys in here - /var/lib/libvirt/images/.image_store/ # stores the images - /var/lib/libvirt/images/.temp/ # for user-data, meta-data, and seed.iso(has ud and md inside) need to delete after initialising a new vm - image/ # vm cloud init templates to make user-data, meta-data, and seed.iso - fedora.sh <vm name> # images in /var/lib/libvirt/images/.image_store/ and in images/.temp - opensuse.sh - debian.sh - freebsd.sh - list-all-images.sh - compute/ - create.sh - # default vcpu ram disk storage pool network - # allow specification of storage pool and network. - # auto list all available images, storage pools, networks. on no argument. just call stroage/network list.sh - delete.sh and all associated virtual machines and block devices on all storage device - start.sh before starting the vm, make sure the cloud init seed.iso is not attached anymore. - shutdown.sh - list.sh # list by (sorted by) network group, ip address, then cpu ram disk, total disk(incl all attachedvols), name - network/ - attach.sh # one click, so if vm is running, ask fro permission to shutdown. or if can, live attach - list.sh - detach.sh - create.sh # allow specifying the ip address range. auto make uuid - delete.sh - disk/ - attach.sh - detach.sh - create.sh - list all available storage hardware devices on no argument. just call list.sh - make directory linking from /var/lib/libvirt/images/${VM_NAME}-vm-disks/ to the actual storage device - delete.sh - delete all blocks. all the linking if can. may not be able to link like that idk. list all mounts and delete the originla and the links - list.sh #list storage pools and include --all to view all blocks and their sizes - device/ - usb/ - attach.sh - detach.sh - view.sh # usbs of all the vms - -# No arguments shows available options -./vm compute create -Available networks: - prod-net 192.168.1.0/24 - dev-net 192.168.2.0/24 - test-net 192.168.3.0/24 - -Available storage pools: - prod-store 2TB free - dev-store 500GB free - backup 1TB free - -Available images: - fedora40(41) - - -Usage: ./vm compute create <name> <os> --vcpu 4 --ram 8 --disk 40 --storage-pool prod-store --network prod-net - -# Then use directly -./vm compute create myvm fedora40 prod-net prod-store - - - -os - debian functionally works, and issues are primarily quality of life issues - Debian primarily uses /etc/bash.bashrc, move the colour to this instead of etc bashrc on fedora - change the localhost to name of the vm in the config app - - - freebsd have not tried editing root password, boots on generic cloud image, no login yet - ubuntu no boot no login yet. - -others - ssh - public ssh port like online vps - this forwards 2222 on the host to port 22 - # 1. First, check your VM's IP address (from inside the VM) - ip addr show - # 2. Set up port forwarding in libvirt - virsh net-list # List networks - virsh net-edit default # Edit default network config - # Add this inside the <network> section: - <forward mode='nat'> - <nat> - <port start='2222' end='2222'> - <protocol>tcp</protocol> - <forward address='192.168.122.x' port='22'/> # Replace x with your VM's last octet - </port> - </nat> - </forward> - # 3. Restart the network - virsh net-destroy default - virsh net-start default - # 4. Configure firewall on Fedora host - sudo firewall-cmd --add-port=2222/tcp --permanent - sudo firewall-cmd --reload - seed.iso - move seed.iso to images/vmname/seed.iso - allows concurrent usage of vm.sh as there will be no conflicts diff --git a/others/vm3/meta-data b/others/vm3/meta-data deleted file mode 100644 index ce225e1..0000000 --- a/others/vm3/meta-data +++ /dev/null @@ -1,2 +0,0 @@ -instance-id: iid-fedora-vm -local-hostname: fedora-cloudimg diff --git a/others/vm3/user-data b/others/vm3/user-data deleted file mode 100644 index 58817da..0000000 --- a/others/vm3/user-data +++ /dev/null @@ -1,27 +0,0 @@ -#cloud-config -users: - - name: fedora - ssh-authorized-keys: - - sk-ssh-ed25519@openssh.com AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29tAAAAIGoBf5Lj8oDjXaXNrXtO8YVdOLgV2FN/bwiX93VxDmZ0AAAAEnNzaDptYWNtMS1yZXNpZGVudA== macm1-resident - - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHPOwh0+Bpkhg2dNKgUWsh0FKeCeI/awegY3sMb/ZAMb x@fedora - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - groups: wheel - shell: /bin/bash -runcmd: - - echo "#!/bin/bash" > /home/fedora/disk-setup.sh - - echo "DISK='/dev/vda'; MOUNT_PATH='/home/ceph5-vda-200G';" >> /home/fedora/disk-setup.sh - - echo '[ ! -d $MOUNT_PATH ] && sudo mkdir -p $MOUNT_PATH;' >> /home/fedora/disk-setup.sh - - echo 'sudo blkid | grep -q $DISK || sudo mkfs.ext4 $DISK;' >> /home/fedora/disk-setup.sh - - echo 'grep -q $DISK /etc/fstab || echo "$DISK $MOUNT_PATH ext4 defaults,nofail 0 0" | sudo tee -a /etc/fstab;' >> /home/fedora/disk-setup.sh - - echo "sudo mount -a;" >> /home/fedora/disk-setup.sh - - echo "sudo systemctl daemon-reload" >> /home/fedora/disk-setup.sh - - sudo chmod +x /home/fedora/disk-setup.sh - - sudo /home/fedora/disk-setup.sh - - sudo rm -f /home/fedora/disk-setup.sh - - sudo growpart /dev/sda 1 - - sudo xfs_growfs / - - sudo dnf install -y vim git - - cd /home/fedora - - git clone https://git.0nom.ch/setup - - sudo ./setup/setup.sh - - touch /home/fedora/runcmd_done diff --git a/others/vm3/vm.sh b/others/vm3/vm.sh deleted file mode 100755 index a043f89..0000000 --- a/others/vm3/vm.sh +++ /dev/null @@ -1,119 +0,0 @@ -#!/bin/bash -# Main entry point for VM management -# also negotiator for executing scripts, so for storage - -if [ "$(id -u)" != "0" ]; then - echo "This script must be run as root" - exit 1 -fi - -# Get script directory for relative paths -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" - -# First argument is the category (compute, network, storage, etc) -category=$1 -shift - -# Exit if no category specified -if [ -z "$category" ]; then - echo -e "Usage: ./vm <category> <action> [args...]\n" - echo -e "Categories: \ncompute\nnetwork\nstorage\ndevice\n" - echo "Run ./vm <category> for available subactions or tree for all available actions." - exit 1 -fi - -# Second argument is the action -action=$1 -shift - -# Handle each category -case $category in - compute) - case $action in - create) - $SCRIPT_DIR/compute/create.sh "$@" - ;; - start) - $SCRIPT_DIR/compute/start.sh "$@" - ;; - ls) - $SCRIPT_DIR/compute/ls.sh "$@" - ;; - shutdown) - $SCRIPT_DIR/compute/shutdown.sh "$@" - ;; - rm) - $SCRIPT_DIR/compute/rm.sh "$@" - ;; - *) - echo -e "Available compute actions: \ncreate\nstart\nls\nshutdown\ndelete" - exit 1 - ;; - esac - ;; - - network) - case $action in - attach) - $SCRIPT_DIR/network/attach.sh "$@" - ;; - detach) - $SCRIPT_DIR/network/detach.sh "$@" - ;; - list) - $SCRIPT_DIR/network/list.sh "$@" - ;; - create) - $SCRIPT_DIR/network/create.sh "$@" - ;; - delete) - $SCRIPT_DIR/network/delete.sh "$@" - ;; - *) - echo "Available network actions: \ncreate\nattach\ndetach\nlist\ndelete" - exit 1 - ;; - esac - ;; - - disk) - case $action in - attach) - $SCRIPT_DIR/disk/attach.sh "$@" - ;; - list) - $SCRIPT_DIR/disk/list.sh "$@" - ;; - *) - echo "Available disk actions: \ncreate\nattach\ndetach\nlist\ndelete" - exit 1 - ;; - esac - ;; - - storage-pool) - case $action in - create) # using a directory as a storage pool - $SCRIPT_DIR/storage-pool/create.sh "$@" - ;; - list) - $SCRIPT_DIR/storage-pool/list.sh "$@" - ;; - create-from-device) # initialise and use a devcie as storage pool - $SCRIPT_DIR/storage-pool/create-from-device.sh "$@" - ;; - - *) - echo "Available disk actions: \ncreate\nlist\ncreate-from-device\ndelete" - exit 1 - ;; - esac - ;; - - - *) - echo "Unknown category: $category" - echo "Available categories: compute, network, storage" - exit 1 - ;; -esac |
