@@ -17,6 +17,7 @@ SSH_KEYS=""
17
17
CLOUD_CONFIG_FILE=" "
18
18
IGNITION_CONFIG_FILE=" "
19
19
CONFIG_IMAGE=" "
20
+ SWTPM_DIR=
20
21
SAFE_ARGS=0
21
22
USAGE=" Usage: $0 [-a authorized_keys] [--] [qemu options...]
22
23
Options:
@@ -26,6 +27,16 @@ Options:
26
27
-c FILE Config drive as an iso or fat filesystem image.
27
28
-a FILE SSH public keys for login access. [~/.ssh/id_{dsa,rsa}.pub]
28
29
-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).
29
40
-s Safe settings: single simple cpu and no KVM.
30
41
-h this ;-)
31
42
@@ -76,6 +87,27 @@ while [ $# -ge 1 ]; do
76
87
-s|-safe)
77
88
SAFE_ARGS=1
78
89
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 ;;
79
111
-v|-verbose)
80
112
set -x
81
113
shift ;;
@@ -109,6 +141,29 @@ write_ssh_keys() {
109
141
sed -e ' s/^/ - /'
110
142
}
111
143
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
112
167
113
168
if [ -z " ${CONFIG_IMAGE} " ]; then
114
169
CONFIG_DRIVE=$( mktemp -d)
0 commit comments