1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-24 21:34:08 +03:00

Merge pull request #21093 from poettering/homework-trivial-tweaks

homed trivial refactoring
This commit is contained in:
Lennart Poettering 2021-10-22 19:12:01 +02:00 committed by GitHub
commit 7afef7ed14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 27 deletions

View File

@ -28,7 +28,7 @@ int home_setup_cifs(
char **pw; char **pw;
int r; int r;
r = home_unshare_and_mount(NULL, NULL, false, user_record_mount_flags(h)); r = home_unshare_and_mkdir();
if (r < 0) if (r < 0)
return r; return r;
@ -68,7 +68,7 @@ int home_setup_cifs(
if (r == 0) { if (r == 0) {
/* Child */ /* Child */
execl("/bin/mount", "/bin/mount", "-n", "-t", "cifs", execl("/bin/mount", "/bin/mount", "-n", "-t", "cifs",
h->cifs_service, "/run/systemd/user-home-mount", h->cifs_service, HOME_RUNTIME_WORK_DIR,
"-o", options, NULL); "-o", options, NULL);
log_error_errno(errno, "Failed to execute mount: %m"); log_error_errno(errno, "Failed to execute mount: %m");
@ -89,7 +89,7 @@ int home_setup_cifs(
return log_error_errno(SYNTHETIC_ERRNO(ENOKEY), return log_error_errno(SYNTHETIC_ERRNO(ENOKEY),
"Failed to mount home directory with supplied password."); "Failed to mount home directory with supplied password.");
setup->root_fd = open("/run/systemd/user-home-mount", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW); setup->root_fd = open(HOME_RUNTIME_WORK_DIR, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
} }
if (setup->root_fd < 0) if (setup->root_fd < 0)
return log_error_errno(errno, "Failed to open home directory: %m"); return log_error_errno(errno, "Failed to open home directory: %m");

View File

@ -1260,7 +1260,7 @@ int home_setup_luks(
ip = force_image_path ?: user_record_image_path(h); ip = force_image_path ?: user_record_image_path(h);
subdir = path_join("/run/systemd/user-home-mount/", user_record_user_name_and_realm(h)); subdir = path_join(HOME_RUNTIME_WORK_DIR, user_record_user_name_and_realm(h));
if (!subdir) if (!subdir)
return log_oom(); return log_oom();
@ -1374,7 +1374,7 @@ int home_setup_luks(
fail: fail:
if (mounted) if (mounted)
(void) umount_verbose(LOG_ERR, "/run/systemd/user-home-mount", UMOUNT_NOFOLLOW); (void) umount_verbose(LOG_ERR, HOME_RUNTIME_WORK_DIR, UMOUNT_NOFOLLOW);
if (dm_activated) if (dm_activated)
(void) sym_crypt_deactivate_by_name(cd, setup->dm_name, 0); (void) sym_crypt_deactivate_by_name(cd, setup->dm_name, 0);
@ -2242,7 +2242,7 @@ int home_create_luks(
mounted = true; mounted = true;
subdir = path_join("/run/systemd/user-home-mount/", user_record_user_name_and_realm(h)); subdir = path_join(HOME_RUNTIME_WORK_DIR, user_record_user_name_and_realm(h));
if (!subdir) { if (!subdir) {
r = log_oom(); r = log_oom();
goto fail; goto fail;
@ -2302,7 +2302,7 @@ int home_create_luks(
root_fd = safe_close(root_fd); root_fd = safe_close(root_fd);
r = umount_verbose(LOG_ERR, "/run/systemd/user-home-mount", UMOUNT_NOFOLLOW); r = umount_verbose(LOG_ERR, HOME_RUNTIME_WORK_DIR, UMOUNT_NOFOLLOW);
if (r < 0) if (r < 0)
goto fail; goto fail;
@ -2372,7 +2372,7 @@ fail:
root_fd = safe_close(root_fd); root_fd = safe_close(root_fd);
if (mounted) if (mounted)
(void) umount_verbose(LOG_WARNING, "/run/systemd/user-home-mount", UMOUNT_NOFOLLOW); (void) umount_verbose(LOG_WARNING, HOME_RUNTIME_WORK_DIR, UMOUNT_NOFOLLOW);
if (dm_activated) if (dm_activated)
(void) sym_crypt_deactivate_by_name(cd, dm_name, 0); (void) sym_crypt_deactivate_by_name(cd, dm_name, 0);
@ -2474,7 +2474,7 @@ static int ext4_offline_resize_fs(HomeSetup *setup, uint64_t new_size, bool disc
} }
if (setup->undo_mount) { if (setup->undo_mount) {
r = umount_verbose(LOG_ERR, "/run/systemd/user-home-mount", UMOUNT_NOFOLLOW); r = umount_verbose(LOG_ERR, HOME_RUNTIME_WORK_DIR, UMOUNT_NOFOLLOW);
if (r < 0) if (r < 0)
return r; return r;
@ -2542,7 +2542,7 @@ static int ext4_offline_resize_fs(HomeSetup *setup, uint64_t new_size, bool disc
} }
if (re_open) { if (re_open) {
setup->root_fd = open("/run/systemd/user-home-mount", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW); setup->root_fd = open(HOME_RUNTIME_WORK_DIR, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
if (setup->root_fd < 0) if (setup->root_fd < 0)
return log_error_errno(errno, "Failed to reopen file system: %m"); return log_error_errno(errno, "Failed to reopen file system: %m");
} }

View File

@ -5,6 +5,7 @@
#include "alloc-util.h" #include "alloc-util.h"
#include "homework-mount.h" #include "homework-mount.h"
#include "homework.h"
#include "mkdir.h" #include "mkdir.h"
#include "mount-util.h" #include "mount-util.h"
#include "path-util.h" #include "path-util.h"
@ -25,6 +26,9 @@ int home_mount_node(const char *node, const char *fstype, bool discard, unsigned
const char *options, *discard_option; const char *options, *discard_option;
int r; int r;
assert(node);
assert(fstype);
options = mount_options_for_fstype(fstype); options = mount_options_for_fstype(fstype);
discard_option = discard ? "discard" : "nodiscard"; discard_option = discard ? "discard" : "nodiscard";
@ -38,7 +42,7 @@ int home_mount_node(const char *node, const char *fstype, bool discard, unsigned
} else } else
options = discard_option; options = discard_option;
r = mount_nofollow_verbose(LOG_ERR, node, "/run/systemd/user-home-mount", fstype, flags|MS_RELATIME, strempty(options)); r = mount_nofollow_verbose(LOG_ERR, node, HOME_RUNTIME_WORK_DIR, fstype, flags|MS_RELATIME, strempty(options));
if (r < 0) if (r < 0)
return r; return r;
@ -46,24 +50,35 @@ int home_mount_node(const char *node, const char *fstype, bool discard, unsigned
return 0; return 0;
} }
int home_unshare_and_mount(const char *node, const char *fstype, bool discard, unsigned long flags) { int home_unshare_and_mkdir(void) {
int r; int r;
if (unshare(CLONE_NEWNS) < 0) if (unshare(CLONE_NEWNS) < 0)
return log_error_errno(errno, "Couldn't unshare file system namespace: %m"); return log_error_errno(errno, "Couldn't unshare file system namespace: %m");
assert(path_startswith(HOME_RUNTIME_WORK_DIR, "/run"));
r = mount_nofollow_verbose(LOG_ERR, "/run", "/run", NULL, MS_SLAVE|MS_REC, NULL); /* Mark /run as MS_SLAVE in our new namespace */ r = mount_nofollow_verbose(LOG_ERR, "/run", "/run", NULL, MS_SLAVE|MS_REC, NULL); /* Mark /run as MS_SLAVE in our new namespace */
if (r < 0) if (r < 0)
return r; return r;
(void) mkdir_p("/run/systemd/user-home-mount", 0700); (void) mkdir_p(HOME_RUNTIME_WORK_DIR, 0700);
if (node)
return home_mount_node(node, fstype, discard, flags);
return 0; return 0;
} }
int home_unshare_and_mount(const char *node, const char *fstype, bool discard, unsigned long flags) {
int r;
assert(node);
assert(fstype);
r = home_unshare_and_mkdir();
if (r < 0)
return r;
return home_mount_node(node, fstype, discard, flags);
}
int home_move_mount(const char *user_name_and_realm, const char *target) { int home_move_mount(const char *user_name_and_realm, const char *target) {
_cleanup_free_ char *subdir = NULL; _cleanup_free_ char *subdir = NULL;
const char *d; const char *d;
@ -74,13 +89,13 @@ int home_move_mount(const char *user_name_and_realm, const char *target) {
/* If user_name_and_realm is set, then we'll mount a subdir of the source mount into the host. If /* If user_name_and_realm is set, then we'll mount a subdir of the source mount into the host. If
* it's NULL we'll move the mount itself */ * it's NULL we'll move the mount itself */
if (user_name_and_realm) { if (user_name_and_realm) {
subdir = path_join("/run/systemd/user-home-mount/", user_name_and_realm); subdir = path_join(HOME_RUNTIME_WORK_DIR, user_name_and_realm);
if (!subdir) if (!subdir)
return log_oom(); return log_oom();
d = subdir; d = subdir;
} else } else
d = "/run/systemd/user-home-mount/"; d = HOME_RUNTIME_WORK_DIR;
(void) mkdir_p(target, 0700); (void) mkdir_p(target, 0700);
@ -88,7 +103,7 @@ int home_move_mount(const char *user_name_and_realm, const char *target) {
if (r < 0) if (r < 0)
return r; return r;
r = umount_verbose(LOG_ERR, "/run/systemd/user-home-mount", UMOUNT_NOFOLLOW); r = umount_verbose(LOG_ERR, HOME_RUNTIME_WORK_DIR, UMOUNT_NOFOLLOW);
if (r < 0) if (r < 0)
return r; return r;

View File

@ -4,5 +4,6 @@
#include <stdbool.h> #include <stdbool.h>
int home_mount_node(const char *node, const char *fstype, bool discard, unsigned long flags); int home_mount_node(const char *node, const char *fstype, bool discard, unsigned long flags);
int home_unshare_and_mkdir(void);
int home_unshare_and_mount(const char *node, const char *fstype, bool discard, unsigned long flags); int home_unshare_and_mount(const char *node, const char *fstype, bool discard, unsigned long flags);
int home_move_mount(const char *user_name_and_realm, const char *target); int home_move_mount(const char *user_name_and_realm, const char *target);

View File

@ -317,7 +317,7 @@ int home_setup_done(HomeSetup *setup) {
} }
if (setup->undo_mount) { if (setup->undo_mount) {
q = umount_verbose(LOG_DEBUG, "/run/systemd/user-home-mount", UMOUNT_NOFOLLOW); q = umount_verbose(LOG_DEBUG, HOME_RUNTIME_WORK_DIR, UMOUNT_NOFOLLOW);
if (q < 0) if (q < 0)
r = q; r = q;
} }

View File

@ -28,12 +28,12 @@ typedef struct HomeSetup {
void *volume_key; void *volume_key;
size_t volume_key_size; size_t volume_key_size;
bool undo_dm; bool undo_dm:1;
bool undo_mount; bool undo_mount:1; /* Whether to unmount /run/systemd/user-home-mount */
bool do_offline_fitrim; bool do_offline_fitrim:1;
bool do_offline_fallocate; bool do_offline_fallocate:1;
bool do_mark_clean; bool do_mark_clean:1;
bool do_drop_caches; bool do_drop_caches:1;
uint64_t partition_offset; uint64_t partition_offset;
uint64_t partition_size; uint64_t partition_size;
@ -83,3 +83,5 @@ int home_extend_embedded_identity(UserRecord *h, UserRecord *used, HomeSetup *se
int user_record_authenticate(UserRecord *h, UserRecord *secret, PasswordCache *cache, bool strict_verify); int user_record_authenticate(UserRecord *h, UserRecord *secret, PasswordCache *cache, bool strict_verify);
int home_sync_and_statfs(int root_fd, struct statfs *ret); int home_sync_and_statfs(int root_fd, struct statfs *ret);
#define HOME_RUNTIME_WORK_DIR "/run/systemd/user-home-mount"