Debian Codegraph @

bin/mk-data-partition

raw symbols SCIP
1 #! /bin/bashscip-shell filesystem . . `/bin/bash`.
2
3 # (c) 2023-2026 by Thomas Lange, [email protected]
4 #
5 # mk-data-partition ISO | <USB device>
6 # extend an ISO file by size and create a data partition
7 # for USB stick the data partition will take the whole size of the USB stick
8
9 set -o pipefail
10
11 mntdir=/media/datascip-shell filesystem . . `/media/data`.
12
13 diescip-shell shell fai 6.6 die().() {
14
15 local e=$1 # first parameter is the exit code
16 shift
17
18 echoscip-shell system . . echo. "$*" >&2 # print error message
19 exit $e
20 }
21
22 add_p3scip-shell shell fai 6.6 add_p3().() {
23
24 # $1: filename or device name
25 # $2: partition name
26 local type
27
28 [scip-shell system . . `[`. -b "$2" ] && diescip-shell shell fai 6.6 die(). 4 "Partition 3 already exists on $1"
29
30 if [scip-shell system . . `[`. X$fmt = Xexfat ]; then
31 type=7
32 else
33 type=83
34 fi
35 # add 3th partion to the image or device
36
37 {
38 {
39 echoscip-shell system . . echo. -e "n\np\n3\n\n\n"; sleepscip-shell system . . sleep. 2; echoscip-shell system . . echo. -e "v\nt\n3\n$type\nw\n"
40 } | fdisk -w never "$1"
41 } >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || diescip-shell shell fai 6.6 die(). 3 "Cannot add 3rd partition to $1. Error code $?"
42 udevadm settle --timeout=10
43 }
44
45 add_p4scip-shell shell fai 6.6 add_p4().() {
46
47 # $1: filename or device name
48 # $2: partition name
49 local type
50
51 [scip-shell system . . `[`. -b "$2" ] && diescip-shell shell fai 6.6 die(). 4 "Partition 4 already exists on $1"
52
53 if [scip-shell system . . `[`. X$fmt = Xexfat ]; then
54 type=0700
55 else
56 type=8300
57 fi
58
59 # add 4th gpt partion to the image or device
60 {
61 echoscip-shell system . . echo. -e "x\ne\nm\nn\n\n\n\n8300\nw\nY\n" | gdisk $1
62 } >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || diescip-shell shell fai 6.6 die(). 3 "Cannot add 4th partition to $1. Error code $?"
63 udevadm settle --timeout=10
64 }
65
66 make_fsscip-shell shell fai 6.6 make_fs().() {
67
68 if [scip-shell system . . `[`. -b "$1" ]; then
69 if [scip-shell system . . `[`. X$fmt = Xexfat ]; then
70 mkfs.exfat -L MY-DATA $1
71 else
72 mkfs.ext4 -q -L MY-DATA -J size=5 -E lazy_itable_init $1
73 fi
74 echoscip-shell system . . echo. "Data partition MY-DATA created"
75 fi
76 }
77
78 mount_datascip-shell shell fai 6.6 mount_data().() {
79
80 mkdirscip-shell system . . mkdir. $mntdir
81 mountscip-shell system . . mount. $1 $mntdir
82 }
83
84 umount_datascip-shell shell fai 6.6 umount_data().() {
85
86 umountscip-shell system . . umount. $mntdir
87 rmdirscip-shell system . . rmdir. $mntdir
88
89 }
90 copy_datascip-shell shell fai 6.6 copy_data().() {
91
92 cpscip-shell system . . cp. -a $* $mntdir
93 echoscip-shell system . . echo. "Copied files into the ISO."
94 duscip-shell system . . du. --exclude=lost+found -sh $mntdir/*
95 }
96
97
98 usagescip-shell shell fai 6.6 usage().() {
99
100 catscip-shell system . . cat. <<EOF
101 Usage: mk-data-partition [OPTION] IMAGE [dir...]
102
103 Create additional partition for storing data.
104
105 -F Format data partition as exFAT. Default is to use ext4
106 -s Set size of data partition. Default 300M, ignored for USB devices
107 -c Copy a list of directories to the data partition
108 IMAGE Can be an ISO file or the device of an USB stick
109
110 The command adds a third partition to an ISO or an USB stick containing a bootable ISO.
111 This partition contains an ext4 or exFAT file system with label MY-DATA that can be
112 mounted read-write. An ISO file will be extended by the given size, on an USB stick the
113 partition will take the whole remaining size.
114
115 Examples:
116
117 Add a data partition of size 1G to the Debian installer ISO using an ext4 partition
118 # mk-data-partition -s 1G debian-12.2.0-amd64-netinst.iso
119
120 Create the data partition using an exFAT file system on USB named /dev/sdb.
121 First copy (or dd) the ISO onto the USB stick. Then you can add the data partition
122 to the USB stick.
123 # mk-data-partition -F /dev/sdb
124
125 Create the data partition and copy directories A and B to it
126 # mk-data-partition -c debian-12.2.0-amd64-netinst.iso A B
127
128 Copyright (C) 2023-2026 by Thomas Lange
129
130 EOF
131 exit $1
132 }
133
134 while getopts Fhs:c opt ; do
135 case "$opt" in
136 h) usagescip-shell shell fai 6.6 usage(). 0;;
137 c) copy=1 ;;
138 F) fmt=exfat ;;
139 s) size=$OPTARG ;;
140 *) usagescip-shell shell fai 6.6 usage(). 1;;
141 esac
142 done
143 shift $((OPTIND - 1))
144
145 filename=$1
146 shift
147 if [scip-shell system . . `[`. -z "$filename" ]; then
148 echoscip-shell system . . echo. "ERROR: No filename or device supplied"
149 echoscip-shell system . . echo.
150 usagescip-shell shell fai 6.6 usage(). 1
151 fi
152 filename=$(readlinkscip-shell system . . readlink. -f $filename)
153
154 exprscip-shell system . . expr. "$size" : '^[0-9]\+$' >/dev/nullscip-shell filesystem . . `/dev/null`. && diescip-shell shell fai 6.6 die(). 5 "Size $size needs a unit like K, M, G, T"
155
156 if [scip-shell system . . `[`. -n "$1" ] && [scip-shell system . . `[`. X$copy = X ]; then
157 diescip-shell shell fai 6.6 die(). 3 "If you want to copy directories, add -c"
158 fi
159
160 [scip-shell system . . `[`. $(idscip-shell system . . id. -u) != "0" ] && diescip-shell shell fai 6.6 die(). 2 "Run this program as root."
161
162 if [scip-shell system . . `[`. ! -b "$filename" ] && [scip-shell system . . `[`. ! -f "$filename" ]; then
163 diescip-shell shell fai 6.6 die(). 9 "$filename is neither a device nor an existing file"
164 fi
165
166 type=$(fdisk -l $filename 2>/dev/nullscip-shell filesystem . . `/dev/null`.| grepscip-shell system . . grep. -Po '(?<=Disklabel type:\s)...' )
167 case $type in
168 dos)
169 p=3
170 fname=add_p3
171 ;;
172 gpt)
173 p=4
174 fname=add_p4
175 ;;
176 esac
177
178 if [scip-shell system . . `[`. -b "$filename" ]; then
179 # file is a USB device
180 if [scip-shell system . . `[`. -n "$size" ]; then
181 echoscip-shell system . . echo. "Warning: Ignoring the size parameter for USB sticks."
182 fi
183 part=${filename}$p
184 $fname $filename $part
185 make_fsscip-shell shell fai 6.6 make_fs(). $part
186 fi
187
188 if [scip-shell system . . `[`. -f "$filename" ]; then
189
190 # check if the image already has a 3th partition
191 set +e
192 fdisk -l $filename | egrepscip-shell system . . egrep. -q ^${filename}$p && diescip-shell shell fai 6.6 die(). 5 "Partition $p already exists on $filename"
193 size=${size:-300M}
194 echoscip-shell system . . echo. "Extend $filename by $size"
195 truncatescip-shell system . . truncate. -s +$size $filename || diescip-shell shell fai 6.6 die(). 9 "Cannot extend $filename. Aborting"
196 if ! loop=$(losetupscip-shell system . . losetup. -P -f --show $filename); then
197 diescip-shell shell fai 6.6 die(). 5 "Cannot create loop device"
198 fi
199 trap "losetup -d $loop" EXIT
200 part=${loop}p$p
201 $fname $loop $part
202 make_fsscip-shell shell fai 6.6 make_fs(). $part
203 fi
204
205 if [scip-shell system . . `[`. X$copy = X1 ]; then
206
207 mount_datascip-shell shell fai 6.6 mount_data(). $part
208 copy_datascip-shell shell fai 6.6 copy_data(). $*
209 umount_datascip-shell shell fai 6.6 umount_data(). $part
210 fi