@@ -666,74 +666,96 @@ def test_cgroupsv2_written_only_once(uvm_plain, cgroups_info):
666
666
assert len (mkdir_lines ) == 1
667
667
668
668
669
- def test_mount_proagation_to_root (uvm_plain , tmp_path , guest_kernel , rootfs_rw ):
669
+ def test_jail_mount (uvm_plain , guest_kernel , rootfs_rw ):
670
670
"""
671
671
Test that the jailer mounts are propagated to the root mount namespace.
672
-
673
- This is a test for
674
- https://github.com/firecracker-microvm/firecracker/pull/#1093
675
672
"""
676
-
673
+ # setup the microvm
677
674
test_microvm = uvm_plain
678
675
676
+ chroot_base = test_microvm .jailer .chroot_base / str (test_microvm .id )[:8 ]
679
677
# make a directory to hold the original content
680
- original_content_dir = tmp_path / "original "
681
- original_content_dir .mkdir (parents = True )
678
+ original_content_dir = chroot_base / "original_content "
679
+ original_content_dir .mkdir (parents = True , exist_ok = True )
682
680
683
681
# make a directory to hold the jailed content
684
- jailed_content_dir = tmp_path / "firecracker" / "testbindmount" / "root"
685
- jailed_content_dir .mkdir (parents = True )
686
-
687
- test_microvm .jailer .jailer_id = "testbindmount"
688
- test_microvm .jailer .chroot_base = tmp_path
689
- test_microvm .jailer .daemonize = True
690
- test_microvm .jailer .gid = 0
691
- test_microvm .jailer .uid = 0
692
- test_microvm .extra_args = {"seccomp-level" : 0 }
682
+ jailed_content_dir = chroot_base / "firecracker" / "testbindmount" / "root"
683
+ jailed_content_dir .mkdir (parents = True , exist_ok = True )
684
+ jailed_content_dir_ssh = jailed_content_dir / ".ssh"
685
+ jailed_content_dir_ssh .mkdir (parents = True , exist_ok = True )
693
686
694
687
# assert that the directory was created
688
+ assert original_content_dir .exists ()
695
689
assert jailed_content_dir .exists ()
690
+ assert jailed_content_dir_ssh .exists ()
696
691
697
- # Create the guest kernel and rootfs in the jailed content directory
698
- # and mount them in the jailed content directory
699
- os .system (f"cp { guest_kernel } { original_content_dir } " )
700
- os .system (f"cp { rootfs_rw } { original_content_dir } " )
701
- guest_kernel_mount_path = jailed_content_dir / os .path .basename (guest_kernel )
702
- rootfs_mount_path = jailed_content_dir / os .path .basename (rootfs_rw )
703
- guest_kernel_mount_path .touch ()
704
- rootfs_mount_path .touch ()
692
+ # add the ssh key to the jailed content dir so we can ssh into the microvm
693
+ pub_key_contents = Path ("/srv/img/x86_64/id_rsa.pub" ).read_text ()
694
+ ssh_key = jailed_content_dir_ssh / "authorized_keys"
695
+ ssh_key .write_text (pub_key_contents )
705
696
706
- # assert that the files were created
707
- assert guest_kernel_mount_path .exists ()
708
- assert rootfs_mount_path .exists ()
697
+ # create the files that will be mounted
698
+ test_data = original_content_dir / "test_data"
699
+ test_data .touch ()
700
+ assert test_data .exists ()
701
+ test_data .write_text ("test_data" )
702
+ assert test_data .read_text () == "test_data"
709
703
710
- # mount the rootfs
704
+ os .system (f"cp { guest_kernel } { original_content_dir } " )
705
+ os .system (f"cp { rootfs_rw } { original_content_dir } " )
706
+ assert (original_content_dir / guest_kernel .name ).exists ()
707
+ assert (original_content_dir / rootfs_rw .name ).exists ()
708
+
709
+ jailed_test_data = jailed_content_dir / "test_data"
710
+ jailed_test_data .touch ()
711
+ assert jailed_test_data .exists ()
712
+ assert jailed_test_data .read_text () == ""
713
+ jailed_kernel = jailed_content_dir / guest_kernel .name
714
+ jailed_rootfs = jailed_content_dir / rootfs_rw .name
715
+ jailed_kernel .touch ()
716
+ jailed_rootfs .touch ()
717
+ assert (jailed_content_dir / guest_kernel .name ).exists ()
718
+ assert (jailed_content_dir / rootfs_rw .name ).exists ()
719
+
720
+ pid_file = jailed_content_dir / "firecracker.pid"
721
+ pid_file .touch ()
722
+
723
+ # mount the data
724
+ subprocess .run (
725
+ ["mount" , "--bind" , test_data , jailed_test_data ],
726
+ check = True ,
727
+ )
711
728
subprocess .run (
712
729
[
713
730
"mount" ,
714
731
"--bind" ,
715
- original_content_dir / os . path . basename ( guest_kernel ) ,
716
- guest_kernel_mount_path ,
732
+ original_content_dir / guest_kernel . name ,
733
+ jailed_content_dir / guest_kernel . name ,
717
734
],
718
735
check = True ,
719
736
)
720
737
subprocess .run (
721
738
[
722
739
"mount" ,
723
740
"--bind" ,
724
- original_content_dir / os . path . basename ( rootfs_rw ) ,
725
- rootfs_mount_path ,
741
+ original_content_dir / rootfs_rw . name ,
742
+ jailed_content_dir / rootfs_rw . name ,
726
743
],
727
744
check = True ,
728
745
)
729
746
730
- # assert that the mounts are present
731
- assert guest_kernel_mount_path .exists ()
732
- assert rootfs_mount_path .exists ()
733
-
734
- # run
747
+ # spawn the microvm
735
748
test_microvm .spawn ()
749
+ test_microvm .basic_config ()
750
+ # set params for the microvm
751
+ test_microvm .kernel_file = str (jailed_content_dir / guest_kernel .name )
752
+ test_microvm .rootfs_file = str (jailed_content_dir / rootfs_rw .name )
753
+ test_microvm .jailer .chroot_base = chroot_base
754
+ test_microvm .jailer .jailer_id = "testbindmount"
755
+ test_microvm .add_net_iface ()
756
+ test_microvm .start ()
736
757
737
- # assert that the mounts are present
738
- assert guest_kernel_mount_path .exists ()
739
- assert rootfs_mount_path .exists ()
758
+ # and assert the content is there in the microvm
759
+ _ , stdout , stderr = test_microvm .ssh .run ("cat /root/test_data" )
760
+ assert stdout == "test_data"
761
+ assert stderr == ""
0 commit comments