]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mkdir: Add support for creating subvolumes to mkdir_p_root()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 14 Aug 2023 14:39:39 +0000 (16:39 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 14 Aug 2023 16:46:08 +0000 (18:46 +0200)
We pass in the paths which should be subvolumes and try to create
those as subvolumes if we can.

src/basic/mkdir.c
src/basic/mkdir.h
src/partition/repart.c
src/shared/dissect-image.c
src/test/test-mkdir.c

index 41af1482bc7bfbf0d139bcc4c913df485d6389ef..c770e5ed32eac0f3c99c89c71406d4a6c72e9ad6 100644 (file)
@@ -5,6 +5,7 @@
 #include <string.h>
 
 #include "alloc-util.h"
+#include "btrfs.h"
 #include "chase.h"
 #include "fd-util.h"
 #include "format-util.h"
@@ -209,7 +210,7 @@ int mkdir_p_safe(const char *prefix, const char *path, mode_t mode, uid_t uid, g
         return mkdir_p_internal(prefix, path, mode, uid, gid, flags, mkdirat_errno_wrapper);
 }
 
-int mkdir_p_root(const char *root, const char *p, uid_t uid, gid_t gid, mode_t m) {
+int mkdir_p_root(const char *root, const char *p, uid_t uid, gid_t gid, mode_t m, char **subvolumes) {
         _cleanup_free_ char *pp = NULL, *bn = NULL;
         _cleanup_close_ int dfd = -EBADF;
         int r;
@@ -227,7 +228,7 @@ int mkdir_p_root(const char *root, const char *p, uid_t uid, gid_t gid, mode_t m
                 return r;
         else {
                 /* Extracting the parent dir worked, hence we aren't top-level? Recurse up first. */
-                r = mkdir_p_root(root, pp, uid, gid, m);
+                r = mkdir_p_root(root, pp, uid, gid, m, subvolumes);
                 if (r < 0)
                         return r;
 
@@ -242,11 +243,15 @@ int mkdir_p_root(const char *root, const char *p, uid_t uid, gid_t gid, mode_t m
         if (r < 0)
                 return r;
 
-        if (mkdirat(dfd, bn, m) < 0) {
-                if (errno == EEXIST)
+        if (path_strv_contains(subvolumes, p))
+                r = btrfs_subvol_make_fallback(dfd, bn, m);
+        else
+                r = RET_NERRNO(mkdirat(dfd, bn, m));
+        if (r < 0) {
+                if (r == -EEXIST)
                         return 0;
 
-                return -errno;
+                return r;
         }
 
         if (uid_is_valid(uid) || gid_is_valid(gid)) {
index 117d9e80613362026a1551b9f7be8ae3bb424a4e..e5387483e23190c5f7ad9c88b58dfc5642e65b07 100644 (file)
@@ -23,7 +23,7 @@ static inline int mkdir_parents(const char *path, mode_t mode) {
 int mkdir_parents_safe(const char *prefix, const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags);
 int mkdir_p(const char *path, mode_t mode);
 int mkdir_p_safe(const char *prefix, const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags);
-int mkdir_p_root(const char *root, const char *p, uid_t uid, gid_t gid, mode_t m);
+int mkdir_p_root(const char *root, const char *p, uid_t uid, gid_t gid, mode_t m, char **subvolumes);
 
 /* The following are used to implement the mkdir_xyz_label() calls, don't use otherwise. */
 typedef int (*mkdirat_func_t)(int dir_fd, const char *pathname, mode_t mode);
index b73427c9df2690f2370e8ebdea10d9595400e6b0..0a335b2cc1306ef07725d38a14f31350340c562f 100644 (file)
@@ -4368,7 +4368,7 @@ static int do_copy_files(Context *context, Partition *p, const char *root) {
                                 if (r < 0)
                                         return log_error_errno(r, "Failed to extract directory from '%s': %m", *target);
 
-                                r = mkdir_p_root(root, dn, UID_INVALID, GID_INVALID, 0755);
+                                r = mkdir_p_root(root, dn, UID_INVALID, GID_INVALID, 0755, NULL);
                                 if (r < 0)
                                         return log_error_errno(r, "Failed to create parent directory '%s': %m", dn);
 
@@ -4408,7 +4408,7 @@ static int do_copy_files(Context *context, Partition *p, const char *root) {
                         if (r < 0)
                                 return log_error_errno(r, "Failed to extract directory from '%s': %m", *target);
 
-                        r = mkdir_p_root(root, dn, UID_INVALID, GID_INVALID, 0755);
+                        r = mkdir_p_root(root, dn, UID_INVALID, GID_INVALID, 0755, NULL);
                         if (r < 0)
                                 return log_error_errno(r, "Failed to create parent directory: %m");
 
@@ -4441,7 +4441,7 @@ static int do_make_directories(Partition *p, const char *root) {
 
         STRV_FOREACH(d, p->make_directories) {
 
-                r = mkdir_p_root(root, *d, UID_INVALID, GID_INVALID, 0755);
+                r = mkdir_p_root(root, *d, UID_INVALID, GID_INVALID, 0755, NULL);
                 if (r < 0)
                         return log_error_errno(r, "Failed to create directory '%s' in file system: %m", *d);
         }
index b4fbc771773457ce0b180877b02170c679a57c42..3b77706f8fb2b6993199ae781bc97ecb7a8c35b4 100644 (file)
@@ -1936,7 +1936,7 @@ static int mount_partition(
 
         if (directory) {
                 /* Automatically create missing mount points inside the image, if necessary. */
-                r = mkdir_p_root(where, directory, uid_shift, (gid_t) uid_shift, 0755);
+                r = mkdir_p_root(where, directory, uid_shift, (gid_t) uid_shift, 0755, NULL);
                 if (r < 0 && r != -EROFS)
                         return r;
 
index 24933a3464aa2685601375f4c6b0017559d1a104..03e5a4997c81be545701bbe7128d8d8dd19d6e5b 100644 (file)
@@ -96,7 +96,7 @@ TEST(mkdir_p_root) {
         assert_se(mkdtemp_malloc("/tmp/test-mkdir-XXXXXX", &tmp) >= 0);
 
         assert_se(p = path_join(tmp, "run/aaa/bbb"));
-        assert_se(mkdir_p_root(tmp, "/run/aaa/bbb", UID_INVALID, GID_INVALID, 0755) >= 0);
+        assert_se(mkdir_p_root(tmp, "/run/aaa/bbb", UID_INVALID, GID_INVALID, 0755, NULL) >= 0);
         assert_se(is_dir(p, false) > 0);
         assert_se(is_dir(p, true) > 0);
 
@@ -109,18 +109,18 @@ TEST(mkdir_p_root) {
 
         p = mfree(p);
         assert_se(p = path_join(tmp, "var/run/hoge/foo/baz"));
-        assert_se(mkdir_p_root(tmp, "/var/run/hoge/foo/baz", UID_INVALID, GID_INVALID, 0755) >= 0);
+        assert_se(mkdir_p_root(tmp, "/var/run/hoge/foo/baz", UID_INVALID, GID_INVALID, 0755, NULL) >= 0);
         assert_se(is_dir(p, false) > 0);
         assert_se(is_dir(p, true) > 0);
 
         p = mfree(p);
         assert_se(p = path_join(tmp, "not-exists"));
-        assert_se(mkdir_p_root(p, "/aaa", UID_INVALID, GID_INVALID, 0755) == -ENOENT);
+        assert_se(mkdir_p_root(p, "/aaa", UID_INVALID, GID_INVALID, 0755, NULL) == -ENOENT);
 
         p = mfree(p);
         assert_se(p = path_join(tmp, "regular-file"));
         assert_se(touch(p) >= 0);
-        assert_se(mkdir_p_root(p, "/aaa", UID_INVALID, GID_INVALID, 0755) == -ENOTDIR);
+        assert_se(mkdir_p_root(p, "/aaa", UID_INVALID, GID_INVALID, 0755, NULL) == -ENOTDIR);
 
         /* FIXME: The tests below do not work.
         p = mfree(p);