-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvdioptimize
73 lines (64 loc) · 1.25 KB
/
vdioptimize
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
#!/bin/bash
echo ======== INITIALIZATION ========
if ! [ -f "$1" ]
then
echo This file doesn\'t exists
exit
fi
before=$(du -h "$1")
sudo modprobe nbd
echo
sleep 1
echo ======== MOUNTING VIRTUAL DISK ========
slot=0
error=0
while [ "$error" == "0" ]
do
slot=$(($slot+1))
if ! [ -b "/dev/nbd$slot" ]
then
echo No NBD slot available
exit
fi
lsblk /dev/nbd$slot > /dev/null
error=$?
done
echo Using /dev/nbd$slot
sudo qemu-nbd -c /dev/nbd$slot "$1"
echo
sleep 1
echo ======== CHECKING /dev/nbd$slot ========
echo \(This will fail if you have a partition table\)
sudo fsck /dev/nbd$slot
sudo e2fsck -yfv /dev/nbd$slot
echo
sleep 1
echo ======== ZEROING /dev/nbd$slot ========
echo \(This will fail if you have a partition table\)
sudo zerofree -v /dev/nbd$slot
echo
sleep 1
for part in /dev/nbd$slot\p*
do
echo ======== CHECKING $part ========
sudo fsck $part
sudo e2fsck -yfv $part
echo
sleep 1
echo ======== ZEROING $part ========
sudo zerofree -v $part
echo
sleep 1
done
echo ======== UNMOUNTING VIRTUAL DISK ========
sudo qemu-nbd -d /dev/nbd$slot
echo
sleep 1
echo ======== COMPACTING VIRTUAL DISK ========
vboxmanage modifymedium --compact "$1"
echo
sleep 1
echo ======== BEFORE-AFTER COMPARISON ========
after=$(du -h "$1")
echo $before
echo $after