diff options
author | Daan De Meyer <daan.j.demeyer@gmail.com> | 2023-08-14 16:39:39 +0200 |
---|---|---|
committer | Daan De Meyer <daan.j.demeyer@gmail.com> | 2023-08-14 18:46:08 +0200 |
commit | c55a97f1fd879f08052729f901fb899c3d8dfd52 (patch) | |
tree | 053ea51c332dbf42996959dd21e2160571d3cba0 /src | |
parent | copy: Add support for creating subvolumes to copy_tree_at() (diff) | |
download | systemd-c55a97f1fd879f08052729f901fb899c3d8dfd52.tar.xz systemd-c55a97f1fd879f08052729f901fb899c3d8dfd52.zip |
mkdir: Add support for creating subvolumes to mkdir_p_root()
We pass in the paths which should be subvolumes and try to create
those as subvolumes if we can.
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/mkdir.c | 15 | ||||
-rw-r--r-- | src/basic/mkdir.h | 2 | ||||
-rw-r--r-- | src/partition/repart.c | 6 | ||||
-rw-r--r-- | src/shared/dissect-image.c | 2 | ||||
-rw-r--r-- | src/test/test-mkdir.c | 8 |
5 files changed, 19 insertions, 14 deletions
diff --git a/src/basic/mkdir.c b/src/basic/mkdir.c index 41af1482bc..c770e5ed32 100644 --- a/src/basic/mkdir.c +++ b/src/basic/mkdir.c @@ -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)) { diff --git a/src/basic/mkdir.h b/src/basic/mkdir.h index 117d9e8061..e5387483e2 100644 --- a/src/basic/mkdir.h +++ b/src/basic/mkdir.h @@ -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); diff --git a/src/partition/repart.c b/src/partition/repart.c index b73427c9df..0a335b2cc1 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -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); } diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index b4fbc77177..3b77706f8f 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -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; diff --git a/src/test/test-mkdir.c b/src/test/test-mkdir.c index 24933a3464..03e5a4997c 100644 --- a/src/test/test-mkdir.c +++ b/src/test/test-mkdir.c @@ -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); |