1
0
mirror of https://github.com/containers/youki synced 2024-11-23 17:32:15 +01:00

adding more mount_to_container case

This commit is contained in:
tommady 2021-10-06 15:53:34 +00:00
parent ac4e93759f
commit c80b1c2c58
No known key found for this signature in database
GPG Key ID: 15E4E2B7957CF963
2 changed files with 88 additions and 36 deletions

@ -401,7 +401,8 @@ impl RootFS {
.as_ref()
.with_context(|| "no source in mount spec".to_string())?;
let src = if typ == Some("bind") {
let src = canonicalize(source)?;
let src = canonicalize(source)
.with_context(|| format!("Failed to canonicalize: {:?}", source))?;
let dir = if src.is_file() {
Path::new(&dest).parent().unwrap()
} else {
@ -416,7 +417,7 @@ impl RootFS {
.create(true)
.write(true)
.open(&dest)
.unwrap();
.with_context(|| format!("Failed to create file for bind mount: {:?}", src))?;
}
src
@ -439,7 +440,7 @@ impl RootFS {
.with_context(|| format!("Failed to mount {:?} to {:?}", src, dest))?;
}
if flags.contains(MsFlags::MS_BIND)
if typ == Some("bind")
&& flags.intersects(
!(MsFlags::MS_REC
| MsFlags::MS_REMOUNT

@ -452,39 +452,90 @@ fn test_create_devices() {
#[test]
fn test_mount_to_container() {
let tmp_dir = TempDir::new("/tmp/test_mount_to_container").unwrap();
let rootfs = RootFS::new();
let mount = &MountBuilder::default()
.destination(PathBuf::from("/dev/pts"))
.typ("devpts")
.source(PathBuf::from("devpts"))
.options(vec![
"nosuid".to_string(),
"noexec".to_string(),
"newinstance".to_string(),
"ptmxmode=0666".to_string(),
"mode=0620".to_string(),
"gid=5".to_string(),
])
.build()
.unwrap();
let (flags, data) = parse_mount(mount);
{
let rootfs = RootFS::new();
let mount = &MountBuilder::default()
.destination(PathBuf::from("/dev/pts"))
.typ("devpts")
.source(PathBuf::from("devpts"))
.options(vec![
"nosuid".to_string(),
"noexec".to_string(),
"newinstance".to_string(),
"ptmxmode=0666".to_string(),
"mode=0620".to_string(),
"gid=5".to_string(),
])
.build()
.unwrap();
let (flags, data) = parse_mount(mount);
assert!(rootfs
.mount_to_container(mount, tmp_dir.path(), flags, &data, None)
.is_ok());
assert!(rootfs
.mount_to_container(mount, tmp_dir.path(), flags, &data, Some("defaults"))
.is_ok());
let want = MountArgs {
source: Some(PathBuf::from("devpts")),
target: tmp_dir.path().join("dev/pts"),
fstype: Some("devpts".to_string()),
flags: MsFlags::MS_NOSUID | MsFlags::MS_NOEXEC,
data: Some("newinstance,ptmxmode=0666,mode=0620,gid=5".to_string()),
};
let got = &rootfs
.syscall
.as_any()
.downcast_ref::<TestHelperSyscall>()
.unwrap()
.get_mount_args()[0];
assert_eq!(want, *got);
let want = vec![MountArgs {
source: Some(PathBuf::from("devpts")),
target: tmp_dir.path().join("dev/pts"),
fstype: Some("devpts".to_string()),
flags: MsFlags::MS_NOSUID | MsFlags::MS_NOEXEC,
data: Some(
"newinstance,ptmxmode=0666,mode=0620,gid=5,context=\"defaults\"".to_string(),
),
}];
let got = &rootfs
.syscall
.as_any()
.downcast_ref::<TestHelperSyscall>()
.unwrap()
.get_mount_args();
assert_eq!(want, *got);
assert_eq!(got.len(), 1);
}
{
let rootfs = RootFS::new();
let mount = &MountBuilder::default()
.destination(PathBuf::from("/dev/null"))
.typ("bind")
.source(tmp_dir.path().join("null"))
.options(vec!["ro".to_string()])
.build()
.unwrap();
let (flags, data) = parse_mount(mount);
OpenOptions::new()
.create(true)
.write(true)
.open(tmp_dir.path().join("null"))
.unwrap();
assert!(rootfs
.mount_to_container(mount, tmp_dir.path(), flags, &data, None)
.is_ok());
let want = vec![
MountArgs {
source: Some(tmp_dir.path().join("null")),
target: tmp_dir.path().join("dev/null"),
fstype: Some("bind".to_string()),
flags: MsFlags::MS_RDONLY,
data: Some("".to_string()),
},
// remount one
MountArgs {
source: Some(tmp_dir.path().join("dev/null")),
target: tmp_dir.path().join("dev/null"),
fstype: None,
flags: MsFlags::MS_RDONLY | MsFlags::MS_REMOUNT,
data: None,
},
];
let got = &rootfs
.syscall
.as_any()
.downcast_ref::<TestHelperSyscall>()
.unwrap()
.get_mount_args();
assert_eq!(want, *got);
assert_eq!(got.len(), 2);
}
}