chiark / gitweb /
core,nspawn: unify code that moves the root dir
[elogind.git] / src / shared / util.c
index ceb88d54c25399eb7330313a75a231a377aba4ac..5efb9591a60f671ff2b2e1b67948ae05a464202d 100644 (file)
@@ -6144,3 +6144,21 @@ int parse_mode(const char *s, mode_t *ret) {
         *ret = (mode_t) l;
         return 0;
 }
+
+int mount_move_root(const char *path) {
+        assert(path);
+
+        if (chdir(path) < 0)
+                return -errno;
+
+        if (mount(path, "/", NULL, MS_MOVE, NULL) < 0)
+                return -errno;
+
+        if (chroot(".") < 0)
+                return -errno;
+
+        if (chdir("/") < 0)
+                return -errno;
+
+        return 0;
+}