blob: 3791953e7536ba1959ac0012a111f10d678000e8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
#!/bin/sh
# Copyright 2022 Helmut Grohne <helmut@subdivi.de>
# SPDX-License-Identifier: MIT
# shellcheck disable=SC2016 # Intentional quoting technique
set -u
ARCHITECTURE=$(dpkg --print-architecture)
IMAGE=rootfs.ext2
INCLUDE_PACKAGES=init
MIRROR="http://deb.debian.org/debian"
SIZE=$((1024*1024*1024))
SSHKEY=
SUITE=unstable
VMNAME=testvm
die() {
echo "$*" 1>&2
exit 1
}
usage() {
die "usage: $0 [-a architecture] [-h hostname] [-k sshkey] [-m mirror] [-o output] [-p packages] [-r release] [-s size_in_GB] [-- mmdebstrap options]"
}
while test "$#" -gt 0; do
case "$1" in
-a)
test "$#" -eq 1 && usage
ARCHITECTURE=$2
shift 2
;;
-h)
test "$#" -eq 1 && usage
VMNAME=$2
shift 2
;;
-k)
test "$#" -eq 1 && usage
SSHKEY=$2
shift 2
;;
-m)
test "$#" -eq 1 && usage
MIRROR=$2
shift 2
;;
-o)
test "$#" -eq 1 && usage
IMAGE=$2
shift 2
;;
-p)
test "$#" -eq 1 && usage
INCLUDE_PACKAGES="$INCLUDE_PACKAGES,$2"
shift 2
;;
-r)
test "$#" -eq 1 && usage
SUITE=$2
shift 2
;;
-s)
test "$#" -eq 1 && usage
SIZE=$(($2*1024*1024*1024))
shift 2
;;
--)
shift
break
;;
*)
usage
;;
esac
done
if test -n "$SSHKEY" && ! test -f "$SSHKEY"; then
die "error: ssh keyfile '$SSHKEY' not found"
fi
KERNEL_SUFFIX=-$ARCHITECTURE
case "$ARCHITECTURE" in
amd64|arm64)
KERNEL_SUFFIX="-cloud-$ARCHITECTURE"
if test "$SUITE" = jessie || test "$SUITE" = stretch; then
KERNEL_SUFFIX="-$ARCHITECTURE"
fi
;;
armhf)
KERNEL_SUFFIX=-armmp
;;
i386)
KERNEL_SUFFIX=-686-pae
;;
mips64el)
KERNEL_SUFFIX=-mips64r2el
;;
mipsel)
KERNEL_SUFFIX=-mips32r2el
;;
ppc64el)
KERNEL_SUFFIX=-powerpc64le
;;
esac
INCLUDE_PACKAGES="$INCLUDE_PACKAGES,linux-image$KERNEL_SUFFIX"
if test -n "$SSHKEY"; then
INCLUDE_PACKAGES="$INCLUDE_PACKAGES,openssh-server"
fi
# construct mmdebstrap options as $@:
set -- \
--verbose \
--variant=apt \
--format=ext2 \
"--architecture=$ARCHITECTURE" \
"--include=$INCLUDE_PACKAGES" \
"$@"
# unless we set up a fstab, / will be read-only
set -- "--customize-hook=echo 'LABEL=debvm / ext4 defaults 0 1' >"'"$1/etc/fstab"' "$@"
# set up a hostname
set -- \
"--customize-hook=echo $VMNAME >"'"$1/etc/hostname"' \
"--customize-hook=echo 127.0.0.1 localhost $VMNAME >"'"$1/etc/hosts"' \
"$@"
# allow password-less root login
set -- '--customize-hook=chroot "$1" passwd --delete root' "$@"
# dhcp on all network interfaces
set -- \
'--customize-hook=chroot "$1" systemctl enable systemd-networkd.service' \
"--customize-hook=printf '"'[Match]\nName=en*\nName=eth*\n[Network]\nDHCP=yes\n'"'"' > "$1/etc/systemd/network/20-wired.network"' \
"$@"
# add ssh key for root
if test -n "$SSHKEY"; then
set -- \
'--customize-hook=mkdir -p "$1/root/.ssh"' \
"--customize-hook=upload $SSHKEY /root/.ssh/authorized_keys" \
"$@"
fi
set -- --skip=cleanup/apt "$@"
case "$SUITE" in
jessie)
# Use obsolete and expired keys.
set -- '--keyring=/usr/share/keyrings/debian-archive-removed-keys.gpg' "$@"
set -- --aptopt='Apt::Key::gpgvcommand "/usr/libexec/mmdebstrap/gpgvnoexpkeysig"' "$@"
# chfn does not work, because libpam-runtime.postinst is late setting up /etc/pam.d/common-auth et al, see #1026765
set -- --extract-hook='chroot "$1" pam-auth-update --package --force' "$@"
;;
buster)
# We need /var/lib/dpkg/available for dpkg --set-selections to work.
set -- '--customize-hook=cat "$1"/var/lib/apt/lists/*_Packages | chroot "$1" dpkg --update-avail' "$@"
;;
stretch|bullseye|stable)
;;
*)
# Avoid the usrmerge package
set -- --hook-dir=/usr/share/mmdebstrap/hooks/merged-usr "$@"
;;
esac
# suite target mirror
set -- "$@" "$SUITE" "$IMAGE" "deb $MIRROR $SUITE main"
set -ex
mmdebstrap "$@"
truncate -s "$SIZE" "$IMAGE"
/sbin/resize2fs "$IMAGE"
/sbin/tune2fs -L debvm -i 0 -O extents,uninit_bg,dir_index,has_journal "$IMAGE"
|