ac0f405a0d9ab505fc16c033ae93c7238c8ec99e
[xfstests-dev.git] / tests / btrfs / 012
1 #! /bin/bash
2 # FS QA Test No. 012
3 #
4 # Test btrfs-convert
5 #
6 # 1) create ext4 filesystem & populate it
7 # 2) convert it to btrfs, mount it, verify contents
8 # 3) verify archived ext4 image integriy & contents
9 # 4) populate btrfs fs with new data
10 # 5) roll back conversion to original ext4
11 # 6) verify rolled-back fs integrity & contents
12 #
13 #-----------------------------------------------------------------------
14 # Copyright (c) 2013 Red Hat, Inc.  All Rights Reserved.
15 #
16 # This program is free software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License as
18 # published by the Free Software Foundation.
19 #
20 # This program is distributed in the hope that it would be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write the Free Software Foundation,
27 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28 #-----------------------------------------------------------------------
29 #
30
31 seq=`basename $0`
32 seqres=$RESULT_DIR/$seq
33 echo "== QA output created by $seq"
34
35 here=`pwd`
36 tmp=/tmp/$$
37 status=1        # failure is the default!
38 trap "_cleanup; exit \$status" 0 1 2 3 15
39
40 _cleanup()
41 {
42     cd /
43     rm -f $tmp.*
44 }
45
46 # get standard environment, filters and checks
47 . ./common/rc
48 . ./common/filter.btrfs
49
50 # real QA test starts here
51
52 # Modify as appropriate.
53 _supported_fs btrfs
54 _supported_os Linux
55 _require_scratch_nocheck
56
57 _require_command "$BTRFS_CONVERT_PROG" btrfs-convert
58 _require_command "$MKFS_EXT4_PROG" mkfs.ext4
59 _require_command "$E2FSCK_PROG" e2fsck
60
61 rm -f $seqres.full
62
63 BLOCK_SIZE=`_get_block_size $TEST_DIR`
64
65 # Create & populate an ext4 filesystem
66 $MKFS_EXT4_PROG -F -b $BLOCK_SIZE $SCRATCH_DEV > $seqres.full 2>&1 || \
67         _notrun "Could not create ext4 filesystem"
68 # Manual mount so we don't use -t btrfs or selinux context
69 mount -t ext4 $SCRATCH_DEV $SCRATCH_MNT
70
71 cp -aR /lib/modules/`uname -r`/ $SCRATCH_MNT
72 _scratch_unmount
73
74 # Convert it to btrfs, mount it, verify the data
75 $BTRFS_CONVERT_PROG $SCRATCH_DEV >> $seqres.full 2>&1 || \
76         _fail "btrfs-convert failed"
77 _try_scratch_mount || _fail "Could not mount new btrfs fs"
78 # (Ignore the symlinks which may be broken/nonexistent)
79 diff -r /lib/modules/`uname -r`/ $SCRATCH_MNT/`uname -r`/ 2>&1 | grep -vw "source\|build"
80
81 # Old ext4 image file should exist & be consistent
82 $E2FSCK_PROG -fn $SCRATCH_MNT/ext2_saved/image >> $seqres.full 2>&1 || \
83         _fail "archived ext4 image is corrupt"
84
85 # And the files in that image should match
86 mkdir -p $SCRATCH_MNT/mnt
87 mount -o loop $SCRATCH_MNT/ext2_saved/image $SCRATCH_MNT/mnt || \
88         _fail "could not loop mount saved ext4 image"
89 # Ignore the symlinks which may be broken/nonexistent
90 diff -r /lib/modules/`uname -r`/ $SCRATCH_MNT/mnt/`uname -r`/ 2>&1 | grep -vw "source\|build"
91 umount $SCRATCH_MNT/mnt
92
93 # Now put some fresh data on the btrfs fs
94 mkdir -p $SCRATCH_MNT/new 
95 cp -aR /lib/modules/`uname -r`/ $SCRATCH_MNT/new
96
97 _scratch_unmount
98
99 # Now restore the ext4 device
100 $BTRFS_CONVERT_PROG -r $SCRATCH_DEV >> $seqres.full 2>&1 || \
101         _fail "btrfs-convert rollback failed"
102
103 # Check it again
104 $E2FSCK_PROG -fn $SCRATCH_DEV >> $seqres.full 2>&1 || \
105         _fail "restored ext4 image is corrupt"
106
107 # Mount the un-converted ext4 device & check the contents
108 mount -t ext4 $SCRATCH_DEV $SCRATCH_MNT
109 # (Ignore the symlinks which may be broken/nonexistent)
110 diff -r /lib/modules/`uname -r`/ $SCRATCH_MNT/`uname -r`/ 2>&1 | grep -vw "source\|build"
111
112 _scratch_unmount
113
114 # Convert it to btrfs, mount it and delete "ext2_saved"
115 $BTRFS_CONVERT_PROG $SCRATCH_DEV >> $seqres.full 2>&1 || \
116         _fail "btrfs-convert failed"
117 _try_scratch_mount || _fail "Could not mount new btrfs fs"
118 $BTRFS_UTIL_PROG subvolume delete $SCRATCH_MNT/ext2_saved >> $seqres.full 2>&1 ||
119         _fail "failed to delete ext2_saved subvolume"
120 _scratch_unmount
121
122 # Now restore the ext4 device, expecting a failure
123 $BTRFS_CONVERT_PROG -r $SCRATCH_DEV >> $seqres.full 2>&1
124 [ $? -eq 1 ] || _fail "Failure is expected, but btrfs-convert returns with rollback complete"
125
126 # success, all done
127 status=0
128 exit