Skip to content

Commit 572f276

Browse files
slpalyssarosenzweig
authored andcommitted
Ensure we leave at least 3072MiB free to the host
Leaving less than 3072MiB for the host can compromise the system's stability, so ensure we do that unless instructed by the user. This mainly applies to 8GB systems. Signed-off-by: Sergio Lopez <[email protected]>
1 parent d661705 commit 572f276

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

crates/krun/src/bin/krun.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,27 @@ fn main() -> Result<()> {
124124
let sysinfo = sysinfo().context("Failed to get system information")?;
125125
let ram_total_mib = (sysinfo.ram_total() / (1024 * 1024)) as u32;
126126

127-
// By default, set the microVM RAM to be 80% of the system's RAM.
128-
let ram_mib = options
129-
.mem
130-
.unwrap_or(MiB::from((ram_total_mib as f64 * 0.8) as u32));
127+
let ram_mib = if let Some(ram_mib) = options.mem {
128+
ram_mib
129+
} else {
130+
// By default, set the microVM RAM to be 80% of the system's RAM.
131+
let guest_ram = (ram_total_mib as f64 * 0.8) as u32;
132+
// Ensure we leave 3072 MiB free for the host + VRAM to avoid
133+
// compromising the host's stability. This mainly applies to systems
134+
// with 8GB of RAM.
135+
let guest_ram = if ram_total_mib
136+
.checked_sub(guest_ram)
137+
.context("Failed to calculate the amount of free RAM")?
138+
< 3072
139+
{
140+
ram_total_mib
141+
.checked_sub(3072)
142+
.context("Systems with less than 3072 MiB of RAM are not supported")?
143+
} else {
144+
guest_ram
145+
};
146+
MiB::from(guest_ram)
147+
};
131148
// VRAM is actually the SHM window for virtio-gpu. The userspace drivers in the
132149
// guest are expected to be conservative and tell applications that the amount
133150
// of VRAM is just a percentage of the guest's RAM, so we can afford being more

0 commit comments

Comments
 (0)