shared/sleep-config: return a custom message when not enough swap for hibernation
$ sudo swapoff -av swapoff /dev/vda4 $ sudo systemctl hibernate Failed to hibernate system via logind: Not enough swap space for hibernation Fixes #6729.
This commit is contained in:
@@ -1835,11 +1835,14 @@ static int method_do_shutdown_or_sleep(
|
||||
|
||||
if (sleep_verb) {
|
||||
r = can_sleep(sleep_verb);
|
||||
if (r == -ENOSPC)
|
||||
return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
|
||||
"Not enough swap space for hibernation");
|
||||
if (r == 0)
|
||||
return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
|
||||
"Sleep verb \"%s\" not supported", sleep_verb);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (r == 0)
|
||||
return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
|
||||
}
|
||||
|
||||
r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
|
||||
@@ -2262,10 +2265,10 @@ static int method_can_shutdown_or_sleep(
|
||||
|
||||
if (sleep_verb) {
|
||||
r = can_sleep(sleep_verb);
|
||||
if (IN_SET(r, 0, -ENOSPC))
|
||||
return sd_bus_reply_method_return(message, "s", "na");
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0)
|
||||
return sd_bus_reply_method_return(message, "s", "na");
|
||||
}
|
||||
|
||||
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
|
||||
|
||||
@@ -369,12 +369,12 @@ static bool can_s2h(void) {
|
||||
|
||||
FOREACH_STRING(p, "suspend", "hibernate") {
|
||||
r = can_sleep(p);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to check if %s is possible: %m", p);
|
||||
if (r == 0) {
|
||||
if (IN_SET(r, 0, -ENOSPC)) {
|
||||
log_debug("Unable to %s system.", p);
|
||||
return false;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to check if %s is possible: %m", p);
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -396,5 +396,11 @@ int can_sleep(const char *verb) {
|
||||
if (!can_sleep_state(states) || !can_sleep_disk(modes))
|
||||
return false;
|
||||
|
||||
return streq(verb, "suspend") || enough_memory_for_hibernation();
|
||||
if (streq(verb, "suspend"))
|
||||
return true;
|
||||
|
||||
if (!enough_memory_for_hibernation())
|
||||
return -ENOSPC;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
+11
-4
@@ -65,7 +65,9 @@ static void test_sleep(void) {
|
||||
**platform = strv_new("platform", NULL),
|
||||
**shutdown = strv_new("shutdown", NULL),
|
||||
**freez = strv_new("freeze", NULL);
|
||||
int r;
|
||||
|
||||
log_info("/* configuration */");
|
||||
log_info("Standby configured: %s", yes_no(can_sleep_state(standby) > 0));
|
||||
log_info("Suspend configured: %s", yes_no(can_sleep_state(mem) > 0));
|
||||
log_info("Hibernate configured: %s", yes_no(can_sleep_state(disk) > 0));
|
||||
@@ -75,10 +77,15 @@ static void test_sleep(void) {
|
||||
log_info("Hibernate+Shutdown configured: %s", yes_no(can_sleep_disk(shutdown) > 0));
|
||||
log_info("Freeze configured: %s", yes_no(can_sleep_state(freez) > 0));
|
||||
|
||||
log_info("Suspend configured and possible: %s", yes_no(can_sleep("suspend") > 0));
|
||||
log_info("Hibernation configured and possible: %s", yes_no(can_sleep("hibernate") > 0));
|
||||
log_info("Hybrid-sleep configured and possible: %s", yes_no(can_sleep("hybrid-sleep") > 0));
|
||||
log_info("Suspend-then-Hibernate configured and possible: %s", yes_no(can_sleep("suspend-then-hibernate") > 0));
|
||||
log_info("/* running system */");
|
||||
r = can_sleep("suspend");
|
||||
log_info("Suspend configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
|
||||
r = can_sleep("hibernate");
|
||||
log_info("Hibernation configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
|
||||
r = can_sleep("hybrid-sleep");
|
||||
log_info("Hybrid-sleep configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
|
||||
r = can_sleep("suspend-then-hibernate");
|
||||
log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
Reference in New Issue
Block a user