tests: Document new tests/ subdirectory.
[libguestfs.git] / caution / qemu-snapshot-isolation.sh
1 #!/bin/bash -
2 # libguestfs
3 # Copyright (C) 2011 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 # Test that qemu snapshots are really isolating writes from the
20 # underlying disk image.  If this test were to fail, you could expect
21 # libguestfs to cause massive disk corruption on live guests.
22
23 set -e
24
25 rm -f test1.img test2.img test3.img
26
27 truncate -s 100M test1.img
28 test1_md5sum="$(md5sum test1.img | awk '{print $1}')"
29 truncate -s 100M test2.img
30 test2_md5sum="$(md5sum test2.img | awk '{print $1}')"
31 qemu-img create -f qcow2 test3.img 100M
32 test3_md5sum="$(md5sum test3.img | awk '{print $1}')"
33
34 # The vitally important calls are 'add-drive-ro' and
35 # 'add-drive-opts ... readonly:true'.
36 ../fish/guestfish <<'EOF'
37 add-drive-ro test1.img
38 add-drive-opts test2.img format:raw readonly:true
39 add-drive-opts test3.img format:qcow2 readonly:true
40 run
41
42 part-disk /dev/sda mbr
43 part-disk /dev/sdb mbr
44 part-disk /dev/sdc mbr
45
46 mkfs ext2 /dev/sda1
47 copy-size /dev/sda1 /dev/sdb1 5M
48 pvcreate /dev/sdc1
49 vgcreate VG /dev/sdc1
50 lvcreate LV VG 80
51 mkfs ext3 /dev/VG/LV
52
53 mkmountpoint /a
54 mount-options "" /dev/sda1 /a
55 mkmountpoint /b
56 mount-options "" /dev/sdb1 /b
57 mkmountpoint /c
58 mount-options "" /dev/VG/LV /c
59
60 write /a/test "This is a test"
61 write /b/test "This is a test"
62 write /c/test "This is a test"
63
64 # Really try hard to force writes to the disk.
65 umount-all
66 sync
67
68 EOF
69
70 # Now verify that the original disks have not been touched.
71 function serious_error
72 {
73     echo
74     echo
75     echo "***** SERIOUS ERROR *****"
76     echo "qemu's snapshot isolation does not appear to be working."
77     echo "Running libguestfs could cause disk corruption on live guests."
78     echo
79     echo "DO NOT USE libguestfs before you have resolved this problem."
80     echo
81     exit 1
82 }
83
84 if [ "$(md5sum test1.img | awk '{print $1}')" != "$test1_md5sum" ]; then
85     serious_error
86 fi
87 if [ "$(md5sum test2.img | awk '{print $1}')" != "$test2_md5sum" ]; then
88     serious_error
89 fi
90 if [ "$(md5sum test3.img | awk '{print $1}')" != "$test3_md5sum" ]; then
91     serious_error
92 fi
93
94 rm test1.img test2.img test3.img