]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core,nspawn: unify code that moves the root dir
authorLennart Poettering <lennart@poettering.net>
Tue, 19 May 2015 18:32:44 +0000 (20:32 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 20 May 2015 12:38:12 +0000 (14:38 +0200)
src/core/namespace.c
src/nspawn/nspawn.c
src/shared/util.c
src/shared/util.h

index 5b782798c89ff3b0bf1264c9b947f59469d1c5b1..c0c64fd7069039af8d42869e364672276578b9df 100644 (file)
@@ -125,22 +125,6 @@ static void drop_duplicates(BindMount *m, unsigned *n) {
         *n = t - m;
 }
 
-static int mount_move_root(const char *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;
-}
-
 static int mount_dev(BindMount *m) {
         static const char devnodes[] =
                 "/dev/null\0"
index a38f47dd0a1a2c5e1d887e1843909cbea3063386..1f919c082bc922d9ecd92492277a691e3b14707a 100644 (file)
@@ -4391,23 +4391,9 @@ int main(int argc, char *argv[]) {
                         if (mount_cgroup(arg_directory) < 0)
                                 _exit(EXIT_FAILURE);
 
-                        if (chdir(arg_directory) < 0) {
-                                log_error_errno(errno, "chdir(%s) failed: %m", arg_directory);
-                                _exit(EXIT_FAILURE);
-                        }
-
-                        if (mount(arg_directory, "/", NULL, MS_MOVE, NULL) < 0) {
-                                log_error_errno(errno, "mount(MS_MOVE) failed: %m");
-                                _exit(EXIT_FAILURE);
-                        }
-
-                        if (chroot(".") < 0) {
-                                log_error_errno(errno, "chroot() failed: %m");
-                                _exit(EXIT_FAILURE);
-                        }
-
-                        if (chdir("/") < 0) {
-                                log_error_errno(errno, "chdir() failed: %m");
+                        r = mount_move_root(arg_directory);
+                        if (r < 0) {
+                                log_error_errno(r, "Failed to move root directory: %m");
                                 _exit(EXIT_FAILURE);
                         }
 
index e18645f8f174e1b435e0856634c77d0f3c500941..c3b08bbc431851cef47ea31a84d9c65be52b6bbb 100644 (file)
@@ -6229,3 +6229,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;
+}
index 0e806cf1a101a8812756e0f5cb4df1faa0f35e34..f0382f0d68a6dd3d48e011d0dc993554df9c7239 100644 (file)
@@ -906,3 +906,5 @@ int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char
 char *shell_maybe_quote(const char *s);
 
 int parse_mode(const char *s, mode_t *ret);
+
+int mount_move_root(const char *path);