hibernate-util: rework find_hibernate_location

* "HibernateLocation" struct is renamed to HibernationDevice
  to avoid ambiguity with the EFI variable. Also, it no longer
  takes the reference to a SwapEntry object, since it's really
  unnecessary (only SwapEntry.path is used), but increases complexity.
* SwapEntry is no longer used externally.
* find_hibernate_location is split into read_swap_entries and
  find_suitable_hibernation_device. The former reads all swap entries
  into SwapEntries object for later use.
* Make use of btrfs_get_file_physical_offset_fd

Closes #25130
This commit is contained in:
Mike Yuan 2023-09-28 09:05:41 +08:00
parent 2abc3c6d48
commit 596873c10c
No known key found for this signature in database
GPG key ID: 417471C0A40F58B3
3 changed files with 344 additions and 403 deletions

View file

@ -28,359 +28,10 @@
#define HIBERNATION_SWAP_THRESHOLD 0.98
SwapEntry* swap_entry_free(SwapEntry *se) {
if (!se)
return NULL;
void hibernation_device_done(HibernationDevice *device) {
assert(device);
free(se->path);
return mfree(se);
}
HibernateLocation* hibernate_location_free(HibernateLocation *hl) {
if (!hl)
return NULL;
swap_entry_free(hl->swap);
return mfree(hl);
}
static int swap_device_to_devnum(const SwapEntry *swap, dev_t *ret_dev) {
_cleanup_close_ int fd = -EBADF;
struct stat sb;
int r;
assert(swap);
assert(swap->path);
fd = open(swap->path, O_CLOEXEC|O_PATH);
if (fd < 0)
return -errno;
if (fstat(fd, &sb) < 0)
return -errno;
if (swap->type == SWAP_BLOCK) {
if (!S_ISBLK(sb.st_mode))
return -ENOTBLK;
*ret_dev = sb.st_rdev;
return 0;
}
r = stat_verify_regular(&sb);
if (r < 0)
return r;
return get_block_device_fd(fd, ret_dev);
}
/*
* Attempt to calculate the swap file offset on supported filesystems. On unsupported
* filesystems, a debug message is logged and ret_offset is set to UINT64_MAX.
*/
static int calculate_swap_file_offset(const SwapEntry *swap, uint64_t *ret_offset) {
_cleanup_close_ int fd = -EBADF;
_cleanup_free_ struct fiemap *fiemap = NULL;
int r;
assert(swap);
assert(swap->path);
assert(swap->type == SWAP_FILE);
assert(ret_offset);
fd = open(swap->path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fd < 0)
return log_debug_errno(errno, "Failed to open swap file %s to determine on-disk offset: %m", swap->path);
r = fd_verify_regular(fd);
if (r < 0)
return log_debug_errno(r, "Selected swap file is not a regular file.");
r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
if (r < 0)
return log_debug_errno(r, "Error checking %s for Btrfs filesystem: %m", swap->path);
if (r > 0) {
log_debug("%s: detection of swap file offset on Btrfs is not supported", swap->path);
*ret_offset = UINT64_MAX;
return 0;
}
r = read_fiemap(fd, &fiemap);
if (r < 0)
return log_debug_errno(r, "Unable to read extent map for '%s': %m", swap->path);
*ret_offset = fiemap->fm_extents[0].fe_physical / page_size();
return 0;
}
static int read_resume_files(dev_t *ret_resume, uint64_t *ret_resume_offset) {
_cleanup_free_ char *resume_str = NULL, *resume_offset_str = NULL;
uint64_t resume_offset;
dev_t resume;
int r;
assert(ret_resume);
assert(ret_resume_offset);
r = read_one_line_file("/sys/power/resume", &resume_str);
if (r < 0)
return log_debug_errno(r, "Error reading /sys/power/resume: %m");
r = parse_devnum(resume_str, &resume);
if (r < 0)
return log_debug_errno(r, "Error parsing /sys/power/resume device: %s: %m", resume_str);
r = read_one_line_file("/sys/power/resume_offset", &resume_offset_str);
if (r == -ENOENT) {
log_debug_errno(r, "Kernel does not support resume_offset; swap file offset detection will be skipped.");
resume_offset = 0;
} else if (r < 0)
return log_debug_errno(r, "Error reading /sys/power/resume_offset: %m");
else {
r = safe_atou64(resume_offset_str, &resume_offset);
if (r < 0)
return log_debug_errno(r, "Failed to parse value in /sys/power/resume_offset \"%s\": %m", resume_offset_str);
}
if (resume_offset > 0 && resume == 0)
log_debug("Warning: found /sys/power/resume_offset==%" PRIu64 ", but /sys/power/resume unset. Misconfiguration?",
resume_offset);
*ret_resume = resume;
*ret_resume_offset = resume_offset;
return 0;
}
/*
* Determine if the HibernateLocation matches the resume= (device) and resume_offset= (file).
*/
static bool location_is_resume_device(const HibernateLocation *location, dev_t sys_resume, uint64_t sys_offset) {
if (!location)
return false;
return sys_resume > 0 &&
sys_resume == location->devno &&
(sys_offset == location->offset || (sys_offset > 0 && location->offset == UINT64_MAX));
}
/*
* Attempt to find the hibernation location by parsing /proc/swaps, /sys/power/resume, and
* /sys/power/resume_offset.
*
* Beware:
* Never use a device or file as location that hasn't been somehow specified by a user that would also be
* entrusted with full system memory access (for example via /sys/power/resume) or that isn't an already
* active swap area!
* Otherwise various security attacks might become possible, for example an attacker could silently attach
* such a device and circumvent full disk encryption when it would be automatically used for hibernation.
* Also, having a swap area on top of encryption is not per se enough to protect from all such attacks.
*
* Returns:
* 1 - Values are set in /sys/power/resume and /sys/power/resume_offset.
* ret_hibernate_location will represent matching /proc/swap entry if identified or NULL if not.
*
* 0 - No values are set in /sys/power/resume and /sys/power/resume_offset.
ret_hibernate_location will represent the highest priority swap with most remaining space discovered in /proc/swaps.
*
* Negative value in the case of error.
*/
int find_hibernate_location(HibernateLocation **ret_hibernate_location) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_(hibernate_location_freep) HibernateLocation *hibernate_location = NULL;
dev_t sys_resume = 0; /* Unnecessary initialization to appease gcc */
uint64_t sys_offset = 0;
bool resume_match = false;
int r;
/* read the /sys/power/resume & /sys/power/resume_offset values */
r = read_resume_files(&sys_resume, &sys_offset);
if (r < 0)
return r;
f = fopen("/proc/swaps", "re");
if (!f) {
log_debug_errno(errno, "Failed to open /proc/swaps: %m");
return errno == ENOENT ? -EOPNOTSUPP : -errno; /* Convert swap not supported to a recognizable error */
}
(void) fscanf(f, "%*s %*s %*s %*s %*s\n");
for (unsigned i = 1;; i++) {
_cleanup_(swap_entry_freep) SwapEntry *swap = NULL;
_cleanup_free_ char *type = NULL;
uint64_t swap_offset = 0;
int k;
swap = new(SwapEntry, 1);
if (!swap)
return -ENOMEM;
*swap = (SwapEntry) {
.type = _SWAP_TYPE_INVALID,
};
k = fscanf(f,
"%ms " /* device/file path */
"%ms " /* type of swap */
"%" PRIu64 /* swap size */
"%" PRIu64 /* used */
"%i\n", /* priority */
&swap->path, &type, &swap->size, &swap->used, &swap->priority);
if (k == EOF)
break;
if (k != 5) {
log_debug("Failed to parse /proc/swaps:%u, ignoring", i);
continue;
}
if (streq(type, "file")) {
if (endswith(swap->path, "\\040(deleted)")) {
log_debug("Ignoring deleted swap file '%s'.", swap->path);
continue;
}
swap->type = SWAP_FILE;
r = calculate_swap_file_offset(swap, &swap_offset);
if (r < 0)
return r;
} else if (streq(type, "partition")) {
const char *fn;
fn = path_startswith(swap->path, "/dev/");
if (fn && startswith(fn, "zram")) {
log_debug("%s: ignoring zram swap", swap->path);
continue;
}
swap->type = SWAP_BLOCK;
} else {
log_debug("%s: swap type %s is unsupported for hibernation, ignoring", swap->path, type);
continue;
}
/* prefer resume device or highest priority swap with most remaining space */
if (sys_resume == 0) {
if (hibernate_location && swap->priority < hibernate_location->swap->priority) {
log_debug("%s: ignoring device with lower priority", swap->path);
continue;
}
if (hibernate_location &&
(swap->priority == hibernate_location->swap->priority
&& swap->size - swap->used < hibernate_location->swap->size - hibernate_location->swap->used)) {
log_debug("%s: ignoring device with lower usable space", swap->path);
continue;
}
}
dev_t swap_devno;
r = swap_device_to_devnum(swap, &swap_devno);
if (r < 0)
return log_debug_errno(r, "%s: failed to query device number: %m", swap->path);
if (swap_devno == 0)
return log_debug_errno(SYNTHETIC_ERRNO(ENODEV), "%s: not backed by block device.", swap->path);
hibernate_location = hibernate_location_free(hibernate_location);
hibernate_location = new(HibernateLocation, 1);
if (!hibernate_location)
return -ENOMEM;
*hibernate_location = (HibernateLocation) {
.devno = swap_devno,
.offset = swap_offset,
.swap = TAKE_PTR(swap),
};
/* if the swap is the resume device, stop the loop */
if (location_is_resume_device(hibernate_location, sys_resume, sys_offset)) {
log_debug("%s: device matches configured resume settings.", hibernate_location->swap->path);
resume_match = true;
break;
}
log_debug("%s: is a candidate device.", hibernate_location->swap->path);
}
/* We found nothing at all */
if (!hibernate_location)
return log_debug_errno(SYNTHETIC_ERRNO(ENOSYS),
"No possible swap partitions or files suitable for hibernation were found in /proc/swaps.");
/* resume= is set but a matching /proc/swaps entry was not found */
if (sys_resume != 0 && !resume_match)
return log_debug_errno(SYNTHETIC_ERRNO(ENOSYS),
"No swap partitions or files matching resume config were found in /proc/swaps.");
if (hibernate_location->offset == UINT64_MAX) {
if (sys_offset == 0)
return log_debug_errno(SYNTHETIC_ERRNO(ENOSYS), "Offset detection failed and /sys/power/resume_offset is not set.");
hibernate_location->offset = sys_offset;
}
if (resume_match)
log_debug("Hibernation will attempt to use swap entry with path: %s, device: %u:%u, offset: %" PRIu64 ", priority: %i",
hibernate_location->swap->path, major(hibernate_location->devno), minor(hibernate_location->devno),
hibernate_location->offset, hibernate_location->swap->priority);
else
log_debug("/sys/power/resume is not configured; attempting to hibernate with path: %s, device: %u:%u, offset: %" PRIu64 ", priority: %i",
hibernate_location->swap->path, major(hibernate_location->devno), minor(hibernate_location->devno),
hibernate_location->offset, hibernate_location->swap->priority);
*ret_hibernate_location = TAKE_PTR(hibernate_location);
if (resume_match)
return 1;
return 0;
}
bool enough_swap_for_hibernation(void) {
_cleanup_free_ char *active = NULL;
_cleanup_(hibernate_location_freep) HibernateLocation *hibernate_location = NULL;
unsigned long long act = 0;
int r;
if (getenv_bool("SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK") > 0)
return true;
r = find_hibernate_location(&hibernate_location);
if (r < 0)
return false;
/* If /sys/power/{resume,resume_offset} is configured but a matching entry
* could not be identified in /proc/swaps, user is likely using Btrfs with a swapfile;
* return true and let the system attempt hibernation.
*/
if (r > 0 && !hibernate_location) {
log_debug("Unable to determine remaining swap space; hibernation may fail");
return true;
}
if (!hibernate_location)
return false;
r = get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE, &active);
if (r < 0) {
log_debug_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
return false;
}
r = safe_atollu(active, &act);
if (r < 0) {
log_debug_errno(r, "Failed to parse Active(anon) from /proc/meminfo: %s: %m", active);
return false;
}
r = act <= (hibernate_location->swap->size - hibernate_location->swap->used) * HIBERNATION_SWAP_THRESHOLD;
log_debug("%s swap for hibernation, Active(anon)=%llu kB, size=%" PRIu64 " kB, used=%" PRIu64 " kB, threshold=%.2g%%",
r ? "Enough" : "Not enough", act, hibernate_location->swap->size, hibernate_location->swap->used, 100*HIBERNATION_SWAP_THRESHOLD);
return r;
free(device->path);
}
int read_fiemap(int fd, struct fiemap **ret) {
@ -468,6 +119,321 @@ int read_fiemap(int fd, struct fiemap **ret) {
return 0;
}
static int read_resume_config(dev_t *ret_devno, uint64_t *ret_offset) {
_cleanup_free_ char *devno_str = NULL, *offset_str = NULL;
uint64_t offset;
dev_t devno;
int r;
assert(ret_devno);
assert(ret_offset);
r = read_one_line_file("/sys/power/resume", &devno_str);
if (r < 0)
return log_debug_errno(r, "Failed to read /sys/power/resume: %m");
r = parse_devnum(devno_str, &devno);
if (r < 0)
return log_debug_errno(r, "Failed to parse /sys/power/resume devno '%s': %m", devno_str);
r = read_one_line_file("/sys/power/resume_offset", &offset_str);
if (r == -ENOENT) {
log_debug_errno(r, "Kernel does not expose resume_offset, skipping.");
offset = UINT64_MAX;
} else if (r < 0)
return log_debug_errno(r, "Failed to read /sys/power/resume_offset: %m");
else {
r = safe_atou64(offset_str, &offset);
if (r < 0)
return log_debug_errno(r,
"Failed to parse /sys/power/resume_offset '%s': %m", offset_str);
}
if (devno == 0 && offset > 0 && offset != UINT64_MAX)
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
"Found resume_offset=%" PRIu64 " but resume= is unset, refusing.", offset);
*ret_devno = devno;
*ret_offset = offset;
return 0;
}
/* entry in /proc/swaps */
typedef struct SwapEntry {
char *path;
bool swapfile;
uint64_t size;
uint64_t used;
int priority;
/* Not present in original entry */
dev_t devno;
uint64_t offset;
} SwapEntry;
typedef struct SwapEntries {
SwapEntry *swaps;
size_t n_swaps;
} SwapEntries;
static void swap_entry_done(SwapEntry *entry) {
assert(entry);
free(entry->path);
}
static void swap_entries_done(SwapEntries *entries) {
assert(entries);
FOREACH_ARRAY(i, entries->swaps, entries->n_swaps)
swap_entry_done(i);
free(entries->swaps);
}
static int swap_entry_get_resume_config(SwapEntry *swap) {
_cleanup_close_ int fd = -EBADF;
uint64_t offset_raw;
struct stat st;
int r;
assert(swap);
assert(swap->path);
fd = open(swap->path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (fd < 0)
return -errno;
if (fstat(fd, &st) < 0)
return -errno;
if (!swap->swapfile) {
if (!S_ISBLK(st.st_mode))
return -ENOTBLK;
swap->devno = st.st_rdev;
swap->offset = 0;
return 0;
}
r = stat_verify_regular(&st);
if (r < 0)
return r;
r = get_block_device_fd(fd, &swap->devno);
if (r < 0)
return r;
r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
if (r < 0)
return log_debug_errno(r, "Failed to check if swap file '%s' is on Btrfs: %m", swap->path);
if (r > 0) {
r = btrfs_get_file_physical_offset_fd(fd, &offset_raw);
if (r < 0)
return r;
} else {
_cleanup_free_ struct fiemap *fiemap = NULL;
r = read_fiemap(fd, &fiemap);
if (r < 0)
return log_debug_errno(r, "Failed to read extent map for swap file '%s': %m", swap->path);
offset_raw = fiemap->fm_extents[0].fe_physical;
}
swap->offset = offset_raw / page_size();
return 0;
}
static int read_swap_entries(SwapEntries *ret) {
_cleanup_(swap_entries_done) SwapEntries entries = {};
_cleanup_fclose_ FILE *f = NULL;
assert(ret);
f = fopen("/proc/swaps", "re");
if (!f)
return log_debug_errno(errno, "Failed to open /proc/swaps: %m");
/* Remove header */
(void) fscanf(f, "%*s %*s %*s %*s %*s\n");
for (unsigned i = 1;; i++) {
_cleanup_(swap_entry_done) SwapEntry swap = {};
_cleanup_free_ char *type = NULL;
int k;
k = fscanf(f,
"%ms " /* device/file path */
"%ms " /* type of swap */
"%" PRIu64 /* swap size */
"%" PRIu64 /* used */
"%i" /* priority */
"\n",
&swap.path, &type, &swap.size, &swap.used, &swap.priority);
if (k == EOF)
break;
if (k != 5)
return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Failed to parse /proc/swaps line %u.", i);
if (streq(type, "file")) {
if (endswith(swap.path, "\\040(deleted)")) {
log_debug("Swap file '%s' has been deleted, ignoring.", swap.path);
continue;
}
swap.swapfile = true;
} else if (streq(type, "partition")) {
const char *node;
node = path_startswith(swap.path, "/dev/");
if (node && startswith(node, "zram")) {
log_debug("Swap partition '%s' is a zram device, ignoring.", swap.path);
continue;
}
swap.swapfile = false;
} else {
log_debug("Swap type %s is not supported for hibernation, ignoring device: %s",
type, swap.path);
continue;
}
if (!GREEDY_REALLOC(entries.swaps, entries.n_swaps + 1))
return log_oom_debug();
entries.swaps[entries.n_swaps++] = TAKE_STRUCT(swap);
}
*ret = TAKE_STRUCT(entries);
return 0;
}
/* Attempt to find a suitable device for hibernation by parsing /proc/swaps, /sys/power/resume, and
* /sys/power/resume_offset.
*
* Beware:
* Never use a device or file that hasn't been somehow specified by a user who would also be entrusted
* with full system memory access (for example via /sys/power/resume) or that isn't an already active
* swap area! Otherwise various security attacks might become possible, for example an attacker could
* silently attach such a device and circumvent full disk encryption when it would be automatically used
* for hibernation. Also, having a swap area on top of encryption is not per se enough to protect from all
* such attacks.
*
* Returns:
* 1 - Values are set in /sys/power/resume and /sys/power/resume_offset.
*
* 0 - No values are set in /sys/power/resume and /sys/power/resume_offset.
* ret will represent the highest priority swap with most remaining space discovered in /proc/swaps.
*
* Negative value in the case of error */
int find_suitable_hibernation_device_full(HibernationDevice *ret_device, uint64_t *ret_size, uint64_t *ret_used) {
_cleanup_(swap_entries_done) SwapEntries entries = {};
SwapEntry *entry = NULL;
uint64_t resume_config_offset;
dev_t resume_config_devno;
int r;
assert(!ret_size == !ret_used);
r = read_resume_config(&resume_config_devno, &resume_config_offset);
if (r < 0)
return r;
r = read_swap_entries(&entries);
if (r < 0)
return r;
if (entries.n_swaps == 0)
return log_debug_errno(SYNTHETIC_ERRNO(ENOENT), "No swap space available for hibernation.");
FOREACH_ARRAY(swap, entries.swaps, entries.n_swaps) {
r = swap_entry_get_resume_config(swap);
if (r < 0)
return log_debug_errno(r, "Failed to get devno and offset for swap '%s': %m", swap->path);
if (swap->devno == 0) {
assert(swap->swapfile);
log_debug("Swap file '%s' is not backed by block device, ignoring: %m", swap->path);
continue;
}
if (resume_config_devno > 0) {
if (swap->devno == resume_config_devno &&
(!swap->swapfile || resume_config_offset == UINT64_MAX || swap->offset == resume_config_offset)) {
/* /sys/power/resume (resume=) is set, and the calculated swap file offset
* matches with /sys/power/resume_offset. If /sys/power/resume_offset is not
* exposed, we can't do proper check anyway, so use the found swap file too. */
entry = swap;
break;
}
/* If resume= is set, don't try to use other swap spaces. */
continue;
}
if (!entry ||
swap->priority > entry->priority ||
swap->size - swap->used > entry->size - entry->used)
entry = swap;
}
if (!entry) {
/* No need to check n_swaps == 0, since it's rejected early */
assert(resume_config_devno > 0);
return log_debug_errno(SYNTHETIC_ERRNO(ENXIO), "Cannot find swap entry corresponding to /sys/power/resume.");
}
if (ret_device)
*ret_device = (HibernationDevice) {
.devno = entry->devno,
.offset = entry->offset,
.path = TAKE_PTR(entry->path),
};
if (ret_size) {
*ret_size = entry->size;
*ret_used = entry->used;
}
return resume_config_devno > 0;
}
bool enough_swap_for_hibernation(void) {
_cleanup_free_ char *active_str = NULL;
unsigned long long active;
uint64_t size, used;
int r;
if (getenv_bool("SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK") > 0)
return true;
r = find_suitable_hibernation_device_full(NULL, &size, &used);
if (r < 0)
return false;
r = get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE, &active_str);
if (r < 0) {
log_debug_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
return false;
}
r = safe_atollu(active_str, &active);
if (r < 0) {
log_debug_errno(r, "Failed to parse Active(anon) '%s' from /proc/meminfo: %m", active_str);
return false;
}
r = active <= (size - used) * HIBERNATION_SWAP_THRESHOLD;
log_debug("Detected %s swap for hibernation: Active(anon)=%llu kB, size=%" PRIu64 " kB, used=%" PRIu64 " kB, threshold=%.2g%%",
r ? "enough" : "not enough", active, size, used, 100 * HIBERNATION_SWAP_THRESHOLD);
return r;
}
int write_resume_config(dev_t devno, uint64_t offset, const char *device) {
char offset_str[DECIMAL_STR_MAX(uint64_t)];
_cleanup_free_ char *path = NULL;

View file

@ -4,39 +4,23 @@
#include <linux/fiemap.h>
#include <sys/types.h>
typedef enum SwapType {
SWAP_BLOCK,
SWAP_FILE,
_SWAP_TYPE_MAX,
_SWAP_TYPE_INVALID = -EINVAL,
} SwapType;
/* entry in /proc/swaps */
typedef struct SwapEntry {
char *path;
SwapType type;
uint64_t size;
uint64_t used;
int priority;
} SwapEntry;
SwapEntry* swap_entry_free(SwapEntry *se);
DEFINE_TRIVIAL_CLEANUP_FUNC(SwapEntry*, swap_entry_free);
/*
* represents values for /sys/power/resume & /sys/power/resume_offset
* and the matching /proc/swap entry.
*/
typedef struct HibernateLocation {
/* represents values for /sys/power/resume & /sys/power/resume_offset and the corresponding path */
typedef struct HibernationDevice {
dev_t devno;
uint64_t offset; /* in memory pages */
SwapEntry *swap;
} HibernateLocation;
char *path;
} HibernationDevice;
HibernateLocation* hibernate_location_free(HibernateLocation *hl);
DEFINE_TRIVIAL_CLEANUP_FUNC(HibernateLocation*, hibernate_location_free);
void hibernation_device_done(HibernationDevice *hibernation_device);
int find_suitable_hibernation_device_full(HibernationDevice *ret_device, uint64_t *ret_size, uint64_t *ret_used);
static inline int find_suitable_hibernation_device(HibernationDevice *ret) {
return find_suitable_hibernation_device_full(ASSERT_PTR(ret), NULL, NULL);
}
int read_fiemap(int fd, struct fiemap **ret);
int find_hibernate_location(HibernateLocation **ret_hibernate_location);
int write_resume_config(dev_t devno, uint64_t offset, const char *device);
bool enough_swap_for_hibernation(void);
int write_resume_config(dev_t devno, uint64_t offset, const char *device);
/* Only for test-fiemap */
int read_fiemap(int fd, struct fiemap **ret);

View file

@ -54,7 +54,7 @@
static SleepOperation arg_operation = _SLEEP_OPERATION_INVALID;
static int write_efi_hibernate_location(const HibernateLocation *hibernate_location, bool required) {
static int write_efi_hibernate_location(const HibernationDevice *hibernation_device, bool required) {
int log_level = required ? LOG_ERR : LOG_DEBUG;
#if ENABLE_EFI
@ -67,27 +67,26 @@ static int write_efi_hibernate_location(const HibernateLocation *hibernate_locat
struct utsname uts = {};
int r, log_level_ignore = required ? LOG_WARNING : LOG_DEBUG;
assert(hibernate_location);
assert(hibernate_location->swap);
assert(hibernation_device);
if (!is_efi_boot())
return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
"Not an EFI boot, passing HibernateLocation via EFI variable is not possible.");
r = sd_device_new_from_devnum(&device, 'b', hibernate_location->devno);
r = sd_device_new_from_devnum(&device, 'b', hibernation_device->devno);
if (r < 0)
return log_full_errno(log_level, r, "Failed to create sd-device object for '%s': %m",
hibernate_location->swap->path);
hibernation_device->path);
r = sd_device_get_property_value(device, "ID_FS_UUID", &uuid_str);
if (r < 0)
return log_full_errno(log_level, r, "Failed to get filesystem UUID for device '%s': %m",
hibernate_location->swap->path);
hibernation_device->path);
r = sd_id128_from_string(uuid_str, &uuid);
if (r < 0)
return log_full_errno(log_level, r, "Failed to parse ID_FS_UUID '%s' for device '%s': %m",
uuid_str, hibernate_location->swap->path);
uuid_str, hibernation_device->path);
if (uname(&uts) < 0)
log_full_errno(log_level_ignore, errno, "Failed to get kernel info, ignoring: %m");
@ -102,7 +101,7 @@ static int write_efi_hibernate_location(const HibernateLocation *hibernate_locat
r = json_build(&v, JSON_BUILD_OBJECT(
JSON_BUILD_PAIR_UUID("uuid", uuid),
JSON_BUILD_PAIR_UNSIGNED("offset", hibernate_location->offset),
JSON_BUILD_PAIR_UNSIGNED("offset", hibernation_device->offset),
JSON_BUILD_PAIR_CONDITION(!isempty(uts.release), "kernelVersion", JSON_BUILD_STRING(uts.release)),
JSON_BUILD_PAIR_CONDITION(id, "osReleaseId", JSON_BUILD_STRING(id)),
JSON_BUILD_PAIR_CONDITION(image_id, "osReleaseImageId", JSON_BUILD_STRING(image_id)),
@ -127,14 +126,6 @@ static int write_efi_hibernate_location(const HibernateLocation *hibernate_locat
#endif
}
static int write_kernel_hibernate_location(const HibernateLocation *hibernate_location) {
assert(hibernate_location);
assert(hibernate_location->swap);
assert(IN_SET(hibernate_location->swap->type, SWAP_BLOCK, SWAP_FILE));
return write_resume_config(hibernate_location->devno, hibernate_location->offset, hibernate_location->swap->path);
}
static int write_mode(char **modes) {
int r = 0;
@ -266,7 +257,7 @@ static int execute(
NULL
};
_cleanup_(hibernate_location_freep) HibernateLocation *hibernate_location = NULL;
_cleanup_(hibernation_device_done) HibernationDevice hibernation_device = {};
_cleanup_fclose_ FILE *f = NULL;
char **modes, **states;
int r;
@ -296,19 +287,19 @@ static int execute(
if (!strv_isempty(modes)) {
bool resume_set;
r = find_hibernate_location(&hibernate_location);
r = find_suitable_hibernation_device(&hibernation_device);
if (r < 0)
return log_error_errno(r, "Failed to find location to hibernate to: %m");
resume_set = r > 0;
r = write_efi_hibernate_location(hibernate_location, !resume_set);
r = write_efi_hibernate_location(&hibernation_device, !resume_set);
if (!resume_set) {
if (r == -EOPNOTSUPP)
return log_error_errno(r, "No valid 'resume=' option found, refusing to hibernate.");
if (r < 0)
return r;
r = write_kernel_hibernate_location(hibernate_location);
r = write_resume_config(hibernation_device.devno, hibernation_device.offset, hibernation_device.path);
if (r < 0) {
if (is_efi_boot())
(void) efi_set_variable(EFI_SYSTEMD_VARIABLE(HibernateLocation), NULL, 0);