-
Notifications
You must be signed in to change notification settings - Fork 3
/
expand.sh
executable file
·72 lines (60 loc) · 2.01 KB
/
expand.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
#!/bin/bash
# Check if kpartx is installed otherwise install kpartx
if ! [ -x "$(command -v kpartx)" ]; then
echo 'Installing kpartx dependency...'
apt-get install -y kpartx
fi
# List of all the vm
qm list
while true; do
# Promt the user to select the vm id to be expanded
read -p "Enter the VM ID to be expanded: " VM_ID
# Check if the VM id is valid
if [ -z "$(qm list | grep $VM_ID)" ]; then
printf "\nInvalid VM ID\n"
else
break
fi
done
# funtion to check if the vm is stopped
function check_vm_stopped() {
if [ "$(qm status $VM_ID | awk '{print $2}')" != "stopped" ]; then
return 0
else
return 1
fi
}
# Check if the vm is stopped until it is stopped
while check_vm_stopped; do
read -p "The VM $VM_ID must be stopped to expand the strage. Shutdown the VM and press Enter to continue"
done
while true; do
# promt the user to enter the size to be expanded
read -p "Enter the size to be expanded in GB (example: 10G): " EXPAND_BY_GB
# Check if the size is valid
if [ -z "$(printf $EXPAND_BY_GB | grep -E '^[0-9]+[G]$')" ]; then
printf "\nInvalid size. Please enter the size in GB (example: 10G)\n"
else
break
fi
done
# Set needed variables for expansion
DISK_NAME=$(qm config $VM_ID | grep scsi0: | awk '{split($2,a,":|,");print a[1]}')
VIRTUAL_DISK_NAME=$(qm config $VM_ID | grep scsi0: | awk '{split($2,a,":|,");print a[2]}')
VIRTUAL_DISK_PATH="/dev/${DISK_NAME}/${VIRTUAL_DISK_NAME}"
# Display warning
echo VM ID ${VM_ID} disk ${DISK_NAME} will be expanded by ${EXPAND_BY_GB}
echo -e "\e[31mWarning: There is currently no way to downsize the disk! \e[0m"
# Ask for confirmation "yes" and git enter to continue
read -p "Are you sure you want to expand the disk? (yes/no): " CONFIRM
# if the user says yes, then continue otherwise exit
if [ "$CONFIRM" = "yes" ]; then
printf "\nExpanding the disk..."
qm resize $VM_ID scsi0 +${EXPAND_BY_GB}
kpartx -av ${VIRTUAL_DISK_PATH}
sgdisk ${VIRTUAL_DISK_PATH} -e
kpartx -d ${VIRTUAL_DISK_PATH}
else
printf "\nExiting"
exit 1
fi