CreatePersistentImg.sh 1.41 KB
Newer Older
longpanda's avatar
longpanda committed
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
#!/bin/sh

size=1024
fstype=ext4
label=casper-rw

print_usage() {
    echo 'Usage:  CreatePersistentImg.sh [ -s size ] [ -t fstype ] [ -l LABEL ]'
    echo '  OPTION: (optional)'
    echo '   -s size in MB, default is 1024'
    echo '   -t filesystem type, default is ext4  ext2/ext3/ext4/xfs are supported now'
    echo '   -l label, default is casper-rw'
    echo ''
}

while [ -n "$1" ]; do
    if [ "$1" = "-s" ]; then
        shift
        size=$1
    elif [ "$1" = "-t" ]; then
        shift
        fstype=$1
    elif [ "$1" = "-l" ]; then
        shift
        label=$1
    else
        print_usage
        exit 1
    fi
    shift
done


# check label
if [ -z "$label" ]; then
    echo "The label can NOT be empty."
    exit 1
fi

# check size
if echo $size | grep -q "^[0-9][0-9]*$"; then
    if [ $size -le 1 ]; then
        echo "Invalid size $size"
        exit 1
    fi
else
    echo "Invalid size $size"
    exit 1
fi


# check file system type
# nodiscard must be set for ext2/3/4
# -K must be set for xfs 
if echo $fstype | grep -q '^ext[234]$'; then
    fsopt='-E nodiscard'
elif [ "$fstype" = "xfs" ]; then
    fsopt='-K'
else
    echo "unsupported file system $fstype"
    exit 1
fi

# 00->ff avoid sparse file
longpanda's avatar
longpanda committed
65
dd if=/dev/zero  bs=1M count=$size | tr '\000' '\377' > persistence.dat
longpanda's avatar
longpanda committed
66
67
68
69
sync

freeloop=$(losetup -f)

longpanda's avatar
longpanda committed
70
losetup $freeloop persistence.dat
longpanda's avatar
longpanda committed
71
72
73
74
75
76
77

mkfs -t $fstype $fsopt -L $label $freeloop 

sync

losetup -d $freeloop