-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextend-disk.sh
executable file
·145 lines (127 loc) · 4.16 KB
/
extend-disk.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
#
# This extends the VM's disk by the given size
#
# It works by adding an additional qcow2 image file
# as a nev disk to the machine and using it to
# extend the machine's LVM. The disks can be added
# until the letter z (disk vdz) is reached ;)
#
# gnd @ gnd.sk, 2017 - 2019
#
#######################################################################
usage() {
printf "\n"
printf "This command adds a new disk to the virtual machine. \n"
printf "The disk is then added to the LVM, thus extending the existing disk capacity. \n"
printf "The size parameter should be the number of GB you need adding to the VM. \n"
printf "\n"
printf "Usage: \n"
printf "$0 <name> <size> (in GB) \n"
printf "\n"
}
# Check if LIMA_ROOT set
if [ -z $LIMA_ROOT ]; then
echo "Cant find LIMA. Please check if the install finished correctly."
echo "Exiting. Reason: LIMA_ROOT not set."
exit
fi
# Define globals
source $LIMA_ROOT/vms/settings
# Check if parameter given
if [[ -z $1 ]]; then
usage
exit
fi
if [[ -z $2 ]]; then
usage
exit
fi
# Check for the machine
VM_NAME=$1
LINS=`cat $VM_LIST | awk {'print $2;'}|grep "^$VM_NAME$"|wc -l`
if [[ $LINS -lt 1 ]]; then
echo "No such name $VM_NAME found"
exit
fi
if [[ $LINS -gt 1 ]]; then
echo "More names found, please be specific:"
cat $VM_LIST | awk {'print $2;'}|grep "^$VM_NAME$"
exit
fi
echo "Machine $VM_NAME found"
VM_IP=`cat $VM_LIST | awk {'print $2" "$3;'}|grep "^$VM_NAME "|awk {'print $2;'}`
VM_TYPE=`cat $VM_LIST | awk {'print $2" "$5;'}|grep "^$VM_NAME "|awk {'print $2;'}`
if [[ $VM_TYPE == "sta" ]]; then
VM_TYPE="static"
fi
if [[ $VM_TYPE == "dyn" ]]; then
VM_TYPE="dynamic"
fi
VM_XML=$VM_DIR"/"$VM_TYPE"/"$VM_NAME"/vm.xml"
VM_DIR=$VM_DIR"/"$VM_TYPE"/"$VM_NAME
# Get current drive letter
echo "Determining the current last drive letter"
CURR=`cat $VM_XML |grep virtio|grep target|head -1|sed 's/.*<target dev="vd//g'`
CURR=${CURR:0:1}
echo "Last drive letter is $CURR"
# Determine next letter
echo "Setting the next drive letter"
if [[ ! $CURR == "z" ]]; then
NXT=`for k in {a..z}; do echo -n $k; done | sed "s/.*$CURR//g"` # LOL !
NXT=${NXT:0:1}
echo "Next drive letter is $NXT"
else
echo "Last letter reached. Please contact the admin"
exit
fi
# Create the disk file
SIZE=$2
echo "Creating the new disk image: "$VM_DIR"/disk-"$NXT".img"
/usr/bin/qemu-img create -f qcow2 $VM_DIR"/disk-"$NXT".img" $SIZE"G"
# Add the disk to the VM XML spec. sth like:
RND=`openssl rand -hex 2`
TMPFILE="/tmp/dsk_"$RND
touch $TMPFILE
chmod 600 $TMPFILE
echo '<disk type="file" device="disk">' > $TMPFILE
echo '<driver name="qemu" type="qcow2"/>' >> $TMPFILE
echo '<source file="'$VM_DIR'/disk-'$NXT'.img"/>' >> $TMPFILE
echo '<target dev="vd'$NXT'" bus="virtio"/>' >> $TMPFILE
echo '</disk>' >> $TMPFILE
echo "Modifying the VM definition file"
# Using ; instead of / as a delimiter in sed replace
sed -i '0,/<disk type="file" device="disk">/s;<disk type="file" device="disk">;<disk type="file" device="disk">\n <driver name="qemu" type="qcow2"/>\n <source file="'$VM_DIR'/disk-'$NXT'.img"/>\n <target dev="vd'$NXT'" bus="virtio"/>\n </disk>\n <disk type="file" device="disk">;g' $VM_XML
# Adding the new disk to the machine
virsh attach-device $VM_NAME $TMPFILE
rm $TMPFILE
# The fdisk batch part
RND=`openssl rand -hex 2`
TMPFILE="/tmp/fd_"$RND
echo "#!/bin/bash" > $TMPFILE
echo -n 'echo "n
p
1
w
"' >> $TMPFILE
echo '|fdisk /dev/vd'$NXT >> $TMPFILE
# Transfer the fdisk script to the machine
scp $TMPFILE $VM_IP:/root/nxtd
rm $TMPFILE
# Run the script on the machine
echo "Creating a new partition"
ssh $VM_IP "chmod 700 /root/nxtd"
ssh $VM_IP "/root/nxtd"
ssh $VM_IP "rm /root/nxtd"
# The LVM batch part
# get the name of the VG
echo "Getting VG name for $VM_NAME"
VG_NAME=`ssh $VM_IP vgs -o vg_name --noheadings|sed 's/ //g'`
echo "Default VG name for $VM_NAME is $VG_NAME"
echo "Extending the LVM"
ssh $VM_IP "pvcreate /dev/vd"$NXT"1"
ssh $VM_IP "vgextend $VG_NAME /dev/vd"$NXT"1"
ssh $VM_IP "pvscan"
ssh $VM_IP "lvextend /dev/$VG_NAME/root /dev/vd"$NXT"1"
ssh $VM_IP "resize2fs /dev/$VG_NAME/root"
#ssh $VM_IP "echo \"/dev/vd"$NXT"1\" >> /root/scripts/disks"