Debian Codegraph @

bin/mk-data-partition

raw symbols SCIP
1 #! /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/data
12
13 die() {
14
15 local e=$1 # first parameter is the exit code
16 shift
17
18 echo "$*" >&2 # print error message
19 exit $e
20 }
21
22 add_p3() {
23
24 # $1: filename or device name
25 # $2: partition name
26 local type
27
28 [ -b "$2" ] && die 4 "Partition 3 already exists on $1"
29
30 if [ 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 echo -e "n\np\n3\n\n\n"; sleep 2; echo -e "v\nt\n3\n$type\nw\n"
40 } | fdisk -w never "$1"
41 } >/dev/null 2>&1 || die 3 "Cannot add 3rd partition to $1. Error code $?"
42 udevadm settle --timeout=10
43 }
44
45 add_p4() {
46
47 # $1: filename or device name
48 # $2: partition name
49 local type
50
51 [ -b "$2" ] && die 4 "Partition 4 already exists on $1"
52
53 if [ 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 echo -e "x\ne\nm\nn\n\n\n\n8300\nw\nY\n" | gdisk $1
62 } >/dev/null 2>&1 || die 3 "Cannot add 4th partition to $1. Error code $?"
63 udevadm settle --timeout=10
64 }
65
66 make_fs() {
67
68 if [ -b "$1" ]; then
69 if [ 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 echo "Data partition MY-DATA created"
75 fi
76 }
77
78 mount_data() {
79
80 mkdir $mntdir
81 mount $1 $mntdir
82 }
83
84 umount_data() {
85
86 umount $mntdir
87 rmdir $mntdir
88
89 }
90 copy_data() {
91
92 cp -a $* $mntdir
93 echo "Copied files into the ISO."
94 du --exclude=lost+found -sh $mntdir/*
95 }
96
97
98 usage() {
99
100 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) usage 0;;
137 c) copy=1 ;;
138 F) fmt=exfat ;;
139 s) size=$OPTARG ;;
140 *) usage 1;;
141 esac
142 done
143 shift $((OPTIND - 1))
144
145 filename=$1
146 shift
147 if [ -z "$filename" ]; then
148 echo "ERROR: No filename or device supplied"
149 echo
150 usage 1
151 fi
152 filename=$(readlink -f $filename)
153
154 expr "$size" : '^[0-9]\+$' >/dev/null && die 5 "Size $size needs a unit like K, M, G, T"
155
156 if [ -n "$1" ] && [ X$copy = X ]; then
157 die 3 "If you want to copy directories, add -c"
158 fi
159
160 [ $(id -u) != "0" ] && die 2 "Run this program as root."
161
162 if [ ! -b "$filename" ] && [ ! -f "$filename" ]; then
163 die 9 "$filename is neither a device nor an existing file"
164 fi
165
166 type=$(fdisk -l $filename 2>/dev/null| 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 [ -b "$filename" ]; then
179 # file is a USB device
180 if [ -n "$size" ]; then
181 echo "Warning: Ignoring the size parameter for USB sticks."
182 fi
183 part=${filename}$p
184 $fname $filename $part
185 make_fs $part
186 fi
187
188 if [ -f "$filename" ]; then
189
190 # check if the image already has a 3th partition
191 set +e
192 fdisk -l $filename | egrep -q ^${filename}$p && die 5 "Partition $p already exists on $filename"
193 size=${size:-300M}
194 echo "Extend $filename by $size"
195 truncate -s +$size $filename || die 9 "Cannot extend $filename. Aborting"
196 if ! loop=$(losetup -P -f --show $filename); then
197 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_fs $part
203 fi
204
205 if [ X$copy = X1 ]; then
206
207 mount_data $part
208 copy_data $*
209 umount_data $part
210 fi