[CRIU] [PATCH 3/9] mntns: pivot_root() can move the current root to a non-shared mount

Pavel Emelyanov xemul at parallels.com
Mon Oct 27 11:21:05 PDT 2014


On 10/23/2014 05:49 PM, Andrey Vagin wrote:
> So we need to create a temporary private mount for the old root.
> 
> Signed-off-by: Andrey Vagin <avagin at openvz.org>
> ---
>  mount.c | 27 +++++++++++++++++++++++----
>  1 file changed, 23 insertions(+), 4 deletions(-)
> 
> diff --git a/mount.c b/mount.c
> index 07f6db4..ac89baa 100644
> --- a/mount.c
> +++ b/mount.c
> @@ -1465,6 +1465,7 @@ static int clean_mnt_ns(struct mount_info *mntinfo_tree)
>  static int cr_pivot_root(char *root)
>  {
>  	char put_root[] = "crtools-put-root.XXXXXX";
> +	int exit_code = -1;
>  
>  	pr_info("Move the root to %s\n", root ? : ".");
>  
> @@ -1480,11 +1481,19 @@ static int cr_pivot_root(char *root)
>  		return -1;
>  	}
>  
> +	if (mount(put_root, put_root, NULL, MS_BIND, NULL)) {

I have a subtle feeling that we've discussed that already. Shouldn't the
MS_REC be here? Otherwise we loose any mountpoints below the --root one.

> +		pr_perror("Unable to mount tmpfs in %s", put_root);
> +		goto err_root;
> +	}
> +
> +	if (mount(NULL, put_root, NULL, MS_PRIVATE, NULL)) {
> +		pr_perror("Can't remount %s with MS_PRIVATE", put_root);
> +		goto err_tmpfs;
> +	}
> +
>  	if (pivot_root(".", put_root)) {
>  		pr_perror("pivot_root(., %s) failed", put_root);
> -		if (rmdir(put_root))
> -			pr_perror("Can't remove the directory %s", put_root);
> -		return -1;
> +		goto err_tmpfs;
>  	}
>  
>  	if (mount("none", put_root, "none", MS_REC|MS_PRIVATE, NULL)) {
> @@ -1492,16 +1501,26 @@ static int cr_pivot_root(char *root)
>  		return -1;
>  	}
>  
> +	exit_code = 0;
> +
>  	if (umount2(put_root, MNT_DETACH)) {
>  		pr_perror("Can't umount %s", put_root);
>  		return -1;
>  	}
> +
> +err_tmpfs:
> +	if (umount2(put_root, MNT_DETACH)) {
> +		pr_perror("Can't umount %s", put_root);
> +		return -1;
> +	}
> +
> +err_root:
>  	if (rmdir(put_root)) {
>  		pr_perror("Can't remove the directory %s", put_root);
>  		return -1;
>  	}
>  
> -	return 0;
> +	return exit_code;
>  }
>  
>  struct mount_info *mnt_entry_alloc()
> 



More information about the CRIU mailing list