Skip to content

Commit 395c884

Browse files
authored
Merge pull request #1827 from flatcar/kai/qemu-swtpm
qemu_template.sh: Add support for attaching a software TPM
2 parents ef267c4 + 5e7b4b6 commit 395c884

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

build_library/qemu_template.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SSH_KEYS=""
1717
CLOUD_CONFIG_FILE=""
1818
IGNITION_CONFIG_FILE=""
1919
CONFIG_IMAGE=""
20+
SWTPM_DIR=
2021
SAFE_ARGS=0
2122
USAGE="Usage: $0 [-a authorized_keys] [--] [qemu options...]
2223
Options:
@@ -26,6 +27,16 @@ Options:
2627
-c FILE Config drive as an iso or fat filesystem image.
2728
-a FILE SSH public keys for login access. [~/.ssh/id_{dsa,rsa}.pub]
2829
-p PORT The port on localhost to map to the VM's sshd. [2222]
30+
-I FILE Set a custom image file.
31+
-M MB Set VM memory in MBs.
32+
-T DIR Add a software TPM2 device through swtpm which stores secrets
33+
and the control socket to the given directory. This may need
34+
some configuration first with 'swtpm_setup --tpmstate DIR ...'
35+
(see https://github.com/stefanberger/swtpm/wiki/Certificates-created-by-swtpm_setup).
36+
-R FILE Set up pflash ro content, e.g., for UEFI (with -W).
37+
-W FILE Set up pflash rw content, e.g., for UEFI (with -R).
38+
-K FILE Set kernel for direct boot used to simulate a PXE boot (with -R).
39+
-R FILE Set initrd for direct boot used to simulate a PXE boot (with -K).
2940
-s Safe settings: single simple cpu and no KVM.
3041
-h this ;-)
3142
@@ -76,6 +87,27 @@ while [ $# -ge 1 ]; do
7687
-s|-safe)
7788
SAFE_ARGS=1
7889
shift ;;
90+
-I|-image-file)
91+
VM_IMAGE="$2"
92+
shift 2 ;;
93+
-M|-memory)
94+
VM_MEMORY="$2"
95+
shift 2 ;;
96+
-T|-tpm)
97+
SWTPM_DIR="$2"
98+
shift 2 ;;
99+
-R|-pflash-ro)
100+
VM_PFLASH_RO="$2"
101+
shift 2 ;;
102+
-W|-pflash-rw)
103+
VM_PFLASH_RW="$2"
104+
shift 2 ;;
105+
-K|-kernel-file)
106+
VM_KERNEL="$2"
107+
shift 2 ;;
108+
-R|-initrd-file)
109+
VM_INITRD="$2"
110+
shift 2 ;;
79111
-v|-verbose)
80112
set -x
81113
shift ;;
@@ -109,6 +141,29 @@ write_ssh_keys() {
109141
sed -e 's/^/ - /'
110142
}
111143

144+
if [ -n "${SWTPM_DIR}" ]; then
145+
mkdir -p "${SWTPM_DIR}"
146+
if ! command -v swtpm >/dev/null; then
147+
echo "$0: swtpm command not found!" >&2
148+
exit 1
149+
fi
150+
case "${VM_BOARD}" in
151+
amd64-usr)
152+
TPM_DEV=tpm-tis ;;
153+
arm64-usr)
154+
TPM_DEV=tpm-tis-device ;;
155+
*) die "Unsupported arch" ;;
156+
esac
157+
SWTPM_SOCK="${SWTPM_DIR}/socket"
158+
swtpm socket --tpmstate "dir=${SWTPM_DIR}" --ctrl "type=unixio,path=${SWTPM_SOCK},terminate" --tpm2 &
159+
SWTPM_PROC=$!
160+
PARENT=$$
161+
# The swtpm process exits if qemu disconnects but if we never started qemu because
162+
# this script fails or qemu failed to start, we need to kill the process.
163+
# The EXIT trap is already in use by the config drive cleanup and anyway doesn't work with kill -9.
164+
(while [ -e "/proc/${PARENT}" ]; do sleep 1; done; kill "${SWTPM_PROC}" 2>/dev/null; exit 0) &
165+
set -- -chardev "socket,id=chrtpm,path=${SWTPM_SOCK}" -tpmdev emulator,id=tpm0,chardev=chrtpm -device "${TPM_DEV}",tpmdev=tpm0 "$@"
166+
fi
112167

113168
if [ -z "${CONFIG_IMAGE}" ]; then
114169
CONFIG_DRIVE=$(mktemp -d)

build_library/vm_image_util.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ IMG_DEFAULT_CONF_FORMAT=
119119
IMG_DEFAULT_BUNDLE_FORMAT=
120120

121121
# Memory size to use in any config files
122-
IMG_DEFAULT_MEM=1024
122+
IMG_DEFAULT_MEM=2048
123123

124124
# Number of CPUs to use in any config files
125125
IMG_DEFAULT_CPUS=2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- The default VM memory was bumped to 2 GB in the Qemu script and for VMware OVFs

0 commit comments

Comments
 (0)