-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbashvm-change-resources.sh
131 lines (120 loc) · 4.9 KB
/
bashvm-change-resources.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
# Manage Resources of a VM
while true; do
echo -e "\n=================================== Manage Resources ==================================="
echo "s. Show resources of a VM 1. Add disk space to a VM 2. Shrink disk space of a VM"
echo "3. Change the number of vcpu in a VM 4. Change the memory of a VM 5. Attach a disk to a VM"
echo "6. Detach a disk from a VM 7. Attach Network to a VM 8. Detach Network from a VM"
echo "q. Back to main menu"
echo ""
read -ep "Enter your choice: " manage_choice
case $manage_choice in
s)
# Show resources of a VM
read -ep "Enter the VM name: " vm_name
virsh dominfo "$vm_name"
virsh domfsinfo "$vm_name"
virsh domblkinfo "$vm_name" --all --human
;;
1)
# Add disk space to a VM
read -ep "Enter the name of the virtual machine: " vm_name
read -ep "Enter the new disk size (e.g., 40GB): " disk_size
read -ep "Enter the pool name [default]: " pool_name
if [ -z "$pool_name" ]; then
pool_name="default"
fi
virsh vol-resize "$vm_name".qcow2 "$disk_size" --pool "$pool_name"
;;
2)
# Shrink disk space of a VM
read -ep "Enter the name of the virtual machine: " vm_name
read -ep "Enter the new disk size (e.g., 40GB): " disk_size
read -ep "Enter the pool name [default]: " pool_name
if [ -z "$pool_name" ]; then
pool_name="default"
fi
virsh vol-resize "$vm_name".qcow2 "$disk_size" --pool "$pool_name" --shrink
;;
3)
# Change vcpus
read -ep "Enter the name of the virtual machine: " vm_name
read -ep "Enter the new vcpu number (e.g., 4): " vcpu_num
virsh setvcpus --domain "$vm_name" --count "$vcpu_num" --config --maximum
if [ ! $? == 0 ]; then
echo "Failed to set new vcpu maximum"
break
fi
virsh setvcpus --domain "$vm_name" --count "$vcpu_num" --config
if [ ! $? == 0 ]; then
echo "Failed to set new vcpu count"
break
fi
echo "New vcpu count has been set successfully"
;;
4)
# Change memory
read -ep "Enter the name of the virtual machine: " vm_name
read -ep "Enter the new memory size (e.g., 1GB): " mem_num
virsh setmaxmem --domain "$vm_name" --size "$mem_num" --current
if [ ! $? == 0 ]; then
echo "Failed to set new memory maximum"
break
fi
virsh setmem --domain "$vm_name" --size "$mem_num" --current
if [ ! $? == 0 ]; then
echo "Failed to set new memory size"
break
fi
echo "New memory size has been set successfully"
;;
5)
# Attach a disk to a VM
read -ep "Enter the name of the virtual machine: " vm_name
read -ep "Enter the new target device: (e.g., vdb): " vm_target
read -ep "Enter the qcow2 virtual disk name: (e.g., /var/lib/libvirt/images/vm1.qcow2): " vm_qcow2
virsh attach-disk "$vm_name" "$vm_qcow2" "$vm_target" --current --subdriver qcow2
;;
6)
# Detach a disk from a VM
read -ep "Enter the name of the virtual machine: " vm_name
read -ep "Enter the qcow2 virtual disk name: (e.g., /var/lib/libvirt/images/vm1.qcow2): " vm_qcow2
virsh detach-disk "$vm_name" "$vm_qcow2" --current
;;
7)
# Attach network to a VM
read -ep "Enter the name of the virtual machine: " vm_name
read -ep "Enter the name of the network to attach: " network_name
NETWORK_XML="/tmp/${network_name}-network.xml"
cat > "$NETWORK_XML" <<EOF
<interface type='network'>
<source network='${network_name}'/>
<model type='e1000'/>
</interface>
EOF
virsh attach-device "$vm_name" "$NETWORK_XML" --config
rm -f "$NETWORK_XML"
;;
8)
# Detach network from a VM
read -ep "Enter the name of the virtual machine: " vm_name
read -ep "Enter the name of the network to detach: " network_name
NETWORK_XML="/tmp/${network_name}-network.xml"
cat > "$NETWORK_XML" <<EOF
<interface type='network'>
<source network='${network_name}'/>
<model type='e1000'/>
</interface>
EOF
virsh detach-device "$vm_name" "$NETWORK_XML" --config
rm -f "$NETWORK_XML"
;;
q)
# Back to main menu
break
;;
*)
echo "Invalid choice. Please enter a valid option."
;;
esac
done