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