diff --git a/docs/fields.md b/docs/fields.md index f831d70..0d3eeba 100644 --- a/docs/fields.md +++ b/docs/fields.md @@ -165,6 +165,9 @@ Requirements - disable_emulation: Disable sound card emulation, set this option to true if the sound card is passthrough in `[passthrough]` +### [usb] +- usb_tablet: set `true` to enable usb tablet, by default usb tablet is disabled. + ### [extra] - cmd: Set extra command. - service: Set extra services, use `:` split different services. diff --git a/src/guest/config_parser.cc b/src/guest/config_parser.cc index 80b266e..6eab6b1 100644 --- a/src/guest/config_parser.cc +++ b/src/guest/config_parser.cc @@ -44,6 +44,7 @@ map> kConfigMap = { { kGroupAudio, { kDisableEmul } }, { kGroupMed, { kMedBattery, kMedThermal, kMedCamera } }, { kGroupService, { kServTimeKeep, kServPmCtrl, kServVinput } }, + { kGroupUsb, { kUsbTablet } }, { kGroupExtra, { kExtraCmd, kExtraService, kExtraPwrCtrlMultiOS } } }; diff --git a/src/guest/config_parser.h b/src/guest/config_parser.h index 947bc42..6669eb8 100644 --- a/src/guest/config_parser.h +++ b/src/guest/config_parser.h @@ -31,6 +31,7 @@ constexpr char kGroupPciPt[] = "passthrough"; constexpr char kGroupAudio[] = "audio"; constexpr char kGroupMed[] = "mediation"; constexpr char kGroupService[] = "guest_control"; +constexpr char kGroupUsb[] = "usb"; constexpr char kGroupExtra[] = "extra"; /* Keys */ @@ -88,6 +89,8 @@ constexpr char kServTimeKeep[] = "time_keep"; constexpr char kServPmCtrl[] = "pm_control"; constexpr char kServVinput[] = "vinput"; +constexpr char kUsbTablet[] = "usb_tablet"; + constexpr char kExtraCmd[] = "cmd"; constexpr char kExtraService[] = "service"; constexpr char kExtraPwrCtrlMultiOS[] = "pwr_ctrl_multios"; diff --git a/src/guest/vm_builder_qemu.cc b/src/guest/vm_builder_qemu.cc index 89cadcb..5fa565e 100644 --- a/src/guest/vm_builder_qemu.cc +++ b/src/guest/vm_builder_qemu.cc @@ -246,7 +246,7 @@ static int SetAvailableVf(void) { execQuantumPath.append(std::to_string(i) + kGtExecQuantumMs); WriteSysFile(execQuantumPath.c_str(), std::to_string(kExecQuantum)); return i; - } + } } LOG(error) << "Failed to find 1 available VF!"; @@ -285,9 +285,9 @@ void VmBuilderQemu::BuildExtraGuestPmCtrlCmd(void) { std::string ex_cmd = cfg_.GetValue(kGroupExtra, kExtraPwrCtrlMultiOS); if (ex_cmd == "true") { static std::string socketPath; - for(int avail = 0; avail < MAX_NUM_GUEST; avail++) { + for (int avail = 0; avail < MAX_NUM_GUEST; avail++) { socketPath = kQmpPowerSocket + std::to_string(avail); - if(access(socketPath.c_str(), F_OK) != 0) { + if (access(socketPath.c_str(), F_OK) != 0) { std::string qmpCmd = " -qmp unix:" + socketPath + ",server,nowait"; emul_cmd_.append(qmpCmd); break; @@ -297,8 +297,9 @@ void VmBuilderQemu::BuildExtraGuestPmCtrlCmd(void) { end_call_.emplace([](){ boost::filesystem::remove(socketPath); }); - } else + } else { return; + } } enum PciPassthroughAction { @@ -485,7 +486,7 @@ static int GetUid(void) { void VmBuilderQemu::BuildAudioCmd(void) { int uid = GetUid(); - if (cfg_.GetValue(kGroupAudio, kDisableEmul).compare("true") == 0) + if (cfg_.GetValue(kGroupAudio, kDisableEmul).compare("true") == 0) return; emul_cmd_.append(" -device intel-hda" @@ -494,6 +495,15 @@ void VmBuilderQemu::BuildAudioCmd(void) { "in.fixed-settings=off,out.fixed-settings=off,server=/run/user/1000/pulse/native"); } +void VmBuilderQemu::BuildUsbCmd(void) { + emul_cmd_.append(" -device qemu-xhci,id=xhci,p2=8,p3=8 -device usb-kbd"); + if (cfg_.GetValue(kGroupUsb, kUsbTablet).compare("true") != 0) { + emul_cmd_.append(" -device usb-mouse"); + } else { + emul_cmd_.append(" -device usb-tablet"); + } +} + void VmBuilderQemu::BuildExtraCmd(void) { std::string ex_cmd = cfg_.GetValue(kGroupExtra, kExtraCmd); if (ex_cmd.empty()) @@ -543,10 +553,7 @@ void VmBuilderQemu::BuildFixedCmd(void) { " -machine kernel_irqchip=on" " -k en-us" " -cpu host,-waitpkg,pmu=off" - " -enable-kvm" - " -device qemu-xhci,id=xhci,p2=8,p3=8" - " -device usb-mouse" - " -device usb-kbd"); + " -enable-kvm"); emul_cmd_.append( /* Make sure this device be the last argument */ " -device intel-iommu,device-iotlb=on,caching-mode=on" @@ -630,7 +637,8 @@ void VmBuilderQemu::BuildRpmbCmd(void) { emul_cmd_.append(" -device virtio-serial,addr=1" " -device virtserialport,chardev=rpmb0,name=rpmb0,nr=1" " -chardev socket,id=rpmb0,path=" + rpmb_sock); - co_procs_.emplace_back(std::make_unique(std::move(rpmb_bin), std::move(rpmb_data), std::move(rpmb_sock))); + co_procs_.emplace_back(std::make_unique(std::move(rpmb_bin), std::move(rpmb_data), + std::move(rpmb_sock))); } } @@ -643,10 +651,9 @@ void VmBuilderQemu::BuildVtpmCmd(void) { " -tpmdev emulator,id=tpm0,chardev=chrtpm -device tpm-crb,tpmdev=tpm0"); co_procs_.emplace_back(std::make_unique(std::move(vtpm_bin), std::move(vtpm_data))); } - } -void VmBuilderQemu::InitAafCfg(void){ +void VmBuilderQemu::InitAafCfg(void) { std::string aaf_path = cfg_.GetValue(kGroupAaf, kAafPath); if (!aaf_path.empty()) { aaf_cfg_ = std::make_unique(aaf_path.c_str()); @@ -705,7 +712,8 @@ bool VmBuilderQemu::BuildVgpuCmd(void) { return false; } emul_cmd_.append(" -vga none -nographic" - " -device vfio-pci,host=00:02.0,x-igd-gms=2,id=hostdev0,bus=pcie.0,addr=0x2,x-igd-opregion=on -display none"); + " -device vfio-pci,host=00:02.0,x-igd-gms=2,id=hostdev0,bus=pcie.0,addr=0x2,x-igd-opregion=on" + " -display none"); if (aaf_cfg_) aaf_cfg_->Set(kAafKeyGpuType, "gvtd"); } else if (vgpu_type.compare(kVgpuVirtio) == 0) { @@ -748,7 +756,7 @@ void VmBuilderQemu::BuildVinputCmd(void) { LOG(warning) << "vinput-manager not found"; return; } - + if (vgpu_type.compare(kVgpuGvtD) == 0) { vinput.append(" --gvtd"); emul_cmd_.append( @@ -822,7 +830,7 @@ bool VmBuilderQemu::BuildVmArgs(void) { if (!BuildVgpuCmd()) return false; - + BuildVinputCmd(); if (!BuildAafCfg()) @@ -856,6 +864,8 @@ bool VmBuilderQemu::BuildVmArgs(void) { BuildAudioCmd(); + BuildUsbCmd(); + BuildExtraCmd(); BuildFixedCmd(); diff --git a/src/guest/vm_builder_qemu.h b/src/guest/vm_builder_qemu.h index 6eb6ffe..364de16 100644 --- a/src/guest/vm_builder_qemu.h +++ b/src/guest/vm_builder_qemu.h @@ -62,6 +62,7 @@ class VmBuilderQemu : public VmBuilder { void BuildGuestPmCtrlCmd(void); void BuildExtraGuestPmCtrlCmd(void); void BuildAudioCmd(void); + void BuildUsbCmd(void); void BuildExtraCmd(void); void SoundCardHook(void);