mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2025-01-22 04:25:21 +00:00
virtio-balloon: Store the balloon size to support reboot
This commit store balloon size to MemoryConfig. After reboot, virtio-balloon can use this size to inflate back to the size before reboot. Signed-off-by: Hui Zhu <teawater@antfin.com>
This commit is contained in:
parent
50da100afd
commit
800220acbb
@ -503,6 +503,7 @@ mod unit_tests {
|
|||||||
shared: false,
|
shared: false,
|
||||||
hugepages: false,
|
hugepages: false,
|
||||||
balloon: false,
|
balloon: false,
|
||||||
|
balloon_size: 0,
|
||||||
},
|
},
|
||||||
kernel: Some(KernelConfig {
|
kernel: Some(KernelConfig {
|
||||||
path: PathBuf::from("/path/to/kernel"),
|
path: PathBuf::from("/path/to/kernel"),
|
||||||
|
@ -410,10 +410,11 @@ pub struct Balloon {
|
|||||||
|
|
||||||
impl Balloon {
|
impl Balloon {
|
||||||
// Create a new virtio-balloon.
|
// Create a new virtio-balloon.
|
||||||
pub fn new(id: String) -> io::Result<Self> {
|
pub fn new(id: String, size: u64) -> io::Result<Self> {
|
||||||
let avail_features = 1u64 << VIRTIO_F_VERSION_1;
|
let avail_features = 1u64 << VIRTIO_F_VERSION_1;
|
||||||
|
|
||||||
let config = VirtioBalloonConfig::default();
|
let mut config = VirtioBalloonConfig::default();
|
||||||
|
config.num_pages = (size >> PAGE_SHIFT) as u32;
|
||||||
|
|
||||||
Ok(Balloon {
|
Ok(Balloon {
|
||||||
id,
|
id,
|
||||||
|
@ -340,6 +340,8 @@ pub struct MemoryConfig {
|
|||||||
pub hugepages: bool,
|
pub hugepages: bool,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub balloon: bool,
|
pub balloon: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub balloon_size: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MemoryConfig {
|
impl MemoryConfig {
|
||||||
@ -400,6 +402,7 @@ impl MemoryConfig {
|
|||||||
shared,
|
shared,
|
||||||
hugepages,
|
hugepages,
|
||||||
balloon,
|
balloon,
|
||||||
|
balloon_size: 0,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -415,6 +418,7 @@ impl Default for MemoryConfig {
|
|||||||
shared: false,
|
shared: false,
|
||||||
hugepages: false,
|
hugepages: false,
|
||||||
balloon: false,
|
balloon: false,
|
||||||
|
balloon_size: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1873,6 +1877,7 @@ mod tests {
|
|||||||
shared: false,
|
shared: false,
|
||||||
hugepages: false,
|
hugepages: false,
|
||||||
balloon: false,
|
balloon: false,
|
||||||
|
balloon_size: 0,
|
||||||
},
|
},
|
||||||
kernel: Some(KernelConfig {
|
kernel: Some(KernelConfig {
|
||||||
path: PathBuf::from("/path/to/kernel"),
|
path: PathBuf::from("/path/to/kernel"),
|
||||||
|
@ -2309,8 +2309,11 @@ impl DeviceManager {
|
|||||||
let id = String::from(BALLOON_DEVICE_NAME);
|
let id = String::from(BALLOON_DEVICE_NAME);
|
||||||
|
|
||||||
let virtio_balloon_device = Arc::new(Mutex::new(
|
let virtio_balloon_device = Arc::new(Mutex::new(
|
||||||
virtio_devices::Balloon::new(id.clone())
|
virtio_devices::Balloon::new(
|
||||||
.map_err(DeviceManagerError::CreateVirtioBalloon)?,
|
id.clone(),
|
||||||
|
self.config.lock().unwrap().memory.balloon_size,
|
||||||
|
)
|
||||||
|
.map_err(DeviceManagerError::CreateVirtioBalloon)?,
|
||||||
));
|
));
|
||||||
|
|
||||||
self.memory_manager
|
self.memory_manager
|
||||||
|
@ -799,13 +799,12 @@ impl MemoryManager {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn balloon_resize(&mut self, expected_ram: u64) -> Result<(), Error> {
|
pub fn balloon_resize(&mut self, expected_ram: u64) -> Result<u64, Error> {
|
||||||
|
let mut balloon_size = 0;
|
||||||
if let Some(balloon) = &self.balloon {
|
if let Some(balloon) = &self.balloon {
|
||||||
let balloon_size = if expected_ram < self.current_ram {
|
if expected_ram < self.current_ram {
|
||||||
self.current_ram - expected_ram
|
balloon_size = self.current_ram - expected_ram;
|
||||||
} else {
|
}
|
||||||
0
|
|
||||||
};
|
|
||||||
balloon
|
balloon
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -813,7 +812,7 @@ impl MemoryManager {
|
|||||||
.map_err(Error::VirtioBalloonResizeFail)?;
|
.map_err(Error::VirtioBalloonResizeFail)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(balloon_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// In case this function resulted in adding a new memory region to the
|
/// In case this function resulted in adding a new memory region to the
|
||||||
|
@ -738,7 +738,10 @@ impl Vm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Some(desired_ram_w_balloon) = desired_ram_w_balloon {
|
if let Some(desired_ram_w_balloon) = desired_ram_w_balloon {
|
||||||
self.memory_manager
|
// update the configuration value for the balloon size to ensure
|
||||||
|
// a reboot would use the right value.
|
||||||
|
self.config.lock().unwrap().memory.balloon_size = self
|
||||||
|
.memory_manager
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.balloon_resize(desired_ram_w_balloon)
|
.balloon_resize(desired_ram_w_balloon)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user