From 6d6d4459ab969cfd0b6fcb7afea1c76496f8788a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 4 Nov 2021 17:38:13 +0100 Subject: [PATCH] homectl: add new "homectl rebalance" command Let's add an explicit, synchronous command to request immediate rebalancing and wait for it. --- man/homectl.xml | 12 ++++++++++++ src/home/homectl.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/man/homectl.xml b/man/homectl.xml index ca9225a1e0d..5a06346d7b6 100644 --- a/man/homectl.xml +++ b/man/homectl.xml @@ -946,6 +946,18 @@ scripts and such, but requires authentication with the user's credentials in order to be able to unlock the user's home directory. + + + rebalance + + Rebalance free disk space between active home areas and the backing storage. See + above. This executes no operation unless there's at least one + active LUKS2 home area that has disk space rebalancing enabled. This operation is synchronous: it + will only complete once disk space is rebalanced according to the rebalancing weights. Note that + rebalancing also takes place automatically in the background in regular intervals. Use this command + to synchronously ensure disk space is properly redistributed before initiating an operation requiring + large amounts of disk space. + diff --git a/src/home/homectl.c b/src/home/homectl.c index 0c69299c84f..6057c04bae8 100644 --- a/src/home/homectl.c +++ b/src/home/homectl.c @@ -2103,6 +2103,32 @@ static int deactivate_all_homes(int argc, char *argv[], void *userdata) { return 0; } +static int rebalance(int argc, char *argv[], void *userdata) { + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; + _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; + int r; + + r = acquire_bus(&bus); + if (r < 0) + return r; + + r = bus_message_new_method_call(bus, &m, bus_mgr, "Rebalance"); + if (r < 0) + return bus_log_create_error(r); + + r = sd_bus_call(bus, m, HOME_SLOW_BUS_CALL_TIMEOUT_USEC, &error, NULL); + if (r < 0) { + if (sd_bus_error_has_name(&error, BUS_ERROR_REBALANCE_NOT_NEEDED)) + log_info("No homes needed rebalancing."); + else + return log_error_errno(r, "Failed to rebalance: %s", bus_error_message(&error, r)); + } else + log_info("Completed rebalancing."); + + return 0; +} + static int drop_from_identity(const char *field) { int r; @@ -2157,6 +2183,7 @@ static int help(int argc, char *argv[], void *userdata) { " unlock USER… Unlock a temporarily locked home area\n" " lock-all Lock all suitable home areas\n" " deactivate-all Deactivate all active home areas\n" + " rebalance Rebalance free space between home areas\n" " with USER [COMMAND…] Run shell or command with access to a home area\n" "\n%4$sOptions:%5$s\n" " -h --help Show this help\n" @@ -3746,6 +3773,7 @@ static int run(int argc, char *argv[]) { { "with", 2, VERB_ANY, 0, with_home }, { "lock-all", VERB_ANY, 1, 0, lock_all_homes }, { "deactivate-all", VERB_ANY, 1, 0, deactivate_all_homes }, + { "rebalance", VERB_ANY, 1, 0, rebalance }, {} };