Debian Codegraph @

examples/simple/basefiles/mk-basefile

raw
1 #! /bin/bash
2
3 # mk-basefile, create basefiles for some distributions
4 #
5 # Thomas Lange, Uni Koeln, 2011-2026
6 # based on the Makefile implementation of Michael Goetze
7 #
8 # Usage example: mk-basefile -J STRETCH64
9 # This will create a STRETCH64.tar.xz basefile.
10
11 # Supported distributions (i386/amd64):
12 # Debian GNU/Linux
13 # Ubuntu 14.04/16.04/20.04/22.04
14 # AlmaLinux 9/10
15 # Rocky Linux 8/9/10
16 #
17 # Packages you might want to install to use this command:
18 # debootstrap, rinse, xz-utils
19
20
21 # Define your local mirros here
22 MIRROR_DEBIAN=http://deb.debian.org/debian/
23 MIRROR_UBUNTU=http://mirror.netcologne.de/ubuntu/
24
25 EXCLUDE_SQUEEZE=isc-dhcp-client,isc-dhcp-common,info
26 EXCLUDE_WHEEZY=info
27 EXCLUDE_JESSIE=info
28 EXCLUDE_STRETCH=info
29 EXCLUDE_BUSTER=
30 EXCLUDE_BULLSEYE=
31 EXCLUDE_BOOKWORM=
32 EXCLUDE_TRIXIE=
33 EXCLUDE_FORKY=
34 EXCLUDE_SID=
35
36 EXCLUDE_TRUSTY=dhcp3-client,dhcp3-common,info
37 EXCLUDE_XENIAL=udhcpc,dibbler-client,info
38 EXCLUDE_BIONIC=udhcpc,dibbler-client,info
39 EXCLUDE_FOCAL=udhcpc,dibbler-client,info
40 EXCLUDE_JAMMY=udhcpc,dibbler-client,info
41 EXCLUDE_NOBLE=udhcpc,dibbler-client,info
42 EXCLUDE_RESOLUTE=udhcpc,dibbler-client,info
43
44 # here you can add packages, that are needed very early
45 INCLUDE_DEBIAN=
46
47
48 setarch() {
49
50 l32=
51 if [ X$1 = Xi386 ]; then
52 l32=linux32
53 fi
54 }
55
56 check() {
57
58 if [ $(id -u) != 0 ]; then
59 echo "You must be root to create chroots."
60 exit 1
61 fi
62 mknod $xtmp/test-dev-null c 1 3
63 if [ $? -eq 1 ]; then
64 echo "Cannot create device files on $xtmp, aborting."
65 echo "Perhaps this directory is mounted with option nodev."
66 rm -rf $xtmp
67 exit 1
68 fi
69 echo test > $xtmp/test-dev-null
70 if [ $? -eq 1 ]; then
71 echo "Cannot create device files on $xtmp, aborting."
72 echo "Perhaps this directory is mounted with option nodev."
73 rm -rf $xtmp
74 exit 1
75 fi
76 rm -f $xtmp/test-dev-null
77 }
78
79
80 cleanup-deb() {
81
82 chroot $xtmp apt-get clean
83 rm -f $xtmp/etc/hostname $xtmp/etc/resolv.conf \
84 $xtmp/var/lib/apt/lists/*_* $xtmp/usr/bin/qemu-*-static \
85 $xtmp/etc/udev/rules.d/70-persistent-net.rules \
86 $xtmp/var/lib/dbus/machine-id
87 > $xtmp/etc/machine-id
88 }
89
90
91 cleanup-rinse() {
92
93 # check if chroot works
94 echo "Installed packages in chroot:"
95 chroot $xtmp rpm -qa|sort
96 echo -n "CHROOT rpm -qa: "
97 chroot $xtmp rpm -qa|wc -l
98
99 rm -f $xtmp/etc/resolv.conf
100 if [ -d $xtmp/etc/yum.repos.d/orig ]; then
101 mv $xtmp/etc/yum.repos.d/orig/* $xtmp/etc/yum.repos.d/
102 rm -rf $xtmp/etc/yum.repos.d/orig
103 fi
104 }
105
106
107 tarit() {
108
109 tar $attributes --numeric-owner --one-file-system -C $xtmp -cf - . | $zip > $target.$ext
110 }
111
112
113 rpmdist() {
114
115 local arch=$1
116 local vers=$2
117 local dist=$3
118 local domain=$(domainname)
119
120 check
121 setarch $arch
122 $l32 rinse --directory $xtmp --distribution $dist-$vers --arch $arch
123 domainname $domain # workaround for #613377
124 cleanup-rinse
125 tarit
126 }
127
128
129 alma() {
130 rpmdist $1 $2 alma
131 }
132
133 rocky() {
134 rpmdist $1 $2 rocky
135 }
136
137 debgeneric() {
138
139 local DIST=$1
140 shift
141 local mirror=$1
142 shift
143 local arch=$1
144
145 dist=${DIST%%[0-9][0-9]}
146 local exc="EXCLUDE_$dist"
147 [ -n "${!exc}" ] && exc="--exclude=${!exc}" || unset exc
148 dist=${dist,,}
149
150 check
151 if [ -n "$INCLUDE_DEBIAN" ]; then
152 local inc="--include=$INCLUDE_DEBIAN"
153 fi
154
155 if [ -n "$arch" ]; then
156 debootstrap --arch $arch ${exc} $inc $dist $xtmp $mirror
157 target="${target}_${arch^^}"
158 else
159 if [[ $DIST =~ 64 ]]; then
160 arch=amd64
161 else
162 arch=i386
163 fi
164 debootstrap --arch $arch ${exc} $inc $dist $xtmp $mirror
165 fi
166 cleanup-deb
167 tarit
168 }
169
170 prtdists() {
171
172 echo "Available:
173
174 ALMA9_64
175 ALMA10_64
176 ROCKY8_64
177 ROCKY9_64
178 ROCKY10_64
179 TRUSTY32 TRUSTY64
180 XENIAL32 XENIAL64
181 BIONIC64
182 FOCAL64
183 JAMMY64
184 NOBLE64
185 RESOLUTE64
186 SQUEEZE32 SQUEEZE64
187 WHEEZY32 WHEEZY64
188 JESSIE32 JESSIE64
189 STRETCH32 STRETCH64
190 BUSTER32 BUSTER64
191 BULLSEYE32 BULLSEYE64
192 BOOKWORM32 BOOKWORM64
193 TRIXIE32 TRIXIE64
194 FORKY32 FORKY64
195 DUKE32 DUKE64
196 SID32 SID64
197 "
198 }
199
200 usage() {
201
202 cat <<EOF
203 mk-basefile, create minimal base files for a Linux distritubtion
204
205 Copyright (C) 2011-2026 by Thomas Lange
206
207 Usage: mk-basefile [OPTION] ... DISTRIBUTION
208
209 -s Show list of supported linux distributions
210 -f ARCH Build for foreign architecture ARCH.
211 -d DIR Use DIR for creating the temporary subtree structure.
212 -z Use zstd for compressing the tar file.
213 -J Use xz for compressing the tar file.
214 -k Keep the temporary subtree structure, do not remove it.
215 -h Print help.
216
217 Usage example: mk-basefile -J STRETCH64
218 This will create a STRETCH64.tar.xz basefile.
219
220 EOF
221 exit 0
222 }
223
224 # main routine
225
226 ext=tar
227 zip=cat
228 attributes=
229 cleanup=1
230 attributes="--xattrs --selinux --acls"
231
232 while getopts ashzJd:kf: opt ; do
233 case "$opt" in
234 a) echo "$0: Warning. -a is ignored, because xtattrs, acls and selinux are always added." ;;
235 d) export TMPDIR=$OPTARG ;;
236 f) export ARCH=$OPTARG ;;
237 z) zip="zstd -9"; ext=tar.zst ;;
238 J) zip="xz -8" ext=tar.xz ;;
239 k) cleanup=0 ;;
240 h) usage ;;
241 s) prtdists ; exit 0;;
242 ?) exit 3 ;; # error in option parsing
243 esac
244 done
245 shift $((OPTIND - 1))
246
247 xtmp=$(mktemp --tmpdir -d basefiles.XXXXXXXX)
248 if [ $? -eq 1 ]; then
249 echo "mktemp failed. Aborting."
250 exit 2
251 fi
252 chmod 755 $xtmp
253
254 target=$1 # also the name of the output file
255
256 [ -z "$target" ] && usage
257 case "$target" in
258 ROCKY8_64) rocky amd64 8 ;;
259 ROCKY9_64) rocky amd64 9 ;;
260 ROCKY10_64) rocky amd64 10 ;;
261 ALMA9_64) alma amd64 9 ;;
262 ALMA10_64) alma amd64 10 ;;
263 TRUSTY*|XENIAL*|BIONIC*|FOCAL*|JAMMY*|NOBLE*|RESOLUTE*)
264 debgeneric $target $MIRROR_UBUNTU ;;
265 SQUEEZE*|WHEEZY*|JESSIE*|STRETCH*|BUSTER*|BULLSEYE*|BOOKWORM*|TRIXIE*|FORKY*|DUKE*|SID*)
266 debgeneric $target $MIRROR_DEBIAN $ARCH;;
267 *) echo "Unknown distribution. Aborting."
268 prtdists
269 exit 99 ;;
270 esac
271
272 # cleanup
273 if [ $cleanup -eq 1 ]; then
274 rm -rf $xtmp
275 fi