2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2013 Red Hat, Inc. All Rights Reserved.
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
17 _begin_fstest auto convert
19 # Import common functions.
20 . ./common/filter.btrfs
22 # real QA test starts here
24 # Modify as appropriate.
26 _require_scratch_nocheck
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}"
34 _require_extra_fs ext4
36 BLOCK_SIZE=`_get_block_size $TEST_DIR`
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
44 _require_fs_space $SCRATCH_MNT $(du -s /lib/modules/`uname -r` | ${AWK_PROG} '{print $1}')
46 cp -aR /lib/modules/`uname -r`/ $SCRATCH_MNT
49 # Convert it to btrfs, mount it, verify the data
50 $BTRFS_CONVERT_PROG $SCRATCH_DEV >> $seqres.full 2>&1 || \
51 _fail "btrfs-convert failed"
52 _try_scratch_mount || _fail "Could not mount new btrfs fs"
53 # (Ignore the symlinks which may be broken/nonexistent)
54 diff -r /lib/modules/`uname -r`/ $SCRATCH_MNT/`uname -r`/ 2>&1 | grep -vw "source\|build"
56 # Old ext4 image file should exist & be consistent
57 $E2FSCK_PROG -fn $SCRATCH_MNT/ext2_saved/image >> $seqres.full 2>&1 || \
58 _fail "archived ext4 image is corrupt"
60 # And the files in that image should match
61 mkdir -p $SCRATCH_MNT/mnt
62 mount -o loop $SCRATCH_MNT/ext2_saved/image $SCRATCH_MNT/mnt || \
63 _fail "could not loop mount saved ext4 image"
64 # Ignore the symlinks which may be broken/nonexistent
65 diff -r /lib/modules/`uname -r`/ $SCRATCH_MNT/mnt/`uname -r`/ 2>&1 | grep -vw "source\|build"
66 umount $SCRATCH_MNT/mnt
68 # Now put some fresh data on the btrfs fs
69 mkdir -p $SCRATCH_MNT/new
70 cp -aR /lib/modules/`uname -r`/ $SCRATCH_MNT/new
74 # Now restore the ext4 device
75 $BTRFS_CONVERT_PROG -r $SCRATCH_DEV >> $seqres.full 2>&1 || \
76 _fail "btrfs-convert rollback failed"
79 $E2FSCK_PROG -fn $SCRATCH_DEV >> $seqres.full 2>&1 || \
80 _fail "restored ext4 image is corrupt"
82 # Mount the un-converted ext4 device & check the contents
83 mount -t ext4 $SCRATCH_DEV $SCRATCH_MNT
84 # (Ignore the symlinks which may be broken/nonexistent)
85 diff -r /lib/modules/`uname -r`/ $SCRATCH_MNT/`uname -r`/ 2>&1 | grep -vw "source\|build"
89 # Convert it to btrfs, mount it and delete "ext2_saved"
90 $BTRFS_CONVERT_PROG $SCRATCH_DEV >> $seqres.full 2>&1 || \
91 _fail "btrfs-convert failed"
92 _try_scratch_mount || _fail "Could not mount new btrfs fs"
93 $BTRFS_UTIL_PROG subvolume delete $SCRATCH_MNT/ext2_saved >> $seqres.full 2>&1 ||
94 _fail "failed to delete ext2_saved subvolume"
97 # Now restore the ext4 device, expecting a failure
98 $BTRFS_CONVERT_PROG -r $SCRATCH_DEV >> $seqres.full 2>&1
99 [ $? -eq 1 ] || _fail "Failure is expected, but btrfs-convert returns with rollback complete"