Skip to content

Commit

Permalink
Update start-virt.sh (#254)
Browse files Browse the repository at this point in the history
1. User can specify vCPU number and memory size
2. Add usage example in README.md

Signed-off-by: Hao, Ruomeng <[email protected]>
  • Loading branch information
ruomengh authored Jan 31, 2024
1 parent 844a38f commit fcd202d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 23 deletions.
54 changes: 37 additions & 17 deletions tools/cvm-image-rewriter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,43 @@ $ ./run.sh -i <initial guest image> -t 10
### 3.3 Boot a VM
After above tool is running successfully, you can boot a VM using the generated `output.qcow2` using `qemu-test.sh`.
```
$ sudo ./qemu-test.sh -h
Usage: qemu-test.sh [OPTION]...
Required
-i <guest image> Specify initial guest image file
```
For example:
```
# Boot a TD
$ sudo ./qemu-test.sh -i output.qcow2 -t td -p <qemu monitor port> -f <ssh_forward port>

# Boot a normal VM
$ sudo ./qemu-test.sh -i output.qcow2 -p <qemu monitor port> -f <ssh_forward port>
```
After above tool is running successfully, you can boot a VM using the generated `output.qcow2` using `qemu-test.sh` or `start-virt.sh`.
- Boot TD or normal VM using `qemu-test.sh`.
```
$ sudo ./qemu-test.sh -h
Usage: qemu-test.sh [OPTION]...
Required
-i <guest image> Specify initial guest image file
```
For example:
```
# Boot a TD
$ sudo ./qemu-test.sh -i output.qcow2 -t td -p <qemu monitor port> -f <ssh_forward port>

# Boot a normal VM
$ sudo ./qemu-test.sh -i output.qcow2 -p <qemu monitor port> -f <ssh_forward port>
```
- Boot TD using `start-virt.sh`.
```
$ sudo ./start-virt.sh -h
Usage: start-virt.sh [OPTION]...
-i <guest image file> Default is tdx-guest-ubuntu22.04.qcow2 under current directory
-n <guest name> Name of TD guest
-t <template file> Default is ./tdx-libvirt-ubuntu-host.xml.template
-f Force recreate
-v <vcpu number> VM vCPU number
-m <memory size in GB> VM memory size in GB
-h Show this help
```
For example:
```
# Boot a TD with specified name and CPU/memory
$ sudo ./qemu-test.sh -i output.qcow2 -n <libvirt domain name> -v <vCPU number> -m <memory size in GiB>
```
### 3.4 Run in Nested VM (Optional)
Expand Down
27 changes: 25 additions & 2 deletions tools/cvm-image-rewriter/start-virt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ GUEST_NAME="tdx-guest"
GUEST_ROOTDIR=/tmp/libvirt-vms
TEMPLATE="${CURR_DIR}/tdx-libvirt-ubuntu-host.xml.template"
FORCE=false
VCPU_NUM=1
MEM_SIZE=4

usage() {
cat << EOM
Expand All @@ -19,6 +21,8 @@ Usage: $(basename "$0") [OPTION]...
-n <guest name> Name of TD guest
-t <template file> Default is ./tdx-libvirt-ubuntu-host.xml.template
-f Force recreate
-v <vcpu number> VM vCPU number
-m <memory size in GB> VM memory size in GB
-h Show this help
EOM
}
Expand All @@ -32,11 +36,13 @@ pre-check() {
}

process_args() {
while getopts ":i:n:t:fh" option; do
while getopts ":i:n:t:v:m:fh" option; do
case "$option" in
i) GUEST_IMG=$OPTARG;;
n) GUEST_NAME=$OPTARG;;
t) TEMPLATE=$OPTARG;;
v) VCPU_NUM=$OPTARG;;
m) MEM_SIZE=$OPTARG;;
f) FORCE=true;;
h) usage
exit 0
Expand All @@ -51,6 +57,7 @@ process_args() {

echo "====================================================================="
echo " Use template : ${TEMPLATE}"
echo " Guest name : ${GUEST_NAME}"
echo " Guest XML : ${GUEST_ROOTDIR}/${GUEST_NAME}.xml"
echo " Guest Image : ${GUEST_ROOTDIR}/${GUEST_NAME}.qcow2"
echo " Force Recreate : ${FORCE}"
Expand Down Expand Up @@ -81,18 +88,34 @@ process_args() {
echo "Please specify via -t"
exit 1
fi

# Validate the number of vCPUs
if ! [[ ${VCPU_NUM} =~ ^[0-9]+$ && ${VCPU_NUM} -gt 0 ]]; then
echo "Error: Invalid number of vCPUs: ${VCPU_NUM}"
usage
exit 1
fi

# Validate the size of memory
if ! [[ ${MEM_SIZE} =~ ^[0-9]+$ && ${MEM_SIZE} -gt 0 ]]; then
echo "Error: Invalid memory size: ${MEM_SIZE}"
usage
exit 1
fi
}

create-vm() {
mkdir -p ${GUEST_ROOTDIR}/
echo "> Create ${GUEST_ROOTDIR}/${GUEST_NAME}.qcow2..."
cp "${GUEST_IMG}" "${GUEST_ROOTDIR}/${GUEST_NAME}.qcow2"
echo "> Create ${GUEST_ROOTDIR}/${GUEST_NAME}.xml..."
cp "${CURR_DIR}/${TEMPLATE}" "${GUEST_ROOTDIR}/${GUEST_NAME}.xml"
cp "${TEMPLATE}" "${GUEST_ROOTDIR}/${GUEST_NAME}.xml"

echo "> Modify configurations..."
sed -i "s/.*<name>.*/<name>${GUEST_NAME}<\/name>/" "${GUEST_ROOTDIR}/${GUEST_NAME}.xml"
sed -i "s#/path/to/image#${GUEST_ROOTDIR}/${GUEST_NAME}.qcow2#" "${GUEST_ROOTDIR}/${GUEST_NAME}.xml"
sed -i "s/REPLACE_VCPU_NUM/${VCPU_NUM}/g" "${GUEST_ROOTDIR}/${GUEST_NAME}.xml"
sed -i "s/REPLACE_MEM_SIZE/${MEM_SIZE}/g" "${GUEST_ROOTDIR}/${GUEST_NAME}.xml"
}

start-vm() {
Expand Down
8 changes: 4 additions & 4 deletions tools/cvm-image-rewriter/tdx-libvirt-ubuntu-host.xml.template
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
<name>td-guest-grub-on-ubuntu-host</name>
<memory unit='KiB'>2097152</memory>
<name>tdx-guest</name>
<memory unit='GiB'>REPLACE_MEM_SIZE</memory>
<memoryBacking>
<source type='memfd-private'/>
</memoryBacking>
<vcpu placement="static">1</vcpu>
<vcpu placement="static">REPLACE_VCPU_NUM</vcpu>
<os>
<type arch='x86_64' machine='q35'>hvm</type>
<loader>/usr/share/qemu/OVMF.fd</loader>
Expand All @@ -26,7 +26,7 @@
<suspend-to-disk enable='no'/>
</pm>
<cpu mode='host-passthrough'>
<topology sockets='1' cores='1' threads='1'/>
<topology sockets='1' cores='REPLACE_VCPU_NUM' threads='1'/>
</cpu>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
Expand Down

0 comments on commit fcd202d

Please sign in to comment.