]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/CreatePersistentImg.sh
Norwegian Bokmål language added (#1189)
[Ventoy.git] / INSTALL / CreatePersistentImg.sh
1 #!/bin/sh
2
3 size=1024
4 fstype=ext4
5 label=casper-rw
6 config=''
7 outputfile=persistence.dat
8
9 print_usage() {
10 echo 'Usage: CreatePersistentImg.sh [ -s size ] [ -t fstype ] [ -l LABEL ] [ -c CFG ]'
11 echo ' OPTION: (optional)'
12 echo ' -s size in MB, default is 1024'
13 echo ' -t filesystem type, default is ext4 ext2/ext3/ext4/xfs are supported now'
14 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 ''
18 }
19
20 while [ -n "$1" ]; do
21 if [ "$1" = "-s" ]; then
22 shift
23 size=$1
24 elif [ "$1" = "-t" ]; then
25 shift
26 fstype=$1
27 elif [ "$1" = "-l" ]; then
28 shift
29 label=$1
30 elif [ "$1" = "-c" ]; then
31 shift
32 config=$1
33 elif [ "$1" = "-o" ]; then
34 shift
35 outputfile=$1
36 elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
37 print_usage
38 exit 0
39 else
40 print_usage
41 exit 1
42 fi
43 shift
44 done
45
46
47 # check label
48 if [ -z "$label" ]; then
49 echo "The label can NOT be empty."
50 exit 1
51 fi
52
53 # check size
54 if echo $size | grep -q "^[0-9][0-9]*$"; then
55 if [ $size -le 1 ]; then
56 echo "Invalid size $size"
57 exit 1
58 fi
59 else
60 echo "Invalid size $size"
61 exit 1
62 fi
63
64
65 # check file system type
66 # nodiscard must be set for ext2/3/4
67 # -K must be set for xfs
68 if echo $fstype | grep -q '^ext[234]$'; then
69 fsopt='-E nodiscard'
70 elif [ "$fstype" = "xfs" ]; then
71 fsopt='-K'
72 else
73 echo "unsupported file system $fstype"
74 exit 1
75 fi
76
77 if [ "$outputdir" != "persistence.dat" ]; then
78 mkdir -p "$(dirname "$outputfile")"
79 fi
80
81 # 00->ff avoid sparse file
82 dd if=/dev/zero bs=1M count=$size | tr '\000' '\377' > "$outputfile"
83 sync
84
85 freeloop=$(losetup -f)
86
87 losetup $freeloop "$outputfile"
88
89 mkfs -t $fstype $fsopt -L $label $freeloop
90
91 sync
92
93 if [ -n "$config" ]; then
94 if [ -d ./persist_tmp_mnt ]; then
95 rm -rf ./persist_tmp_mnt
96 fi
97
98 mkdir ./persist_tmp_mnt
99 if mount $freeloop ./persist_tmp_mnt; then
100 echo '/ union' > ./persist_tmp_mnt/$config
101 sync
102 umount ./persist_tmp_mnt
103 fi
104 rm -rf ./persist_tmp_mnt
105 fi
106
107 losetup -d $freeloop