3040a655095c8f42b77aa1173f923d6cbf4f9250
[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 . ./common/preamble
17 _begin_fstest auto convert
18
19 # Import common functions.
20 . ./common/filter.btrfs
21
22 # real QA test starts here
23
24 # Modify as appropriate.
25 _supported_fs btrfs
26 _require_scratch_nocheck
27
28 _require_command "$BTRFS_CONVERT_PROG" btrfs-convert
29 _require_command "$MKFS_EXT4_PROG" mkfs.ext4
30 _require_command "$E2FSCK_PROG" e2fsck
31 # ext4 does not support zoned block device
32 _require_non_zoned_device "${SCRATCH_DEV}"
33
34 _require_fs_space $SCRATCH_MNT $(du -s /lib/modules/`uname -r` | ${AWK_PROG} '{print $1}')
35
36 BLOCK_SIZE=`_get_block_size $TEST_DIR`
37
38 # Create & populate an ext4 filesystem
39 $MKFS_EXT4_PROG -F -b $BLOCK_SIZE $SCRATCH_DEV > $seqres.full 2>&1 || \
40         _notrun "Could not create ext4 filesystem"
41 # Manual mount so we don't use -t btrfs or selinux context
42 mount -t ext4 $SCRATCH_DEV $SCRATCH_MNT
43
44 cp -aR /lib/modules/`uname -r`/ $SCRATCH_MNT
45 _scratch_unmount
46
47 # Convert it to btrfs, mount it, verify the data
48 $BTRFS_CONVERT_PROG $SCRATCH_DEV >> $seqres.full 2>&1 || \
49         _fail "btrfs-convert failed"
50 _try_scratch_mount || _fail "Could not mount new btrfs fs"
51 # (Ignore the symlinks which may be broken/nonexistent)
52 diff -r /lib/modules/`uname -r`/ $SCRATCH_MNT/`uname -r`/ 2>&1 | grep -vw "source\|build"
53
54 # Old ext4 image file should exist & be consistent
55 $E2FSCK_PROG -fn $SCRATCH_MNT/ext2_saved/image >> $seqres.full 2>&1 || \
56         _fail "archived ext4 image is corrupt"
57
58 # And the files in that image should match
59 mkdir -p $SCRATCH_MNT/mnt
60 mount -o loop $SCRATCH_MNT/ext2_saved/image $SCRATCH_MNT/mnt || \
61         _fail "could not loop mount saved ext4 image"
62 # Ignore the symlinks which may be broken/nonexistent
63 diff -r /lib/modules/`uname -r`/ $SCRATCH_MNT/mnt/`uname -r`/ 2>&1 | grep -vw "source\|build"
64 umount $SCRATCH_MNT/mnt
65
66 # Now put some fresh data on the btrfs fs
67 mkdir -p $SCRATCH_MNT/new 
68 cp -aR /lib/modules/`uname -r`/ $SCRATCH_MNT/new
69
70 _scratch_unmount
71
72 # Now restore the ext4 device
73 $BTRFS_CONVERT_PROG -r $SCRATCH_DEV >> $seqres.full 2>&1 || \
74         _fail "btrfs-convert rollback failed"
75
76 # Check it again
77 $E2FSCK_PROG -fn $SCRATCH_DEV >> $seqres.full 2>&1 || \
78         _fail "restored ext4 image is corrupt"
79
80 # Mount the un-converted ext4 device & check the contents
81 mount -t ext4 $SCRATCH_DEV $SCRATCH_MNT
82 # (Ignore the symlinks which may be broken/nonexistent)
83 diff -r /lib/modules/`uname -r`/ $SCRATCH_MNT/`uname -r`/ 2>&1 | grep -vw "source\|build"
84
85 _scratch_unmount
86
87 # Convert it to btrfs, mount it and delete "ext2_saved"
88 $BTRFS_CONVERT_PROG $SCRATCH_DEV >> $seqres.full 2>&1 || \
89         _fail "btrfs-convert failed"
90 _try_scratch_mount || _fail "Could not mount new btrfs fs"
91 $BTRFS_UTIL_PROG subvolume delete $SCRATCH_MNT/ext2_saved >> $seqres.full 2>&1 ||
92         _fail "failed to delete ext2_saved subvolume"
93 _scratch_unmount
94
95 # Now restore the ext4 device, expecting a failure
96 $BTRFS_CONVERT_PROG -r $SCRATCH_DEV >> $seqres.full 2>&1
97 [ $? -eq 1 ] || _fail "Failure is expected, but btrfs-convert returns with rollback complete"
98
99 # success, all done
100 status=0
101 exit