qemu_command_builder/
memory.rs

1use bon::Builder;
2
3use crate::to_command::ToCommand;
4
5pub enum MemoryUnit {
6    MegaBytes(usize),
7    GigaBytes(usize),
8}
9
10/// Sets guest startup RAM size to megs megabytes. Default is 128 MiB.
11/// Optionally, a suffix of "M" or "G" can be used to signify a value in
12/// megabytes or gigabytes respectively. Optional pair slots, maxmem
13/// could be used to set amount of hotpluggable memory slots and maximum
14/// amount of memory. Note that maxmem must be aligned to the page size.
15///
16/// For example, the following command-line sets the guest startup RAM
17/// size to 1GB, creates 3 slots to hotplug additional memory and sets
18/// the maximum memory the guest can reach to 4GB:
19///
20/// `-m 1G,slots=3,maxmem=4G`
21///
22/// If slots and maxmem are not specified, memory hotplug won't be
23/// enabled and the guest startup RAM will never increase.
24#[derive(Builder)]
25pub struct Memory {
26    mem: MemoryUnit,
27    slots: Option<usize>,
28    maxmem: Option<MemoryUnit>,
29}
30
31impl ToCommand for Memory {
32    fn to_command(&self) -> Vec<String> {
33        let mut cmd = vec![];
34
35        cmd.push("-m".to_string());
36
37        let mut arg = String::new();
38        match &self.mem {
39            MemoryUnit::MegaBytes(amount) => {
40                arg.push_str(format!("{}M", amount).as_str());
41            }
42            MemoryUnit::GigaBytes(amount) => {
43                arg.push_str(format!("{}G", amount).as_str());
44            }
45        }
46        if let Some(slots) = self.slots {
47            arg.push_str(format!(",slots={}", slots).as_str());
48        }
49        if let Some(maxmem) = &self.maxmem {
50            match maxmem {
51                MemoryUnit::MegaBytes(amount) => {
52                    arg.push_str(format!(",maxmem={}M", amount).as_str());
53                }
54                MemoryUnit::GigaBytes(amount) => {
55                    arg.push_str(format!(",maxmem={}G", amount).as_str());
56                }
57            }
58        }
59        cmd.push(arg);
60
61        cmd
62    }
63}