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