-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
sparsify.sh
executable file
·40 lines (37 loc) · 1.13 KB
/
sparsify.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
#!/bin/bash
#This script sparses an attached disk
#The guest must be shutdown
#Great gain on disk space !
name=$1
check_guest_name () {
if [ -z "${name}" ]; then
echo "This script sparses an attached disk"
echo "Please provide a the guest name of a destroyed guest: exit"
echo "Usage : $0 <guest name>"
exit
fi
guests_defined="$(virsh list --all --name)"
if grep -qvw "$name" <<< ${guests_defined} ; then
echo "Please provide a defined guest name : exit"
echo "Guests available : "
echo "$(virsh list --all --name)"
echo "Usage : $0 <guest name>"
exit
fi
}
sparsify ()
{
echo "stop guest OS"
virsh destroy $name 2> /dev/null
echo "Sparse disk optimization"
# make a virtual machine disk sparse
virt-sparsify --check-tmpdir ignore --compress --convert qcow2 --format qcow2 /var/lib/libvirt/images/$name.qcow2 /var/lib/libvirt/images/$name-sparsified.qcow2
# remove original image
rm -rf /var/lib/libvirt/images/$name.qcow2
# rename sparsified
mv /var/lib/libvirt/images/$name-sparsified.qcow2 /var/lib/libvirt/images/$name.qcow2
# set correct ownership for the VM image file
#chown qemu:qemu /var/lib/libvirt/images/$name.qcow2
}
check_guest_name
sparsify