summaryrefslogtreecommitdiff
path: root/others
diff options
context:
space:
mode:
Diffstat (limited to 'others')
-rwxr-xr-xothers/vm3/.config/cloud-init-files/fedora40.sh (renamed from others/vm3/image/fedora.sh)0
-rw-r--r--others/vm3/.docs.swpbin0 -> 12288 bytes
-rwxr-xr-xothers/vm3/compute/create.sh68
-rwxr-xr-xothers/vm3/compute/delete.sh27
-rwxr-xr-xothers/vm3/compute/list.sh45
-rwxr-xr-xothers/vm3/compute/shutdown.sh6
-rwxr-xr-x[-rw-r--r--]others/vm3/compute/start.sh18
-rw-r--r--others/vm3/docs32
-rwxr-xr-xothers/vm3/test.sh49
-rwxr-xr-x[-rw-r--r--]others/vm3/vm.sh148
10 files changed, 335 insertions, 58 deletions
diff --git a/others/vm3/image/fedora.sh b/others/vm3/.config/cloud-init-files/fedora40.sh
index cd05e10..cd05e10 100755
--- a/others/vm3/image/fedora.sh
+++ b/others/vm3/.config/cloud-init-files/fedora40.sh
diff --git a/others/vm3/.docs.swp b/others/vm3/.docs.swp
new file mode 100644
index 0000000..849e1f3
--- /dev/null
+++ b/others/vm3/.docs.swp
Binary files differ
diff --git a/others/vm3/compute/create.sh b/others/vm3/compute/create.sh
index 649cd86..0bb2e63 100755
--- a/others/vm3/compute/create.sh
+++ b/others/vm3/compute/create.sh
@@ -2,20 +2,52 @@
2 2
3# takes in vm name, os type, vcpu, ram, disk as argument 3# takes in vm name, os type, vcpu, ram, disk as argument
4# takes in already generated seed iso and downloaded vm.iso file 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 5
11#echo "$vmname $os $vcpu $ram_gb $disk_gb" 6# default values
7vcpu=8
8ram_gb=4
9disk_gb=32
10os="fedora40.qcow2"
12 11
12# parse arguments
13while [[ $# -gt 0 ]]; do
14 case $1 in
15 --vcpu)
16 vcpu="$2"
17 shift 2
18 ;;
19 --ram)
20 ram_gb="$2"
21 shift 2
22 ;;
23 --disk-size)
24 disk_gb="$2"
25 shift 2
26 ;;
27 --image)
28 os="$2"
29 shift 2
30 ;;
31 *)
32 # Handle positional arguments (vmname and os)
33 if [ -z "$vmname" ]; then
34 vmname="$1"
35 else
36 echo "Unknown argument: $1"
37 exit 1
38 fi
39 shift
40 ;;
41 esac
42done
13 43
14if [ $# -ne 5 ]; then 44# Check mandatory arguments, basicaly checking for initial 1 argument. if it dont exist, vmname will be null
15 echo "Usage: $0 <vm-name> <os> <vcpu> <ram(gb)> <disk(gb)>" 45# image is os!!
16 echo "seed.iso and image file have to be present!" 46if [ -z "$vmname" ] ; then
47 echo "Usage: $0 <vm-name> [--image <os>.qcow2] [--vcpu N] [--ram N] [--disk-size N]"
48 echo "seed.iso and image file have to be present! Default os is fedora"
17 echo "" 49 echo ""
18 echo "Available images" 50 echo "Available images:"
19 sudo ls /var/lib/libvirt/images/.image_store 51 sudo ls /var/lib/libvirt/images/.image_store
20 exit 1 52 exit 1
21fi 53fi
@@ -26,14 +58,20 @@ seed_iso="${workingdir}/seed.iso"
26xml="${workingdir}/xml" 58xml="${workingdir}/xml"
27 59
28image_dir_path="/var/lib/libvirt/images/.image_store" 60image_dir_path="/var/lib/libvirt/images/.image_store"
29src_file="${image_dir_path}/${os}.qcow2" 61src_file="${image_dir_path}/${os}"
30new_vm="/var/lib/libvirt/images/${vmname}.qcow2" 62new_vm_dir="/var/lib/libvirt/images/${vmname}"
63new_vm="${new_vm_dir}/${vmname}.qcow2"
31 64
32if [ ! -f "${src_file}" ]; then 65if [ ! -f "${src_file}" ]; then
33 echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}" 66 echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}"
34 exit 1 67 exit 1
35fi 68fi
36 69
70# run the script to make the cloud init files
71sudo bash "${scriptdir}/../.config/cloud-init-files/$(basename "$os" .qcow2).sh" "${vmname}"
72
73sudo mkdir -p $new_vm_dir
74
37sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; } 75sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; }
38 76
39if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then 77if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then
@@ -47,11 +85,11 @@ sudo virt-install --name $vmname \
47 --disk path=$new_vm,format=qcow2 \ 85 --disk path=$new_vm,format=qcow2 \
48 --disk path=$seed_iso,device=cdrom \ 86 --disk path=$seed_iso,device=cdrom \
49 --os-type linux \ 87 --os-type linux \
50 --os-variant $os \ 88 --os-variant $(basename "$os" .qcow2) \
51 --virt-type kvm \ 89 --virt-type kvm \
52 --graphics none \ 90 --graphics none \
53 --network bridge=virbr0,model=virtio \ 91 --network bridge=virbr0,model=virtio \
54 --print-xml > $xml #|| { echo "Failed to print XML."; exit 1; } 92 --print-xml > $xml || { sudo rm -rf $new_vm_dir; exit 1; }
55 93
56sudo virsh define $xml #&> /dev/null || { echo "Failed to define the new VM."; exit 1; } 94sudo virsh define $xml #&> /dev/null || { echo "Failed to define the new VM."; exit 1; }
57 95
@@ -59,5 +97,5 @@ sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null
59 97
60sudo virsh start $vmname 98sudo virsh start $vmname
61 99
62#sudo rm "${workingdir}"/* 100sudo rm "${workingdir}"/*
63 101
diff --git a/others/vm3/compute/delete.sh b/others/vm3/compute/delete.sh
new file mode 100755
index 0000000..dd65379
--- /dev/null
+++ b/others/vm3/compute/delete.sh
@@ -0,0 +1,27 @@
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/list.sh b/others/vm3/compute/list.sh
new file mode 100755
index 0000000..b62ef14
--- /dev/null
+++ b/others/vm3/compute/list.sh
@@ -0,0 +1,45 @@
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}/${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/shutdown.sh b/others/vm3/compute/shutdown.sh
new file mode 100755
index 0000000..081499b
--- /dev/null
+++ b/others/vm3/compute/shutdown.sh
@@ -0,0 +1,6 @@
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
index 5a2b3aa..d64dbb1 100644..100755
--- a/others/vm3/compute/start.sh
+++ b/others/vm3/compute/start.sh
@@ -1,4 +1,20 @@
1#!/bin/bash 1#!/bin/bash
2 2
3# starts vm 3# starts vm
4# need to remove seed.iso first if vm have the seed iso 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/docs b/others/vm3/docs
index 8423f1c..c274dd3 100644
--- a/others/vm3/docs
+++ b/others/vm3/docs
@@ -10,9 +10,9 @@ vm storage create <name2> /dir/
10 10
11 11
12vm/ 12vm/
13 ./vm # Single entry point. one vm at a time(except for compute delete-all) 13 ./vm.sh # Single entry point. one vm at a time(except for compute delete-all)
14 .config/ # all config files 14 .config/ # all config files
15 init/ 15 init/ # run this everytime. try to think of a way to set a flag that says this device has been initialised
16 install_packages.sh 16 install_packages.sh
17 make_rootkey.sh # in /root/k/k1. can also add keys in here 17 make_rootkey.sh # in /root/k/k1. can also add keys in here
18 /var/lib/libvirt/images/.image_store/ # stores the images 18 /var/lib/libvirt/images/.image_store/ # stores the images
@@ -22,23 +22,23 @@ vm/
22 opensuse.sh 22 opensuse.sh
23 debian.sh 23 debian.sh
24 freebsd.sh 24 freebsd.sh
25 list-all-images.sh
25 compute/ 26 compute/
26 create.sh 27 create.sh
27 # default vcpu ram disk storage pool network 28 # default vcpu ram disk storage pool network
28 # allow specification of storage pool and network. 29 # allow specification of storage pool and network.
29 # auto list all available images, storage pools, networks. on no argument. just call stroage/network list.sh 30 # auto list all available images, storage pools, networks. on no argument. just call stroage/network list.sh
30 delete.sh and all associated virtual machines and block devices on all storage device 31 delete.sh and all associated virtual machines and block devices on all storage device
31 delete-all.sh
32 start.sh before starting the vm, make sure the cloud init seed.iso is not attached anymore. 32 start.sh before starting the vm, make sure the cloud init seed.iso is not attached anymore.
33 shutdown.sh 33 shutdown.sh
34 list.sh # list by ip address, (sorted by) network group, then name, then cpu ram disk, total disk(incl all attachedvols) 34 list.sh # list by (sorted by) network group, ip address, then cpu ram disk, total disk(incl all attachedvols), name
35 network/ 35 network/
36 attach.sh # one click, so if vm is running, ask fro permission to shutdown. or if can, live attach 36 attach.sh # one click, so if vm is running, ask fro permission to shutdown. or if can, live attach
37 list.sh 37 list.sh
38 detach.sh 38 detach.sh
39 create.sh # allow specifying the ip address range. auto make uuid 39 create.sh # allow specifying the ip address range. auto make uuid
40 delete.sh 40 delete.sh
41 storage/ 41 disk/
42 attach.sh 42 attach.sh
43 detach.sh 43 detach.sh
44 create.sh 44 create.sh
@@ -52,4 +52,24 @@ vm/
52 attach.sh 52 attach.sh
53 detach.sh 53 detach.sh
54 view.sh # usbs of all the vms 54 view.sh # usbs of all the vms
55 55
56# No arguments shows available options
57./vm compute create
58Available networks:
59 prod-net 192.168.1.0/24
60 dev-net 192.168.2.0/24
61 test-net 192.168.3.0/24
62
63Available storage pools:
64 prod-store 2TB free
65 dev-store 500GB free
66 backup 1TB free
67
68Available images:
69 fedora40(41)
70
71
72Usage: ./vm compute create <name> <os> --vcpu 4 --ram 8 --disk 40 --storage-pool prod-store --network prod-net
73
74# Then use directly
75./vm compute create myvm fedora 2 4 20 prod-net prod-store
diff --git a/others/vm3/test.sh b/others/vm3/test.sh
new file mode 100755
index 0000000..0dafc23
--- /dev/null
+++ b/others/vm3/test.sh
@@ -0,0 +1,49 @@
1#!/bin/bash
2
3# Default values
4vcpu=2
5ram_gb=4
6disk_gb=20
7
8# Parse named arguments
9while [[ $# -gt 0 ]]; do
10 case $1 in
11 --vcpu)
12 vcpu="$2"
13 shift 2
14 ;;
15 --ram)
16 ram_gb="$2"
17 shift 2
18 ;;
19 --disk-size)
20 disk_gb="$2"
21 shift 2
22 ;;
23 *)
24 # Handle positional arguments (vmname and os)
25 if [ -z "$vmname" ]; then
26 vmname="$1"
27 elif [ -z "$os" ]; then
28 os="$1"
29 else
30 echo "Unknown argument: $1"
31 exit 1
32 fi
33 shift
34 ;;
35 esac
36done
37
38# Check mandatory arguments
39if [ -z "$vmname" ] || [ -z "$os" ]; then
40 echo "Usage: $0 <vm-name> <os> [--vcpu N] [--ram N] [--disk-size N]"
41 exit 1
42fi
43
44# Now you can use the variables
45echo "VM Name: $vmname"
46echo "OS: $os"
47echo "VCPU: $vcpu"
48echo "RAM: $ram_gb GB"
49echo "Disk: $disk_gb GB"
diff --git a/others/vm3/vm.sh b/others/vm3/vm.sh
index 1072569..30168ba 100644..100755
--- a/others/vm3/vm.sh
+++ b/others/vm3/vm.sh
@@ -1,42 +1,118 @@
1#!/bin/bash 1#!/bin/bash
2# Main entry point for VM management
2 3
4if [ "$(id -u)" != "0" ]; then
5 echo "This script must be run as root"
6 exit 1
7fi
3 8
4case "$1" in 9# Get script directory for relative paths
5 "compute") 10SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6 shift 11
7 case "$1" in 12# First argument is the category (compute, network, storage, etc)
8 "create") 13category=$1
9 VM_NAME=$2 14shift
10 shift 2 15
11 while [[ $# -gt 0 ]]; do 16# Exit if no category specified
12 case "$1" in 17if [ -z "$category" ]; then
13 -vcpu) 18 echo -e "Usage: ./vm <category> <action> [args...]\n"
14 VCPU=$2 19 echo -e "Categories: \ncompute\nnetwork\nstorage\ndevice\n"
15 shift 2 20 echo "Run ./vm <category> for available subactions or tree for all available actions."
16 ;; 21 exit 1
17 -ram) 22fi
18 RAM_GB=$(($2*1000)) 23
19 shift 2 24# Second argument is the action
20 ;; 25action=$1
21 -disk) 26shift
22 DISK_GB=$2 27
23 shift 2 28# Handle each category
24 ;; 29case $category in
25 *) 30 compute)
26 echo "Unknown argument: $1" 31 case $action in
27 exit 1 32 create)
28 ;; 33 $SCRIPT_DIR/compute/create.sh "$@"
29 esac 34 ;;
30 done 35 start)
31 ;; 36 $SCRIPT_DIR/compute/start.sh "$@"
32 *) 37 ;;
33 echo "Unknown compute command: $1" 38 list)
34 exit 1 39 $SCRIPT_DIR/compute/list.sh "$@"
35 ;; 40 ;;
36 esac 41 shutdown)
37 ;; 42 $SCRIPT_DIR/compute/shutdown.sh "$@"
38 *) 43 ;;
39 echo "Unknown command: $1" 44 delete)
45 $SCRIPT_DIR/compute/delete.sh "$@"
46 ;;
47 *)
48 echo -e "Available compute actions: \ncreate\nstart\nlist\nshutdown\ndelete"
49 exit 1
50 ;;
51 esac
52 ;;
53
54 network)
55 case $action in
56 attach)
57 $SCRIPT_DIR/network/attach.sh "$@"
58 ;;
59 detach)
60 $SCRIPT_DIR/network/detach.sh "$@"
61 ;;
62 list)
63 $SCRIPT_DIR/network/list.sh "$@"
64 ;;
65 create)
66 $SCRIPT_DIR/network/create.sh "$@"
67 ;;
68 delete)
69 $SCRIPT_DIR/network/delete.sh "$@"
70 ;;
71 *)
72 echo "Available network actions: \ncreate\nattach\ndetach\nlist\ndelete"
73 exit 1
74 ;;
75 esac
76 ;;
77
78 disk)
79 case $action in
80 attach)
81 $SCRIPT_DIR/disk/attach.sh "$@"
82 ;;
83 list)
84 $SCRIPT_DIR/disk/list.sh "$@"
85 ;;
86 *)
87 echo "Available disk actions: \ncreate\nattach\ndetach\nlist\ndelete"
40 exit 1 88 exit 1
41 ;; 89 ;;
90 esac
91 ;;
92
93 storage-pool)
94 case $action in
95 create) # using a directory as a storage pool
96 $SCRIPT_DIR/storage-pool/create.sh "$@"
97 ;;
98 list)
99 $SCRIPT_DIR/storage-pool/list.sh "$@"
100 ;;
101 create-from-device) # initialise and use a devcie as storage pool
102 $SCRIPT_DIR/storage-pool/create-from-device.sh "$@"
103 ;;
104
105 *)
106 echo "Available disk actions: \ncreate\nlist\ncreate-from-device\ndelete"
107 exit 1
108 ;;
109 esac
110 ;;
111
112
113 *)
114 echo "Unknown category: $category"
115 echo "Available categories: compute, network, storage"
116 exit 1
117 ;;
42esac 118esac