homectl: add new "homectl rebalance" command

Let's add an explicit, synchronous command to request immediate rebalancing and
wait for it.
This commit is contained in:
Lennart Poettering 2021-11-04 17:38:13 +01:00
parent 4950591627
commit 6d6d4459ab
2 changed files with 40 additions and 0 deletions

View file

@ -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.</para></listitem>
</varlistentry>
<varlistentry>
<term><command>rebalance</command></term>
<listitem><para>Rebalance free disk space between active home areas and the backing storage. See
<option>--rebalance-weight=</option> 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.</para></listitem>
</varlistentry>
</variablelist>
</refsect1>

View file

@ -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 },
{}
};