dragonball: use const string for legacy device type

As string "com1", "com2" and "rtc" are used in two files
(device_manager/mod.rs and device_manager/legacy.rs), we use public
const variables COM1, COM2 and RTC to replace them respectively.

Fixes: #4544

Signed-off-by: xuejun-xj <jiyunxue@alibaba.linux.com>
Signed-off-by: jingshan <jingshan@linux.alibaba.com>
This commit is contained in:
xuejun-xj 2022-07-11 17:46:10 +08:00
parent f6f96b8fee
commit 458f6f42f6
2 changed files with 15 additions and 7 deletions

View File

@ -156,18 +156,26 @@ pub(crate) mod aarch64 {
type Result<T> = ::std::result::Result<T, Error>;
/// LegacyDeviceType: com1
pub const COM1: &str = "com1";
/// LegacyDeviceType: com2
pub const COM2: &str = "com2";
/// LegacyDeviceType: rtc
pub const RTC: &str = "rtc";
impl LegacyDeviceManager {
/// Create a LegacyDeviceManager instance handling legacy devices.
pub fn create_manager(
bus: &mut IoManager,
vm_fd: Option<Arc<VmFd>>,
resources: &HashMap<String, DeviceResources>,
) -> Result<Self> {
let (com1_device, com1_eventfd) =
Self::create_com_device(bus, vm_fd.as_ref(), resources.get("com1").unwrap())?;
Self::create_com_device(bus, vm_fd.as_ref(), resources.get(COM1).unwrap())?;
let (com2_device, com2_eventfd) =
Self::create_com_device(bus, vm_fd.as_ref(), resources.get("com2").unwrap())?;
Self::create_com_device(bus, vm_fd.as_ref(), resources.get(COM2).unwrap())?;
let (rtc_device, rtc_eventfd) =
Self::create_rtc_device(bus, vm_fd.as_ref(), resources.get("rtc").unwrap())?;
Self::create_rtc_device(bus, vm_fd.as_ref(), resources.get(RTC).unwrap())?;
Ok(LegacyDeviceManager {
_rtc_device: rtc_device,

View File

@ -54,7 +54,7 @@ pub mod console_manager;
pub use self::console_manager::ConsoleManager;
mod legacy;
pub use self::legacy::{Error as LegacyDeviceError, LegacyDeviceManager};
pub use self::legacy::{Error as LegacyDeviceError, LegacyDeviceManager, aarch64::{COM1, COM2, RTC}};
#[cfg(feature = "virtio-vsock")]
/// Device manager for user-space vsock devices.
@ -744,9 +744,9 @@ impl DeviceManager {
) -> std::result::Result<HashMap<String, DeviceResources>, StartMicroVmError> {
let mut resources = HashMap::new();
let legacy_devices = vec![
(DeviceType::Serial, String::from("com1")),
(DeviceType::Serial, String::from("com2")),
(DeviceType::RTC, String::from("rtc")),
(DeviceType::Serial, String::from(COM1)),
(DeviceType::Serial, String::from(COM2)),
(DeviceType::RTC, String::from(RTC)),
];
for (device_type, device_id) in legacy_devices {