summaryrefslogtreecommitdiff
path: root/others/vm3
diff options
context:
space:
mode:
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 @@
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
new file mode 100755
index 0000000..5d07472
--- /dev/null
+++ b/others/vm3/.config/init/make_rootkey.sh
@@ -0,0 +1,4 @@
1#!/bin/bash
2
3sudo mkdir -p ~/keys
4ssh-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 @@
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
5vmname=$1
6os=$2
7vcpu=$3
8ram_gb=$4
9disk_gb=$5
10
11#echo "$vmname $os $vcpu $ram_gb $disk_gb"
12
13
14if [ $# -ne 5 ]; then
15 echo "Usage: $0 <vm-name> <os> <vcpu> <ram(gb)> <disk(gb)>"
16 echo "seed.iso and image file have to be present!"
17 exit 1
18fi
19
20scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir
21workingdir="/var/lib/libvirt/images/.temp"
22seed_iso="${workingdir}/seed.iso"
23xml="${workingdir}/xml"
24
25image_dir_path="/var/lib/libvirt/images/.image_store"
26src_file="${image_dir_path}/${os}.qcow2"
27new_vm="/var/lib/libvirt/images/${vmname}.qcow2"
28
29if [ ! -f "${src_file}" ]; then
30 echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}"
31 exit 1
32fi
33
34sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; }
35
36if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then
37 echo -e "\n$vmname already exist. Delete it before using the same name."
38 exit 1
39fi
40
41sudo virt-install --name $vmname \
42 --vcpus $vcpu \
43 --memory "$((ram_gb * 1024))"\
44 --disk path=$new_vm_path,size=$disk_gb,format=qcow2 \
45 --disk path=$seed_iso,device=cdrom \
46 --os-type linux \
47 --os-variant $os \
48 --virt-type kvm \
49 --graphics none \
50 --network bridge=virbr0,model=virtio \
51 --print-xml > $xml || { echo "Failed to print XML."; exit 1; }
52
53sudo virsh define $xml &> /dev/null || { echo "Failed to define the new VM."; exit 1; }
54
55sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null
56
57sudo virsh start $vmname
58
59sudo rm "${workingdir}"/*
60
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 @@
1
2k1 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 @@
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/keys"
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
21if [ $# -ne 1 ]; then
22 echo "Usage: $0 <vm-name>"
23 exit 1
24fi
25
26sudo mkdir -p "$dir_path"
27sudo mkdir -p "$config_dir"
28
29[ ! -f "$src_file" ] && echo "source image does not exist! downloading..." && sudo wget -O "$src_file" "$image_url"
30
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/bash
46runcmd:
47 - sudo growpart /dev/sda 1
48 - sudo xfs_growfs /
49 - sudo dnf install -y vim git
50 - cd /home/user
51 - git clone https://git.0nom.ch/setup
52 - sudo ./setup/setup.sh
53 - touch /home/user/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; }
62
63echo "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 @@
1#!/bin/bash
2
3
4case "$1" in
5 "compute")
6 shift
7 case "$1" in
8 "create")
9 VM_NAME=$2
10 shift 2
11 while [[ $# -gt 0 ]]; do
12 case "$1" in
13 -vcpu)
14 VCPU=$2
15 shift 2
16 ;;
17 -ram)
18 RAM_GB=$(($2*1000))
19 shift 2
20 ;;
21 -disk)
22 DISK_GB=$2
23 shift 2
24 ;;
25 *)
26 echo "Unknown argument: $1"
27 exit 1
28 ;;
29 esac
30 done
31 ;;
32 *)
33 echo "Unknown compute command: $1"
34 exit 1
35 ;;
36 esac
37 ;;
38 *)
39 echo "Unknown command: $1"
40 exit 1
41 ;;
42esac