memory: Allow memory to be backed by a file

In the context of vhost-user, we need the guest RAM to be backed by
a file in order to be accessed by an external process. This patch
adds the new flag "file=" to the "--memory" option so that we can
specify from the command line if the memory needs to be backed, and
by which specific file.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf
2019-05-24 12:21:23 -07:00
committed by Samuel Ortiz
parent 2ede30b6d3
commit 53085c7ccc
3 changed files with 73 additions and 37 deletions

View File

@@ -843,24 +843,29 @@ impl<'a> Vm<'a> {
let fd = Arc::new(fd);
// Init guest memory
let arch_mem_regions = arch::arch_memory_regions(u64::from(&config.memory) << 20);
let arch_mem_regions = arch::arch_memory_regions(config.memory.size << 20);
let mut mem_regions = Vec::<(GuestAddress, usize, Option<FileOffset>)>::new();
for region in arch_mem_regions.iter() {
let file = OpenOptions::new()
.read(true)
.write(true)
.custom_flags(O_TMPFILE)
.open("/dev/shm")
.map_err(Error::SharedFileCreate)?;
let guest_memory = match config.memory.file {
Some(file) => {
let mut mem_regions = Vec::<(GuestAddress, usize, Option<FileOffset>)>::new();
for region in arch_mem_regions.iter() {
let file = OpenOptions::new()
.read(true)
.write(true)
.custom_flags(O_TMPFILE)
.open(file)
.map_err(Error::SharedFileCreate)?;
file.set_len(region.1 as u64)
.map_err(Error::SharedFileSetLen)?;
file.set_len(region.1 as u64)
.map_err(Error::SharedFileSetLen)?;
mem_regions.push((region.0, region.1, Some(FileOffset::new(file, 0))));
}
mem_regions.push((region.0, region.1, Some(FileOffset::new(file, 0))));
}
let guest_memory = GuestMemoryMmap::with_files(&mem_regions).map_err(Error::GuestMemory)?;
GuestMemoryMmap::with_files(&mem_regions).map_err(Error::GuestMemory)?
}
None => GuestMemoryMmap::new(&arch_mem_regions).map_err(Error::GuestMemory)?,
};
guest_memory
.with_regions(|index, region| {