Skip to content

Commit

Permalink
feat: Verify clocksource is set to TSC (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
kroese authored Jun 9, 2024
1 parent 5e12e36 commit ba28a84
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
30 changes: 17 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

</div></h1>

QEMU in a docker container for running x86 and x64 virtual machines.
Docker container for running virtual machines using QEMU.

It uses high-performance QEMU options (like KVM acceleration, kernel-mode networking, IO threading, etc.) to achieve near-native speed.
It allows you to create VM's which behave just like normal containers, meaning you can manage them using all your existing tools (like Portainer) and configure them in a language (YAML) you are already familiar with.

This greatly reduces the learning curve and also eliminates the need for a dedicated Proxmox or ESXi server in many cases. It uses high-performance QEMU options (like KVM acceleration, kernel-mode networking, IO threading, etc.) to achieve near-native speed.

## Features

Expand Down Expand Up @@ -124,17 +126,6 @@ kubectl apply -f kubernetes.yml

If it still fails to boot, you can set the value to `ide` to emulate a IDE drive, which is slow but requires no drivers and is compatible with almost every system.

* ### How do I verify if my system supports KVM?

To verify if your system supports KVM, run the following commands:

```bash
sudo apt install cpu-checker
sudo kvm-ok
```

If you receive an error from `kvm-ok` indicating that KVM acceleration can't be used, check the virtualization settings in the BIOS.

* ### How do I change the amount of CPU or RAM?

By default, the container will be allowed to use a maximum of 1 CPU core and 1 GB of RAM.
Expand All @@ -147,6 +138,19 @@ kubectl apply -f kubernetes.yml
CPU_CORES: "4"
```

* ### How do I verify if my system supports KVM?

To verify that your system supports KVM, run the following commands:

```bash
sudo apt install cpu-checker
sudo kvm-ok
```

If you receive an error from `kvm-ok` indicating that KVM acceleration can't be used, check whether the virtualization extensions (`Intel VT-x` or `AMD SVM`) are enabled in your BIOS. If you are running the container inside a VM instead of directly on the host, you will also need to enable nested virtualization in its settings. If you are using a cloud provider, you may be out of luck as most of them do not allow nested virtualization for their VPS's. If you are using Windows 10 or MacOS, you are also out of luck, as only Linux and Windows 11 support KVM.

If you don't receive any error from `kvm-ok` at all, but the container still complains that `/dev/kvm` is missing, it might help to add `privileged: true` to your compose file (or `--privileged` to your `run` command), to rule out any permission issue.

* ### How do I assign an individual IP address to the container?

By default, the container uses bridge networking, which shares the IP address with the host.
Expand Down
39 changes: 33 additions & 6 deletions src/disk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ createDevice () {
;;
"ide" )
result+=",if=none \
-device ide-hd,drive=${DISK_ID},bus=ide.$DISK_INDEX,rotation_rate=$DISK_ROTATION${index}"
-device ich9-ahci,id=ahci${DISK_INDEX},addr=$DISK_ADDRESS,iothread=io2 \
-device ide-hd,drive=${DISK_ID},bus=ahci$DISK_INDEX.0,rotation_rate=$DISK_ROTATION${index}"
echo "$result"
;;
"blk" | "virtio-blk" )
Expand Down Expand Up @@ -403,7 +404,7 @@ addMedia () {
local index=""
local DISK_ID="cdrom$DISK_BUS"
[ -n "$DISK_INDEX" ] && index=",bootindex=$DISK_INDEX"
local result="-drive file=$DISK_FILE,id=$DISK_ID,format=raw,readonly=on,media=cdrom"
local result="-drive file=$DISK_FILE,id=$DISK_ID,format=raw,cache=unsafe,readonly=on,media=cdrom"

case "${DISK_TYPE,,}" in
"auto" )
Expand All @@ -416,7 +417,8 @@ addMedia () {
;;
"ide" )
result+=",if=none \
-device ide-cd,drive=${DISK_ID},bus=ide.${DISK_BUS}${index}"
-device ich9-ahci,id=ahci${DISK_BUS},addr=$DISK_ADDRESS,iothread=io2 \
-device ide-cd,drive=${DISK_ID},bus=ahci${DISK_BUS}.0${index}"
echo "$result"
;;
"blk" | "virtio-blk" )
Expand Down Expand Up @@ -529,10 +531,30 @@ html "Initializing disks..."

case "${DISK_TYPE,,}" in
"ide" | "usb" | "scsi" | "blk" | "auto" ) ;;
* ) error "Invalid DISK_TYPE, value \"$DISK_TYPE\" is unrecognized!" && exit 80 ;;
* ) error "Invalid DISK_TYPE specified, value \"$DISK_TYPE\" is unrecognized!" && exit 80 ;;
esac

[[ "${MACHINE,,}" != "virt" ]] && MEDIA_TYPE="ide" || MEDIA_TYPE="auto"
if [ -z "${MEDIA_TYPE:-}" ]; then
case "${DISK_TYPE,,}" in
"ide" | "usb" | "scsi" )
if [[ "${MACHINE,,}" == "virt" ]]; then
MEDIA_TYPE="auto"
else
MEDIA_TYPE="$DISK_TYPE"
fi ;;
"blk" | "auto" )
if [[ "${MACHINE,,}" != "virt" ]] && [[ "${MACHINE,,}" != "pc-i440fx-2"* ]]; then
MEDIA_TYPE="ide"
else
MEDIA_TYPE="auto"
fi ;;
esac
fi

case "${MEDIA_TYPE,,}" in
"ide" | "usb" | "scsi" | "blk" | "auto" ) ;;
* ) error "Invalid MEDIA_TYPE specified, value \"$MEDIA_TYPE\" is unrecognized!" && exit 80 ;;
esac

if [ -f "$BOOT" ] && [ -s "$BOOT" ]; then
ADD_OPTS=$(addMedia "$BOOT" "$MEDIA_TYPE" "0" "$BOOT_INDEX" "0x5")
Expand All @@ -543,7 +565,12 @@ DRIVERS="/drivers.iso"
[ ! -f "$DRIVERS" ] || [ ! -s "$DRIVERS" ] && DRIVERS="$STORAGE/drivers.iso"

if [ -f "$DRIVERS" ] && [ -s "$DRIVERS" ]; then
ADD_OPTS=$(addMedia "$DRIVERS" "$MEDIA_TYPE" "1" "" "0x6")
if [[ "${MACHINE,,}" != "virt" ]] && [[ "${MACHINE,,}" != "pc-i440fx-2"* ]]; then
DRIVER_TYPE="ide"
else
DRIVER_TYPE="auto"
fi
ADD_OPTS=$(addMedia "$DRIVERS" "$DRIVER_TYPE" "1" "" "0x6")
DISK_OPTS+=" $ADD_OPTS"
fi

Expand Down
3 changes: 0 additions & 3 deletions src/network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,10 @@ configureDNS() {
DNSMASQ_OPTS+=" --address=/host.lan/${VM_NET_IP%.*}.1"

DNSMASQ_OPTS=$(echo "$DNSMASQ_OPTS" | sed 's/\t/ /g' | tr -s ' ' | sed 's/^ *//')
[[ "$DEBUG" == [Yy1]* ]] && set -x

if ! $DNSMASQ ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS}; then
error "Failed to start dnsmasq, reason: $?" && exit 29
fi
{ set +x; } 2>/dev/null
[[ "$DEBUG" == [Yy1]* ]] && echo

return 0
}
Expand Down
14 changes: 12 additions & 2 deletions src/proc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ if [[ "$KVM" != [Nn]* ]]; then
warn "you are using Windows 10 which has no KVM support, this will cause a major loss of performance."
else
error "KVM acceleration not available $KVM_ERR, this will cause a major loss of performance."
error "See the FAQ on how to enable it, or continue without KVM by setting KVM=N (not recommended)."
error "See the FAQ on how to diagnose the cause, or continue without KVM by setting KVM=N (not recommended)."
[[ "$DEBUG" != [Yy1]* ]] && exit 88
fi
fi
Expand All @@ -51,6 +51,7 @@ fi
if [[ "$KVM" != [Nn]* ]]; then

CPU_FEATURES="kvm=on,l3-cache=on,+hypervisor"
CLOCK="/sys/devices/system/clocksource/clocksource0/current_clocksource"
KVM_OPTS=",accel=kvm -enable-kvm -global kvm-pit.lost_tick_policy=discard"

if [ -z "$CPU_MODEL" ]; then
Expand All @@ -64,6 +65,15 @@ if [[ "$KVM" != [Nn]* ]]; then
fi
fi

if [ -f "$CLOCK" ]; then
CLOCK=$(<"$CLOCK")
if [[ "${CLOCK,,}" != "tsc" ]]; then
warn "unexpected clocksource: $CLOCK"
fi
else
warn "file \"$CLOCK\" cannot not found?"
fi

if grep -qw "svm" <<< "$flags"; then

# AMD processor
Expand All @@ -83,7 +93,7 @@ if [[ "$KVM" != [Nn]* ]]; then
fi

fi

if [[ "$HV" != [Nn]* ]] && [[ "${BOOT_MODE,,}" == "windows"* ]]; then

HV_FEATURES="hv_passthrough"
Expand Down

0 comments on commit ba28a84

Please sign in to comment.