ExtendPersistentImg.sh 2.95 KB
Newer Older
longpanda's avatar
longpanda committed
1
#!/bin/bash
longpanda's avatar
longpanda committed
2
3
4
5
6

print_usage() {
    echo 'Usage:  ExtendPersistentImg.sh file size'
    echo '   file   persistent dat file'
    echo '   size   extend size in MB'
7
8
9
    echo 'Examples:'
    echo '   sh ExtendPersistentImg.sh ubuntu.dat 2048 - This command would extend ubuntu.dat by 2048MB (2GB)'
    echo '   sh ExtendPersistentImg.sh ubuntu.dat -2048 - This command reduces ubuntu.dat by 2048MB (-2GB)'
longpanda's avatar
longpanda committed
10
11
12
13
14
15
16
17
18
19
20
21
22
    echo ''
}

if [ -z "$1" -o "$1" = "-h" ]; then
    print_usage
    exit 1
fi

if [ -z "$2" ]; then
    print_usage
    exit 1
fi

23
24
25
26
27
uid=$(id -u)
if [ $uid -ne 0 ]; then
    print_err "Please use sudo or run the script as root."
    exit 1
fi
longpanda's avatar
longpanda committed
28
29
30
31
32
33
34
35
36
37
38
39

if [ "$1" = "__vbash__" ]; then
    shift
else
    if readlink /bin/sh | grep -q bash; then
        :
    else
        exec /bin/bash $0 "__vbash__" "$@"
    fi
fi


longpanda's avatar
longpanda committed
40
41
42
43
44
45
46
47
file=$1
size=$2

if [ ! -f "$file" ]; then
    echo "$file not exist."
    exit 1
fi

48
49
50
51
52
53
54
if echo $size | grep -q "^-"; then
    mode="Shrink"
    size=${size:1}
else
    mode="Extend"
fi

longpanda's avatar
longpanda committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
if echo $size | grep -q "[^0-9]"; then
    print_usage
    exit 1
fi

fsize=$(stat -c '%s' $file)

fsmod=$(expr $fsize % 1024)
if [ $fsmod -ne 0 ]; then
    echo "File size of $file is not aligned by 1MB, please check."
    exit 1
fi


fsMB=$(expr $fsize / 1024 / 1024)
70
71
72
73
74
75
76
77
78
79
80

if [ "$mode" = "Extend" ]; then
    total=$(expr $fsMB + $size)
else
    if [ $fsMB -le $size ]; then
        echo "File size of $file is less than ${size}MB."
        exit 1
    fi
    total=$(expr $fsMB - $size)
fi

longpanda's avatar
longpanda committed
81
82
83

magic=$(hexdump -n3 -e  '3/1 "%02X"' $file)
if [ "$magic" = "584653" ]; then
84
85
86
87
88
    if [ "$mode" = "Shrink" ]; then
        echo "Shrink is not supported for XFS filesystem."
        exit 1
    fi

longpanda's avatar
longpanda committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    if which xfs_growfs >/dev/null 2>&1; then
        cmd=xfs_growfs
    else
        echo 'xfs_growfs not found, please install xfsprogs first'
        exit 1
    fi
else
    if which resize2fs >/dev/null 2>&1; then
        cmd=resize2fs
    else
        echo 'resize2fs not found, please install e2fsprogs first'
        exit 1
    fi
fi

104
105
106
107
108
109
110
if [ "$mode" = "Extend" ]; then
    echo "$mode dat file... (current is ${fsMB}MB, append ${size}MB, total ${total}MB)"
    dd if=/dev/zero bs=1M count=$size status=none >> "$file"
    sync
else
    echo "$mode dat file... (current is ${fsMB}MB, reduce ${size}MB, finally ${total}MB)"
fi
longpanda's avatar
longpanda committed
111
112
113
114
115
116


freeloop=$(losetup -f)
losetup $freeloop "$file"

if [ "$cmd" = "resize2fs" ]; then    
117
    echo "$mode ext filesystem by resize2fs ..."
longpanda's avatar
longpanda committed
118
119
120
121
122
    echo "resize2fs $freeloop ${total}M"
    e2fsck -f $freeloop
    resize2fs $freeloop ${total}M
    ret=$?
else
123
    echo "$mode xfs filesystem by xfs_growfs ..."
longpanda's avatar
longpanda committed
124
125
126
127
128
129
130
131
132
    tmpdir=$(mktemp -d)
    mount $freeloop $tmpdir
    xfs_growfs $freeloop
    ret=$?
    umount $tmpdir && rm -rf $tmpdir
fi

losetup -d $freeloop

133
134
135
136
137
138
if [ $ret -eq 0 -a "$mode" = "Shrink" ]; then
    echo "truncate persistent file ..."
    truncate "$file" -s ${total}M
    ret=$?
fi

longpanda's avatar
longpanda committed
139
140
141
142
143
144
145
echo ""
if [ $ret -eq 0 ]; then
    echo "======= SUCCESS ========="
else
    echo "======= FAILED ========="
fi
echo ""