summaryrefslogtreecommitdiff
path: root/tests/btrfs/177
blob: 2fd11e89ca350364287d7ce6a7d7352e5e410d10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2018 Facebook.  All Rights Reserved.
#
# FS QA Test 177
#
# Test relocation (balance and resize) with an active swap file.
#
. ./common/preamble
_begin_fstest auto quick swap balance

. ./common/filter
. ./common/btrfs

# Modify as appropriate.
_supported_fs btrfs
_require_scratch_swapfile

# Eliminate the differences between the old and new output formats
# Old format:
# 	Resize 'SCRATCH_MNT' of '1073741824'
# New format:
# 	Resize device id 1 (SCRATCH_DEV) from 3.00GiB to 1.00GiB
# Convert both outputs to:
# 	Resized to 1073741824
convert_resize_output()
{
        local _field
        local _val
        local _suffix
        _field=`$AWK_PROG '{print $NF}' | tr -d "'"`
        # remove trailing zeroes
        _val=`echo $_field | $AWK_PROG '{print $1 * 1}'`
        # get the first unit char, for example return G in case we have GiB
        _suffix=`echo $_field | grep -o "[GMB]"`
        if [ -z "$_suffix" ]; then
                _suffix="B"
        fi
        _val=`echo "$_val$_suffix" | _filter_size_to_bytes`
	echo "Resized to $_val"
}



swapfile="$SCRATCH_MNT/swap"

_require_scratch_size $((3 * 1024 * 1024)) #kB

# First, create a 1GB filesystem.
fssize=$((1024 * 1024 * 1024))
_scratch_mkfs_sized $fssize >> $seqres.full 2>&1
_scratch_mount

# Create a small file and run balance so we shall deal with the chunk
# size as allocated by the kernel, mkfs allocated chunks are smaller.
dd if=/dev/zero of="$SCRATCH_MNT/fill" bs=4096 count=1 >> $seqres.full 2>&1
_run_btrfs_balance_start "$SCRATCH_MNT" >>$seqres.full

# Now fill it up.
dd if=/dev/zero of="$SCRATCH_MNT/refill" bs=4096 >> $seqres.full 2>&1

# Now add more space and create a swap file. We know that the first $fssize
# of the filesystem was used, so the swap file must be in the new part of the
# filesystem.
$BTRFS_UTIL_PROG filesystem resize $((3 * fssize)) "$SCRATCH_MNT" | convert_resize_output
_format_swapfile "$swapfile" $((32 * 1024 * 1024)) > /dev/null
swapon "$swapfile"

# Free up the first 1GB of the filesystem.
rm -f "$SCRATCH_MNT/fill"
rm -f "$SCRATCH_MNT/refill"

# Get rid of empty block groups and also make sure that balance skips block
# groups containing active swap files.
_run_btrfs_balance_start "$SCRATCH_MNT" >>$seqres.full

# Try to shrink away the area occupied by the swap file, which should fail.
$BTRFS_UTIL_PROG filesystem resize 1G "$SCRATCH_MNT" 2>&1 | grep -o "Text file busy"

swapoff "$swapfile"

# It should work again after swapoff.
$BTRFS_UTIL_PROG filesystem resize $fssize "$SCRATCH_MNT" | convert_resize_output

status=0
exit