summaryrefslogtreecommitdiff
path: root/others/vm3
diff options
context:
space:
mode:
authorYour Name <you@example.com>2024-12-21 15:11:19 +0800
committerYour Name <you@example.com>2024-12-21 15:11:19 +0800
commitbe3dde6ce14698c5818f21f65037a4beef1199cf (patch)
tree79dbd14ec4a07d6b41561931d15a4c03406d5836 /others/vm3
parent47303626554d42356dfd1b61ad3f9de97929cc87 (diff)
betavm3
Diffstat (limited to 'others/vm3')
-rwxr-xr-xothers/vm3/.config/init/install_packages.sh12
-rwxr-xr-xothers/vm3/.config/init/make_rootkey.sh4
-rwxr-xr-xothers/vm3/compute/create.sh60
-rw-r--r--others/vm3/docs2
-rwxr-xr-xothers/vm3/image/fedora.sh63
-rw-r--r--others/vm3/vm.sh42
6 files changed, 183 insertions, 0 deletions
diff --git a/others/vm3/.config/init/install_packages.sh b/others/vm3/.config/init/install_packages.sh
new file mode 100755
index 0000000..d5ca81f
--- /dev/null
+++ b/others/vm3/.config/init/install_packages.sh
@@ -0,0 +1,12 @@
+#!/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
new file mode 100755
index 0000000..5d07472
--- /dev/null
+++ b/others/vm3/.config/init/make_rootkey.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+sudo mkdir -p ~/keys
+ssh-keygen -t ed25519 -f ~/keys/k1 -N ""
diff --git a/others/vm3/compute/create.sh b/others/vm3/compute/create.sh
new file mode 100755
index 0000000..6d83a03
--- /dev/null
+++ b/others/vm3/compute/create.sh
@@ -0,0 +1,60 @@
+#!/bin/bash
+
+# takes in vm name, os type, vcpu, ram, disk as argument
+# takes in already generated seed iso and downloaded vm.iso file
+vmname=$1
+os=$2
+vcpu=$3
+ram_gb=$4
+disk_gb=$5
+
+#echo "$vmname $os $vcpu $ram_gb $disk_gb"
+
+
+if [ $# -ne 5 ]; then
+ echo "Usage: $0 <vm-name> <os> <vcpu> <ram(gb)> <disk(gb)>"
+ echo "seed.iso and image file have to be present!"
+ exit 1
+fi
+
+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="/var/lib/libvirt/images/${vmname}.qcow2"
+
+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 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
+
+sudo virt-install --name $vmname \
+ --vcpus $vcpu \
+ --memory "$((ram_gb * 1024))"\
+ --disk path=$new_vm_path,size=$disk_gb,format=qcow2 \
+ --disk path=$seed_iso,device=cdrom \
+ --os-type linux \
+ --os-variant $os \
+ --virt-type kvm \
+ --graphics none \
+ --network bridge=virbr0,model=virtio \
+ --print-xml > $xml || { echo "Failed to print XML."; exit 1; }
+
+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/docs b/others/vm3/docs
new file mode 100644
index 0000000..dc53806
--- /dev/null
+++ b/others/vm3/docs
@@ -0,0 +1,2 @@
+
+k1 will be created in /root/k1
diff --git a/others/vm3/image/fedora.sh b/others/vm3/image/fedora.sh
new file mode 100755
index 0000000..109f4fc
--- /dev/null
+++ b/others/vm3/image/fedora.sh
@@ -0,0 +1,63 @@
+#!/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/keys"
+
+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"
+
+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 /
+ - 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/vm.sh b/others/vm3/vm.sh
new file mode 100644
index 0000000..1072569
--- /dev/null
+++ b/others/vm3/vm.sh
@@ -0,0 +1,42 @@
+#!/bin/bash
+
+
+case "$1" in
+ "compute")
+ shift
+ case "$1" in
+ "create")
+ VM_NAME=$2
+ shift 2
+ while [[ $# -gt 0 ]]; do
+ case "$1" in
+ -vcpu)
+ VCPU=$2
+ shift 2
+ ;;
+ -ram)
+ RAM_GB=$(($2*1000))
+ shift 2
+ ;;
+ -disk)
+ DISK_GB=$2
+ shift 2
+ ;;
+ *)
+ echo "Unknown argument: $1"
+ exit 1
+ ;;
+ esac
+ done
+ ;;
+ *)
+ echo "Unknown compute command: $1"
+ exit 1
+ ;;
+ esac
+ ;;
+ *)
+ echo "Unknown command: $1"
+ exit 1
+ ;;
+esac