fd43da5333dcc3112760cc700e6456f97716ead4
[xfstests-dev.git] / tests / btrfs / 012
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2013 Red Hat, Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. 012
6 #
7 # Test btrfs-convert
8 #
9 # 1) create ext4 filesystem & populate it
10 # 2) convert it to btrfs, mount it, verify contents
11 # 3) verify archived ext4 image integriy & contents
12 # 4) populate btrfs fs with new data
13 # 5) roll back conversion to original ext4
14 # 6) verify rolled-back fs integrity & contents
15 #
16 seq=`basename $0`
17 seqres=$RESULT_DIR/$seq
18 echo "== QA output created by $seq"
19
20 here=`pwd`
21 tmp=/tmp/$$
22 status=1        # failure is the default!
23 trap "_cleanup; exit \$status" 0 1 2 3 15
24
25 _cleanup()
26 {
27     cd /
28     rm -f $tmp.*
29 }
30
31 # get standard environment, filters and checks
32 . ./common/rc
33 . ./common/filter.btrfs
34
35 # real QA test starts here
36
37 # Modify as appropriate.
38 _supported_fs btrfs
39 _require_scratch_nocheck
40
41 _require_command "$BTRFS_CONVERT_PROG" btrfs-convert
42 _require_command "$MKFS_EXT4_PROG" mkfs.ext4
43 _require_command "$E2FSCK_PROG" e2fsck
44
45 rm -f $seqres.full
46
47 BLOCK_SIZE=`_get_block_size $TEST_DIR`
48
49 # Create & populate an ext4 filesystem
50 $MKFS_EXT4_PROG -F -b $BLOCK_SIZE $SCRATCH_DEV > $seqres.full 2>&1 || \
51         _notrun "Could not create ext4 filesystem"
52 # Manual mount so we don't use -t btrfs or selinux context
53 mount -t ext4 $SCRATCH_DEV $SCRATCH_MNT
54
55 cp -aR /lib/modules/`uname -r`/ $SCRATCH_MNT
56 _scratch_unmount
57
58 # Convert it to btrfs, mount it, verify the data
59 $BTRFS_CONVERT_PROG $SCRATCH_DEV >> $seqres.full 2>&1 || \
60         _fail "btrfs-convert failed"
61 _try_scratch_mount || _fail "Could not mount new btrfs fs"
62 # (Ignore the symlinks which may be broken/nonexistent)
63 diff -r /lib/modules/`uname -r`/ $SCRATCH_MNT/`uname -r`/ 2>&1 | grep -vw "source\|build"
64
65 # Old ext4 image file should exist & be consistent
66 $E2FSCK_PROG -fn $SCRATCH_MNT/ext2_saved/image >> $seqres.full 2>&1 || \
67         _fail "archived ext4 image is corrupt"
68
69 # And the files in that image should match
70 mkdir -p $SCRATCH_MNT/mnt
71 mount -o loop $SCRATCH_MNT/ext2_saved/image $SCRATCH_MNT/mnt || \
72         _fail "could not loop mount saved ext4 image"
73 # Ignore the symlinks which may be broken/nonexistent
74 diff -r /lib/modules/`uname -r`/ $SCRATCH_MNT/mnt/`uname -r`/ 2>&1 | grep -vw "source\|build"
75 umount $SCRATCH_MNT/mnt
76
77 # Now put some fresh data on the btrfs fs
78 mkdir -p $SCRATCH_MNT/new 
79 cp -aR /lib/modules/`uname -r`/ $SCRATCH_MNT/new
80
81 _scratch_unmount
82
83 # Now restore the ext4 device
84 $BTRFS_CONVERT_PROG -r $SCRATCH_DEV >> $seqres.full 2>&1 || \
85         _fail "btrfs-convert rollback failed"
86
87 # Check it again
88 $E2FSCK_PROG -fn $SCRATCH_DEV >> $seqres.full 2>&1 || \
89         _fail "restored ext4 image is corrupt"
90
91 # Mount the un-converted ext4 device & check the contents
92 mount -t ext4 $SCRATCH_DEV $SCRATCH_MNT
93 # (Ignore the symlinks which may be broken/nonexistent)
94 diff -r /lib/modules/`uname -r`/ $SCRATCH_MNT/`uname -r`/ 2>&1 | grep -vw "source\|build"
95
96 _scratch_unmount
97
98 # Convert it to btrfs, mount it and delete "ext2_saved"
99 $BTRFS_CONVERT_PROG $SCRATCH_DEV >> $seqres.full 2>&1 || \
100         _fail "btrfs-convert failed"
101 _try_scratch_mount || _fail "Could not mount new btrfs fs"
102 $BTRFS_UTIL_PROG subvolume delete $SCRATCH_MNT/ext2_saved >> $seqres.full 2>&1 ||
103         _fail "failed to delete ext2_saved subvolume"
104 _scratch_unmount
105
106 # Now restore the ext4 device, expecting a failure
107 $BTRFS_CONVERT_PROG -r $SCRATCH_DEV >> $seqres.full 2>&1
108 [ $? -eq 1 ] || _fail "Failure is expected, but btrfs-convert returns with rollback complete"
109
110 # success, all done
111 status=0
112 exit