|
Packit Service |
158247 |
/*
|
|
Packit Service |
158247 |
* Copyright (C) 2014-2017 Red Hat, Inc.
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* This library is free software; you can redistribute it and/or
|
|
Packit Service |
158247 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
158247 |
* License as published by the Free Software Foundation; either
|
|
Packit Service |
158247 |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* This library is distributed in the hope that it will be useful,
|
|
Packit Service |
158247 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
158247 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
158247 |
* Lesser General Public License for more details.
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
158247 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Author: Vratislav Podzimek <vpodzime@redhat.com>
|
|
Packit Service |
158247 |
*/
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
#include <glib.h>
|
|
Packit Service |
158247 |
#include <string.h>
|
|
Packit Service |
158247 |
#include <unistd.h>
|
|
Packit Service |
158247 |
#include <sys/swap.h>
|
|
Packit Service |
158247 |
#include <fcntl.h>
|
|
Packit Service |
158247 |
#include <blkid.h>
|
|
Packit Service |
158247 |
#include <blockdev/utils.h>
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
#include "swap.h"
|
|
Packit Service |
158247 |
#include "check_deps.h"
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
/**
|
|
Packit Service |
158247 |
* SECTION: swap
|
|
Packit Service |
158247 |
* @short_description: plugin for operations with swap space
|
|
Packit Service |
158247 |
* @title: Swap
|
|
Packit Service |
158247 |
* @include: swap.h
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* A plugin for operations with swap space.
|
|
Packit Service |
158247 |
*/
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
/**
|
|
Packit Service |
158247 |
* bd_swap_error_quark: (skip)
|
|
Packit Service |
158247 |
*/
|
|
Packit Service |
158247 |
GQuark bd_swap_error_quark (void)
|
|
Packit Service |
158247 |
{
|
|
Packit Service |
158247 |
return g_quark_from_static_string ("g-bd-swap-error-quark");
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
static volatile guint avail_deps = 0;
|
|
Packit Service |
158247 |
static GMutex deps_check_lock;
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
#define DEPS_MKSWAP 0
|
|
Packit Service |
158247 |
#define DEPS_MKSWAP_MASK (1 << DEPS_MKSWAP)
|
|
Packit Service |
158247 |
#define DEPS_SWAPLABEL 1
|
|
Packit Service |
158247 |
#define DEPS_SWAPLABEL_MASK (1 << DEPS_SWAPLABEL)
|
|
Packit Service |
158247 |
#define DEPS_LAST 2
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
static const UtilDep deps[DEPS_LAST] = {
|
|
Packit Service |
158247 |
{"mkswap", MKSWAP_MIN_VERSION, NULL, "mkswap from util-linux ([\\d\\.]+)"},
|
|
Packit Service |
158247 |
{"swaplabel", NULL, NULL, NULL},
|
|
Packit Service |
158247 |
};
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
/**
|
|
Packit Service |
158247 |
* bd_swap_check_deps:
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Returns: whether the plugin's runtime dependencies are satisfied or not
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Function checking plugin's runtime dependencies.
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
*/
|
|
Packit Service |
158247 |
gboolean bd_swap_check_deps (void) {
|
|
Packit Service |
158247 |
GError *error = NULL;
|
|
Packit Service |
158247 |
guint i = 0;
|
|
Packit Service |
158247 |
gboolean status = FALSE;
|
|
Packit Service |
158247 |
gboolean ret = TRUE;
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
for (i=0; i < DEPS_LAST; i++) {
|
|
Packit Service |
158247 |
status = bd_utils_check_util_version (deps[i].name, deps[i].version,
|
|
Packit Service |
158247 |
deps[i].ver_arg, deps[i].ver_regexp, &error);
|
|
Packit Service |
158247 |
if (!status)
|
|
Packit Service |
158247 |
g_warning ("%s", error->message);
|
|
Packit Service |
158247 |
else
|
|
Packit Service |
158247 |
g_atomic_int_or (&avail_deps, 1 << i);
|
|
Packit Service |
158247 |
g_clear_error (&error);
|
|
Packit Service |
158247 |
ret = ret && status;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
if (!ret)
|
|
Packit Service |
158247 |
g_warning("Cannot load the swap plugin");
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
return ret;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
/**
|
|
Packit Service |
158247 |
* bd_swap_init:
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Initializes the plugin. **This function is called automatically by the
|
|
Packit Service |
158247 |
* library's initialization functions.**
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
*/
|
|
Packit Service |
158247 |
gboolean bd_swap_init (void) {
|
|
Packit Service |
158247 |
/* nothing to do here */
|
|
Packit Service |
158247 |
return TRUE;
|
|
Packit Service |
158247 |
};
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
/**
|
|
Packit Service |
158247 |
* bd_swap_close:
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Cleans up after the plugin. **This function is called automatically by the
|
|
Packit Service |
158247 |
* library's functions that unload it.**
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
*/
|
|
Packit Service |
158247 |
void bd_swap_close (void) {
|
|
Packit Service |
158247 |
/* nothing to do here */
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
#define UNUSED __attribute__((unused))
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
/**
|
|
Packit Service |
158247 |
* bd_swap_is_tech_avail:
|
|
Packit Service |
158247 |
* @tech: the queried tech
|
|
Packit Service |
158247 |
* @mode: a bit mask of queried modes of operation (#BDSwapTechMode) for @tech
|
|
Packit Service |
158247 |
* @error: (out): place to store error (details about why the @tech-@mode combination is not available)
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Returns: whether the @tech-@mode combination is available -- supported by the
|
|
Packit Service |
158247 |
* plugin implementation and having all the runtime dependencies available
|
|
Packit Service |
158247 |
*/
|
|
Packit Service |
158247 |
gboolean bd_swap_is_tech_avail (BDSwapTech tech UNUSED, guint64 mode, GError **error) {
|
|
Packit Service |
158247 |
guint32 requires = 0;
|
|
Packit Service |
158247 |
if (mode & BD_SWAP_TECH_MODE_CREATE)
|
|
Packit Service |
158247 |
requires |= DEPS_MKSWAP_MASK;
|
|
Packit Service |
158247 |
if (mode & BD_SWAP_TECH_MODE_SET_LABEL)
|
|
Packit Service |
158247 |
requires |= DEPS_SWAPLABEL_MASK;
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
return check_deps (&avail_deps, requires, deps, DEPS_LAST, &deps_check_lock, error);
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
/**
|
|
Packit Service |
158247 |
* bd_swap_mkswap:
|
|
Packit Service |
158247 |
* @device: a device to create swap space on
|
|
Packit Service |
158247 |
* @label: (allow-none): a label for the swap space device
|
|
Packit Service |
158247 |
* @extra: (allow-none) (array zero-terminated=1): extra options for the creation (right now
|
|
Packit Service |
158247 |
* passed to the 'mkswap' utility)
|
|
Packit Service |
158247 |
* @error: (out): place to store error (if any)
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Returns: whether the swap space was successfully created or not
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_CREATE
|
|
Packit Service |
158247 |
*/
|
|
Packit Service |
158247 |
gboolean bd_swap_mkswap (const gchar *device, const gchar *label, const BDExtraArg **extra, GError **error) {
|
|
Packit Service |
158247 |
guint8 next_arg = 2;
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
if (!check_deps (&avail_deps, DEPS_MKSWAP_MASK, deps, DEPS_LAST, &deps_check_lock, error))
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
/* We use -f to force since mkswap tends to refuse creation on lvs with
|
|
Packit Service |
158247 |
a message about erasing bootbits sectors on whole disks. Bah. */
|
|
Packit Service |
158247 |
const gchar *argv[6] = {"mkswap", "-f", NULL, NULL, NULL, NULL};
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
if (label) {
|
|
Packit Service |
158247 |
argv[next_arg] = "-L";
|
|
Packit Service |
158247 |
next_arg++;
|
|
Packit Service |
158247 |
argv[next_arg] = label;
|
|
Packit Service |
158247 |
next_arg++;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
argv[next_arg] = device;
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
return bd_utils_exec_and_report_error (argv, extra, error);
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
/**
|
|
Packit Service |
158247 |
* bd_swap_swapon:
|
|
Packit Service |
158247 |
* @device: swap device to activate
|
|
Packit Service |
158247 |
* @priority: priority of the activated device or -1 to use the default
|
|
Packit Service |
158247 |
* @error: (out): place to store error (if any)
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Returns: whether the swap device was successfully activated or not
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_ACTIVATE_DEACTIVATE
|
|
Packit Service |
158247 |
*/
|
|
Packit Service |
158247 |
gboolean bd_swap_swapon (const gchar *device, gint priority, GError **error) {
|
|
Packit Service |
158247 |
blkid_probe probe = NULL;
|
|
Packit Service |
158247 |
gint fd = 0;
|
|
Packit Service |
158247 |
gint status = 0;
|
|
Packit Service |
158247 |
guint n_try = 0;
|
|
Packit Service |
158247 |
const gchar *value = NULL;
|
|
Packit Service |
158247 |
gint64 status_len = 0;
|
|
Packit Service |
158247 |
gint64 swap_pagesize = 0;
|
|
Packit Service |
158247 |
gint64 sys_pagesize = 0;
|
|
Packit Service |
158247 |
gint flags = 0;
|
|
Packit Service |
158247 |
gint ret = 0;
|
|
Packit Service |
158247 |
guint64 progress_id = 0;
|
|
Packit Service |
158247 |
gchar *msg = NULL;
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
msg = g_strdup_printf ("Started 'swapon %s'", device);
|
|
Packit Service |
158247 |
progress_id = bd_utils_report_started (msg);
|
|
Packit Service |
158247 |
g_free (msg);
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
bd_utils_report_progress (progress_id, 0, "Analysing the swap device");
|
|
Packit Service |
158247 |
/* check the device if it is an activatable swap */
|
|
Packit Service |
158247 |
probe = blkid_new_probe ();
|
|
Packit Service |
158247 |
if (!probe) {
|
|
Packit Service |
158247 |
g_set_error (error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE,
|
|
Packit Service |
158247 |
"Failed to create a new probe");
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, (*error)->message);
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
fd = open (device, O_RDONLY|O_CLOEXEC);
|
|
Packit Service |
158247 |
if (fd == -1) {
|
|
Packit Service |
158247 |
g_set_error (error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE,
|
|
Packit Service |
158247 |
"Failed to open the device '%s'", device);
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, (*error)->message);
|
|
Packit Service |
158247 |
blkid_free_probe (probe);
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
/* we may need to try mutliple times with some delays in case the device is
|
|
Packit Service |
158247 |
busy at the very moment */
|
|
Packit Service |
158247 |
for (n_try=5, status=-1; (status != 0) && (n_try > 0); n_try--) {
|
|
Packit Service |
158247 |
status = blkid_probe_set_device (probe, fd, 0, 0);
|
|
Packit Service |
158247 |
if (status != 0)
|
|
Packit Service |
158247 |
g_usleep (100 * 1000); /* microseconds */
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
if (status != 0) {
|
|
Packit Service |
158247 |
g_set_error (error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE,
|
|
Packit Service |
158247 |
"Failed to create a probe for the device '%s'", device);
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, (*error)->message);
|
|
Packit Service |
158247 |
blkid_free_probe (probe);
|
|
Packit Service |
158247 |
close (fd);
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
blkid_probe_enable_superblocks (probe, 1);
|
|
Packit Service |
158247 |
blkid_probe_set_superblocks_flags (probe, BLKID_SUBLKS_TYPE | BLKID_SUBLKS_MAGIC);
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
/* we may need to try mutliple times with some delays in case the device is
|
|
Packit Service |
158247 |
busy at the very moment */
|
|
Packit Service |
158247 |
for (n_try=5, status=-1; !(status == 0 || status == 1) && (n_try > 0); n_try--) {
|
|
Packit Service |
158247 |
status = blkid_do_safeprobe (probe);
|
|
Packit Service |
158247 |
if (status < 0)
|
|
Packit Service |
158247 |
g_usleep (100 * 1000); /* microseconds */
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
if (status < 0) {
|
|
Packit Service |
158247 |
/* -1 or -2 = error during probing*/
|
|
Packit Service |
158247 |
g_set_error (error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE,
|
|
Packit Service |
158247 |
"Failed to probe the device '%s'", device);
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, (*error)->message);
|
|
Packit Service |
158247 |
blkid_free_probe (probe);
|
|
Packit Service |
158247 |
close (fd);
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
} else if (status == 1) {
|
|
Packit Service |
158247 |
/* 1 = nothing detected */
|
|
Packit Service |
158247 |
g_set_error (error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE,
|
|
Packit Service |
158247 |
"No superblock detected on the device '%s'", device);
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, (*error)->message);
|
|
Packit Service |
158247 |
blkid_free_probe (probe);
|
|
Packit Service |
158247 |
close (fd);
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
status = blkid_probe_lookup_value (probe, "TYPE", &value, NULL);
|
|
Packit Service |
158247 |
if (status != 0) {
|
|
Packit Service |
158247 |
g_set_error (error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE,
|
|
Packit Service |
158247 |
"Failed to get format type for the device '%s'", device);
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, (*error)->message);
|
|
Packit Service |
158247 |
blkid_free_probe (probe);
|
|
Packit Service |
158247 |
close (fd);
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
if (g_strcmp0 (value, "swap") != 0) {
|
|
Packit Service |
158247 |
g_set_error (error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE,
|
|
Packit Service |
158247 |
"Device '%s' is not formatted as swap", device);
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, (*error)->message);
|
|
Packit Service |
158247 |
blkid_free_probe (probe);
|
|
Packit Service |
158247 |
close (fd);
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
status = blkid_probe_lookup_value (probe, "SBMAGIC", &value, NULL);
|
|
Packit Service |
158247 |
if (status != 0) {
|
|
Packit Service |
158247 |
g_set_error (error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE,
|
|
Packit Service |
158247 |
"Failed to get swap status on the device '%s'", device);
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, (*error)->message);
|
|
Packit Service |
158247 |
blkid_free_probe (probe);
|
|
Packit Service |
158247 |
close (fd);
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
if (g_strcmp0 (value, "SWAP-SPACE") == 0) {
|
|
Packit Service |
158247 |
g_set_error (error, BD_SWAP_ERROR, BD_SWAP_ERROR_ACTIVATE_OLD,
|
|
Packit Service |
158247 |
"Old swap format, cannot activate.");
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, (*error)->message);
|
|
Packit Service |
158247 |
blkid_free_probe (probe);
|
|
Packit Service |
158247 |
close (fd);
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
} else if (g_strcmp0 (value, "S1SUSPEND") == 0 || g_strcmp0 (value, "S2SUSPEND") == 0) {
|
|
Packit Service |
158247 |
g_set_error (error, BD_SWAP_ERROR, BD_SWAP_ERROR_ACTIVATE_SUSPEND,
|
|
Packit Service |
158247 |
"Suspended system on the swap device, cannot activate.");
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, (*error)->message);
|
|
Packit Service |
158247 |
blkid_free_probe (probe);
|
|
Packit Service |
158247 |
close (fd);
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
} else if (g_strcmp0 (value, "SWAPSPACE2") != 0) {
|
|
Packit Service |
158247 |
g_set_error (error, BD_SWAP_ERROR, BD_SWAP_ERROR_ACTIVATE_UNKNOWN,
|
|
Packit Service |
158247 |
"Unknown swap space format, cannot activate.");
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, (*error)->message);
|
|
Packit Service |
158247 |
blkid_free_probe (probe);
|
|
Packit Service |
158247 |
close (fd);
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
status_len = (gint64) strlen (value);
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
status = blkid_probe_lookup_value (probe, "SBMAGIC_OFFSET", &value, NULL);
|
|
Packit Service |
158247 |
if (status != 0 || !value) {
|
|
Packit Service |
158247 |
g_set_error (error, BD_SWAP_ERROR, BD_SWAP_ERROR_ACTIVATE_PAGESIZE,
|
|
Packit Service |
158247 |
"Failed to get swap status on the device '%s'", device);
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, (*error)->message);
|
|
Packit Service |
158247 |
blkid_free_probe (probe);
|
|
Packit Service |
158247 |
close (fd);
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
swap_pagesize = status_len + g_ascii_strtoll (value, (char **)NULL, 10);
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
blkid_free_probe (probe);
|
|
Packit Service |
158247 |
close (fd);
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
sys_pagesize = getpagesize ();
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
if (swap_pagesize != sys_pagesize) {
|
|
Packit Service |
158247 |
g_set_error (error, BD_SWAP_ERROR, BD_SWAP_ERROR_ACTIVATE_PAGESIZE,
|
|
Packit Service |
158247 |
"Swap format pagesize (%"G_GINT64_FORMAT") and system pagesize (%"G_GINT64_FORMAT") don't match",
|
|
Packit Service |
158247 |
swap_pagesize, sys_pagesize);
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, (*error)->message);
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
bd_utils_report_progress (progress_id, 10, "Swap device analysed, enabling");
|
|
Packit Service |
158247 |
if (priority >= 0) {
|
|
Packit Service |
158247 |
flags = SWAP_FLAG_PREFER;
|
|
Packit Service |
158247 |
flags |= (priority << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
ret = swapon (device, flags);
|
|
Packit Service |
158247 |
if (ret != 0) {
|
|
Packit Service |
158247 |
g_set_error (error, BD_SWAP_ERROR, BD_SWAP_ERROR_ACTIVATE,
|
|
Packit Service |
158247 |
"Failed to activate swap on %s: %m", device);
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, (*error)->message);
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, "Completed");
|
|
Packit Service |
158247 |
return ret == 0;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
/**
|
|
Packit Service |
158247 |
* bd_swap_swapoff:
|
|
Packit Service |
158247 |
* @device: swap device to deactivate
|
|
Packit Service |
158247 |
* @error: (out): place to store error (if any)
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Returns: whether the swap device was successfully deactivated or not
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_ACTIVATE_DEACTIVATE
|
|
Packit Service |
158247 |
*/
|
|
Packit Service |
158247 |
gboolean bd_swap_swapoff (const gchar *device, GError **error) {
|
|
Packit Service |
158247 |
gint ret = 0;
|
|
Packit Service |
158247 |
guint64 progress_id = 0;
|
|
Packit Service |
158247 |
gchar *msg = NULL;
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
msg = g_strdup_printf ("Started 'swapoff %s'", device);
|
|
Packit Service |
158247 |
progress_id = bd_utils_report_started (msg);
|
|
Packit Service |
158247 |
g_free (msg);
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
ret = swapoff (device);
|
|
Packit Service |
158247 |
if (ret != 0) {
|
|
Packit Service |
158247 |
g_set_error (error, BD_SWAP_ERROR, BD_SWAP_ERROR_ACTIVATE,
|
|
Packit Service |
158247 |
"Failed to deactivate swap on %s: %m", device);
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, (*error)->message);
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
bd_utils_report_finished (progress_id, "Completed");
|
|
Packit Service |
158247 |
return ret == 0;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
/**
|
|
Packit Service |
158247 |
* bd_swap_swapstatus:
|
|
Packit Service |
158247 |
* @device: swap device to get status of
|
|
Packit Service |
158247 |
* @error: (out): place to store error (if any)
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Returns: %TRUE if the swap device is active, %FALSE if not active or failed
|
|
Packit Service |
158247 |
* to determine (@error) is set not a non-NULL value in such case)
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_QUERY
|
|
Packit Service |
158247 |
*/
|
|
Packit Service |
158247 |
gboolean bd_swap_swapstatus (const gchar *device, GError **error) {
|
|
Packit Service |
158247 |
gchar *file_content = NULL;
|
|
Packit Service |
158247 |
gchar *real_device = NULL;
|
|
Packit Service |
158247 |
gsize length;
|
|
Packit Service |
158247 |
gchar *next_line;
|
|
Packit Service |
158247 |
gboolean success;
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
success = g_file_get_contents ("/proc/swaps", &file_content, &length, error);
|
|
Packit Service |
158247 |
if (!success) {
|
|
Packit Service |
158247 |
/* error is already populated */
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
/* get the real device node for device-mapper devices since the ones
|
|
Packit Service |
158247 |
with meaningful names are just dev_paths */
|
|
Packit Service |
158247 |
if (g_str_has_prefix (device, "/dev/mapper/") || g_str_has_prefix (device, "/dev/md/")) {
|
|
Packit Service |
158247 |
real_device = bd_utils_resolve_device (device, error);
|
|
Packit Service |
158247 |
if (!real_device) {
|
|
Packit Service |
158247 |
/* the device doesn't exist and thus is not an active swap */
|
|
Packit Service |
158247 |
g_clear_error (error);
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
if (g_str_has_prefix (file_content, real_device ? real_device : device)) {
|
|
Packit Service |
158247 |
g_free (real_device);
|
|
Packit Service |
158247 |
g_free (file_content);
|
|
Packit Service |
158247 |
return TRUE;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
next_line = (strchr (file_content, '\n') + 1);
|
|
Packit Service |
158247 |
while (next_line && ((gsize)(next_line - file_content) < length)) {
|
|
Packit Service |
158247 |
if (g_str_has_prefix (next_line, real_device ? real_device : device)) {
|
|
Packit Service |
158247 |
g_free (real_device);
|
|
Packit Service |
158247 |
g_free (file_content);
|
|
Packit Service |
158247 |
return TRUE;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
next_line = (strchr (next_line, '\n') + 1);
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
g_free (real_device);
|
|
Packit Service |
158247 |
g_free (file_content);
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
}
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
/**
|
|
Packit Service |
158247 |
* bd_swap_set_label:
|
|
Packit Service |
158247 |
* @device: a device to set label on
|
|
Packit Service |
158247 |
* @label: label that will be set
|
|
Packit Service |
158247 |
* @error: (out): place to store error (if any)
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Returns: whether the label was successfully set or not
|
|
Packit Service |
158247 |
*
|
|
Packit Service |
158247 |
* Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_SET_LABEL
|
|
Packit Service |
158247 |
*/
|
|
Packit Service |
158247 |
gboolean bd_swap_set_label (const gchar *device, const gchar *label, GError **error) {
|
|
Packit Service |
158247 |
const gchar *argv[5] = {"swaplabel", "-L", label, device, NULL};
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
if (!check_deps (&avail_deps, DEPS_SWAPLABEL_MASK, deps, DEPS_LAST, &deps_check_lock, error))
|
|
Packit Service |
158247 |
return FALSE;
|
|
Packit Service |
158247 |
|
|
Packit Service |
158247 |
return bd_utils_exec_and_report_error (argv, NULL, error);
|
|
Packit Service |
158247 |
}
|