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

size=1024
fstype=ext4
label=casper-rw
6
config=''
7
outputfile=persistence.dat
longpanda's avatar
longpanda committed
8
9

print_usage() {
10
    echo 'Usage:  sudo ./CreatePersistentImg.sh [ -s size ] [ -t fstype ] [ -l LABEL ] [ -c CFG ] [ -e ]'
longpanda's avatar
longpanda committed
11
12
13
14
    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'
15
    echo '   -c configfile name inside the persistence file. File content is "/ union"'
16
    echo '   -o outputfile name, default is persistence.dat'
17
    echo '   -e enable encryption, disabled by default (only few distros support this)'    
longpanda's avatar
longpanda committed
18
19
20
21
22
23
24
25
26
27
28
29
30
    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
31
32
33
    elif [ "$1" = "-c" ]; then
        shift
        config=$1
34
35
36
    elif [ "$1" = "-o" ]; then
        shift
        outputfile=$1
37
38
39
    elif [ "$1" = "-e" ]; then
        read -s -p "Encryption passphrase: " passphrase
        echo
longpanda's avatar
update  
longpanda committed
40
41
42
    elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
        print_usage
        exit 0
longpanda's avatar
longpanda committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    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
59
60
61
    vtMinSize=1
    if echo $fstype | grep -q '^xfs$'; then
        vtMinSize=16
longpanda's avatar
longpanda committed
62
    fi
63
64
65
66
67
    
    if [ $size -lt $vtMinSize ]; then
        echo "size too small ($size)"
        exit 1
    fi    
longpanda's avatar
longpanda committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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

86
87
88
89
if [ "$outputdir" != "persistence.dat" ]; then
    mkdir -p "$(dirname "$outputfile")"
fi

longpanda's avatar
longpanda committed
90
# 00->ff avoid sparse file
91
dd if=/dev/zero  bs=1M count=$size | tr '\000' '\377' > "$outputfile"
longpanda's avatar
longpanda committed
92
93
94
95
sync

freeloop=$(losetup -f)

96
losetup $freeloop "$outputfile"
longpanda's avatar
longpanda committed
97

98
99
100
101
102
103
104
if [ ! -z "$passphrase" ]; then
    printf "$passphrase" | cryptsetup -q --verbose luksFormat $freeloop -
    printf "$passphrase" | cryptsetup -q --verbose luksOpen $freeloop persist_decrypted -
    _freeloop=$freeloop
    freeloop="/dev/mapper/persist_decrypted"
fi

longpanda's avatar
longpanda committed
105
106
107
108
mkfs -t $fstype $fsopt -L $label $freeloop 

sync

109
110
111
112
113
114
115
116
117
118
119
120
121
if [ -n "$config" ]; then
    if [ -d ./persist_tmp_mnt ]; then
        rm -rf ./persist_tmp_mnt
    fi
    
    mkdir ./persist_tmp_mnt
    if mount $freeloop ./persist_tmp_mnt; then
        echo '/ union' > ./persist_tmp_mnt/$config
        sync
        umount ./persist_tmp_mnt
    fi
    rm -rf ./persist_tmp_mnt
fi
longpanda's avatar
longpanda committed
122

123
124
125
126
127
if [ ! -z "$passphrase" ]; then
    cryptsetup luksClose $freeloop
    freeloop=$_freeloop
fi

128
losetup -d $freeloop