| 1 |
/* |
| 2 |
* Copyright (C) 2014-2017 Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2.1 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
* Author: Vratislav Podzimek <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <glib.h> |
| 21 |
#include <string.h> |
| 22 |
#include <unistd.h> |
| 23 |
#include <sys/swap.h> |
| 24 |
#include <fcntl.h> |
| 25 |
#include <blkid.h> |
| 26 |
#include <uuid.h> |
| 27 |
#include <blockdev/utils.h> |
| 28 |
|
| 29 |
#include "swap.h" |
| 30 |
#include "check_deps.h" |
| 31 |
|
| 32 |
#define MKSWAP_MIN_VERSION "2.23.2" |
| 33 |
|
| 34 |
/** |
| 35 |
* SECTION: swap |
| 36 |
* @short_description: plugin for operations with swap space |
| 37 |
* @title: Swap |
| 38 |
* @include: swap.h |
| 39 |
* |
| 40 |
* A plugin for operations with swap space. |
| 41 |
*/ |
| 42 |
|
| 43 |
/** |
| 44 |
* bd_swap_error_quark: (skip) |
| 45 |
*/ |
| 46 |
GQuark bd_swap_error_quark (void) |
| 47 |
{ |
| 48 |
return g_quark_from_static_string ("g-bd-swap-error-quark"); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
static volatile guint avail_deps = 0; |
| 53 |
static GMutex deps_check_lock; |
| 54 |
|
| 55 |
#define DEPS_MKSWAP 0 |
| 56 |
#define DEPS_MKSWAP_MASK (1 << DEPS_MKSWAP) |
| 57 |
#define DEPS_SWAPLABEL 1 |
| 58 |
#define DEPS_SWAPLABEL_MASK (1 << DEPS_SWAPLABEL) |
| 59 |
#define DEPS_LAST 2 |
| 60 |
|
| 61 |
static const UtilDep deps[DEPS_LAST] = { |
| 62 |
{"mkswap", MKSWAP_MIN_VERSION, NULL, "mkswap from util-linux ([\\d\\.]+)"}, |
| 63 |
{"swaplabel", NULL, NULL, NULL}, |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
/** |
| 68 |
* bd_swap_init: |
| 69 |
* |
| 70 |
* Initializes the plugin. **This function is called automatically by the |
| 71 |
* library's initialization functions.** |
| 72 |
* |
| 73 |
*/ |
| 74 |
gboolean bd_swap_init (void) { |
| 75 |
/* nothing to do here */ |
| 76 |
return TRUE; |
| 77 |
}; |
| 78 |
|
| 79 |
/** |
| 80 |
* bd_swap_close: |
| 81 |
* |
| 82 |
* Cleans up after the plugin. **This function is called automatically by the |
| 83 |
* library's functions that unload it.** |
| 84 |
* |
| 85 |
*/ |
| 86 |
void bd_swap_close (void) { |
| 87 |
g_atomic_int_set (&avail_deps, 0); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* bd_swap_is_tech_avail: |
| 93 |
* @tech: the queried tech |
| 94 |
* @mode: a bit mask of queried modes of operation (#BDSwapTechMode) for @tech |
| 95 |
* @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available) |
| 96 |
* |
| 97 |
* Returns: whether the @tech-@mode combination is available -- supported by the |
| 98 |
* plugin implementation and having all the runtime dependencies available |
| 99 |
*/ |
| 100 |
gboolean bd_swap_is_tech_avail (BDSwapTech tech G_GNUC_UNUSED, guint64 mode, GError **error) { |
| 101 |
guint32 requires = 0; |
| 102 |
if (mode & BD_SWAP_TECH_MODE_CREATE) |
| 103 |
requires |= DEPS_MKSWAP_MASK; |
| 104 |
if (mode & BD_SWAP_TECH_MODE_SET_LABEL) |
| 105 |
requires |= DEPS_SWAPLABEL_MASK; |
| 106 |
if (mode & BD_SWAP_TECH_MODE_SET_UUID) |
| 107 |
requires |= DEPS_SWAPLABEL_MASK; |
| 108 |
|
| 109 |
return check_deps (&avail_deps, requires, deps, DEPS_LAST, &deps_check_lock, error); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* bd_swap_mkswap: |
| 114 |
* @device: a device to create swap space on |
| 115 |
* @label: (nullable): a label for the swap space device |
| 116 |
* @uuid: (nullable): UUID for the swap space device |
| 117 |
* @extra: (nullable) (array zero-terminated=1): extra options for the creation (right now |
| 118 |
* passed to the 'mkswap' utility) |
| 119 |
* @error: (out) (optional): place to store error (if any) |
| 120 |
* |
| 121 |
* Returns: whether the swap space was successfully created or not |
| 122 |
* |
| 123 |
* Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_CREATE |
| 124 |
*/ |
| 125 |
gboolean bd_swap_mkswap (const gchar *device, const gchar *label, const gchar *uuid, const BDExtraArg **extra, GError **error) { |
| 126 |
guint8 next_arg = 2; |
| 127 |
|
| 128 |
if (!check_deps (&avail_deps, DEPS_MKSWAP_MASK, deps, DEPS_LAST, &deps_check_lock, error)) |
| 129 |
return FALSE; |
| 130 |
|
| 131 |
/* We use -f to force since mkswap tends to refuse creation on lvs with |
| 132 |
a message about erasing bootbits sectors on whole disks. Bah. */ |
| 133 |
const gchar *argv[8] = {"mkswap", "-f", NULL, NULL, NULL, NULL, NULL, NULL}; |
| 134 |
|
| 135 |
if (label) { |
| 136 |
argv[next_arg] = "-L"; |
| 137 |
next_arg++; |
| 138 |
argv[next_arg] = label; |
| 139 |
next_arg++; |
| 140 |
} |
| 141 |
|
| 142 |
if (uuid) { |
| 143 |
argv[next_arg] = "-U"; |
| 144 |
next_arg++; |
| 145 |
argv[next_arg] = uuid; |
| 146 |
next_arg++; |
| 147 |
} |
| 148 |
|
| 149 |
argv[next_arg] = device; |
| 150 |
|
| 151 |
return bd_utils_exec_and_report_error (argv, extra, error); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* bd_swap_swapon: |
| 156 |
* @device: swap device to activate |
| 157 |
* @priority: priority of the activated device or -1 to use the default |
| 158 |
* @error: (out) (optional): place to store error (if any) |
| 159 |
* |
| 160 |
* Returns: whether the swap device was successfully activated or not |
| 161 |
* |
| 162 |
* Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_ACTIVATE_DEACTIVATE |
| 163 |
*/ |
| 164 |
gboolean bd_swap_swapon (const gchar *device, gint priority, GError **error) { |
| 165 |
blkid_probe probe = NULL; |
| 166 |
gint fd = 0; |
| 167 |
gint status = 0; |
| 168 |
guint n_try = 0; |
| 169 |
const gchar *value = NULL; |
| 170 |
gint64 status_len = 0; |
| 171 |
gint64 swap_pagesize = 0; |
| 172 |
gint64 sys_pagesize = 0; |
| 173 |
gint flags = 0; |
| 174 |
gint ret = 0; |
| 175 |
guint64 progress_id = 0; |
| 176 |
gchar *msg = NULL; |
| 177 |
GError *l_error = NULL; |
| 178 |
|
| 179 |
msg = g_strdup_printf ("Started 'swapon %s'", device); |
| 180 |
progress_id = bd_utils_report_started (msg); |
| 181 |
g_free (msg); |
| 182 |
|
| 183 |
bd_utils_report_progress (progress_id, 0, "Analysing the swap device"); |
| 184 |
/* check the device if it is an activatable swap */ |
| 185 |
probe = blkid_new_probe (); |
| 186 |
if (!probe) { |
| 187 |
g_set_error_literal (&l_error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE, |
| 188 |
"Failed to create a new probe"); |
| 189 |
bd_utils_report_finished (progress_id, l_error->message); |
| 190 |
g_propagate_error (error, l_error); |
| 191 |
return FALSE; |
| 192 |
} |
| 193 |
|
| 194 |
fd = open (device, O_RDONLY|O_CLOEXEC); |
| 195 |
if (fd == -1) { |
| 196 |
g_set_error (&l_error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE, |
| 197 |
"Failed to open the device '%s'", device); |
| 198 |
bd_utils_report_finished (progress_id, l_error->message); |
| 199 |
g_propagate_error (error, l_error); |
| 200 |
blkid_free_probe (probe); |
| 201 |
return FALSE; |
| 202 |
} |
| 203 |
|
| 204 |
/* we may need to try multiple times with some delays in case the device is |
| 205 |
busy at the very moment */ |
| 206 |
for (n_try=5, status=-1; (status != 0) && (n_try > 0); n_try--) { |
| 207 |
status = blkid_probe_set_device (probe, fd, 0, 0); |
| 208 |
if (status != 0) |
| 209 |
g_usleep (100 * 1000); /* microseconds */ |
| 210 |
} |
| 211 |
if (status != 0) { |
| 212 |
g_set_error (&l_error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE, |
| 213 |
"Failed to create a probe for the device '%s'", device); |
| 214 |
bd_utils_report_finished (progress_id, l_error->message); |
| 215 |
g_propagate_error (error, l_error); |
| 216 |
blkid_free_probe (probe); |
| 217 |
close (fd); |
| 218 |
return FALSE; |
| 219 |
} |
| 220 |
|
| 221 |
blkid_probe_enable_superblocks (probe, 1); |
| 222 |
blkid_probe_set_superblocks_flags (probe, BLKID_SUBLKS_TYPE | BLKID_SUBLKS_MAGIC); |
| 223 |
|
| 224 |
/* we may need to try multiple times with some delays in case the device is |
| 225 |
busy at the very moment */ |
| 226 |
for (n_try=5, status=-1; !(status == 0 || status == 1) && (n_try > 0); n_try--) { |
| 227 |
status = blkid_do_safeprobe (probe); |
| 228 |
if (status < 0) |
| 229 |
g_usleep (100 * 1000); /* microseconds */ |
| 230 |
} |
| 231 |
if (status < 0) { |
| 232 |
/* -1 or -2 = error during probing*/ |
| 233 |
g_set_error (&l_error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE, |
| 234 |
"Failed to probe the device '%s'", device); |
| 235 |
bd_utils_report_finished (progress_id, l_error->message); |
| 236 |
g_propagate_error (error, l_error); |
| 237 |
blkid_free_probe (probe); |
| 238 |
close (fd); |
| 239 |
return FALSE; |
| 240 |
} else if (status == 1) { |
| 241 |
/* 1 = nothing detected */ |
| 242 |
g_set_error (&l_error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE, |
| 243 |
"No superblock detected on the device '%s'", device); |
| 244 |
bd_utils_report_finished (progress_id, l_error->message); |
| 245 |
g_propagate_error (error, l_error); |
| 246 |
blkid_free_probe (probe); |
| 247 |
close (fd); |
| 248 |
return FALSE; |
| 249 |
} |
| 250 |
|
| 251 |
status = blkid_probe_lookup_value (probe, "TYPE", &value, NULL); |
| 252 |
if (status != 0) { |
| 253 |
g_set_error (&l_error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE, |
| 254 |
"Failed to get format type for the device '%s'", device); |
| 255 |
bd_utils_report_finished (progress_id, l_error->message); |
| 256 |
g_propagate_error (error, l_error); |
| 257 |
blkid_free_probe (probe); |
| 258 |
close (fd); |
| 259 |
return FALSE; |
| 260 |
} |
| 261 |
|
| 262 |
if (g_strcmp0 (value, "swap") != 0) { |
| 263 |
g_set_error (&l_error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE, |
| 264 |
"Device '%s' is not formatted as swap", device); |
| 265 |
bd_utils_report_finished (progress_id, l_error->message); |
| 266 |
g_propagate_error (error, l_error); |
| 267 |
blkid_free_probe (probe); |
| 268 |
close (fd); |
| 269 |
return FALSE; |
| 270 |
} |
| 271 |
|
| 272 |
status = blkid_probe_lookup_value (probe, "SBMAGIC", &value, NULL); |
| 273 |
if (status != 0) { |
| 274 |
g_set_error (&l_error, BD_SWAP_ERROR, BD_SWAP_ERROR_UNKNOWN_STATE, |
| 275 |
"Failed to get swap status on the device '%s'", device); |
| 276 |
bd_utils_report_finished (progress_id, l_error->message); |
| 277 |
g_propagate_error (error, l_error); |
| 278 |
blkid_free_probe (probe); |
| 279 |
close (fd); |
| 280 |
return FALSE; |
| 281 |
} |
| 282 |
|
| 283 |
if (g_strcmp0 (value, "SWAP-SPACE") == 0) { |
| 284 |
g_set_error_literal (&l_error, BD_SWAP_ERROR, BD_SWAP_ERROR_ACTIVATE_OLD, |
| 285 |
"Old swap format, cannot activate."); |
| 286 |
bd_utils_report_finished (progress_id, l_error->message); |
| 287 |
g_propagate_error (error, l_error); |
| 288 |
blkid_free_probe (probe); |
| 289 |
close (fd); |
| 290 |
return FALSE; |
| 291 |
} else if (g_strcmp0 (value, "S1SUSPEND") == 0 || g_strcmp0 (value, "S2SUSPEND") == 0) { |
| 292 |
g_set_error_literal (&l_error, BD_SWAP_ERROR, BD_SWAP_ERROR_ACTIVATE_SUSPEND, |
| 293 |
"Suspended system on the swap device, cannot activate."); |
| 294 |
bd_utils_report_finished (progress_id, l_error->message); |
| 295 |
g_propagate_error (error, l_error); |
| 296 |
blkid_free_probe (probe); |
| 297 |
close (fd); |
| 298 |
return FALSE; |
| 299 |
} else if (g_strcmp0 (value, "SWAPSPACE2") != 0) { |
| 300 |
g_set_error_literal (&l_error, BD_SWAP_ERROR, BD_SWAP_ERROR_ACTIVATE_UNKNOWN, |
| 301 |
"Unknown swap space format, cannot activate."); |
| 302 |
bd_utils_report_finished (progress_id, l_error->message); |
| 303 |
g_propagate_error (error, l_error); |
| 304 |
blkid_free_probe (probe); |
| 305 |
close (fd); |
| 306 |
return FALSE; |
| 307 |
} |
| 308 |
|
| 309 |
status_len = (gint64) strlen (value); |
| 310 |
|
| 311 |
status = blkid_probe_lookup_value (probe, "SBMAGIC_OFFSET", &value, NULL); |
| 312 |
if (status != 0 || !value) { |
| 313 |
g_set_error (&l_error, BD_SWAP_ERROR, BD_SWAP_ERROR_ACTIVATE_PAGESIZE, |
| 314 |
"Failed to get swap status on the device '%s'", device); |
| 315 |
bd_utils_report_finished (progress_id, l_error->message); |
| 316 |
g_propagate_error (error, l_error); |
| 317 |
blkid_free_probe (probe); |
| 318 |
close (fd); |
| 319 |
return FALSE; |
| 320 |
} |
| 321 |
|
| 322 |
swap_pagesize = status_len + g_ascii_strtoll (value, (char **)NULL, 10); |
| 323 |
|
| 324 |
blkid_free_probe (probe); |
| 325 |
close (fd); |
| 326 |
|
| 327 |
sys_pagesize = getpagesize (); |
| 328 |
|
| 329 |
if (swap_pagesize != sys_pagesize) { |
| 330 |
g_set_error (&l_error, BD_SWAP_ERROR, BD_SWAP_ERROR_ACTIVATE_PAGESIZE, |
| 331 |
"Swap format pagesize (%"G_GINT64_FORMAT") and system pagesize (%"G_GINT64_FORMAT") don't match", |
| 332 |
swap_pagesize, sys_pagesize); |
| 333 |
bd_utils_report_finished (progress_id, l_error->message); |
| 334 |
g_propagate_error (error, l_error); |
| 335 |
return FALSE; |
| 336 |
} |
| 337 |
|
| 338 |
bd_utils_report_progress (progress_id, 10, "Swap device analysed, enabling"); |
| 339 |
if (priority >= 0) { |
| 340 |
flags = SWAP_FLAG_PREFER; |
| 341 |
flags |= (priority << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK; |
| 342 |
} |
| 343 |
|
| 344 |
ret = swapon (device, flags); |
| 345 |
if (ret != 0) { |
| 346 |
g_set_error (&l_error, BD_SWAP_ERROR, BD_SWAP_ERROR_ACTIVATE, |
| 347 |
"Failed to activate swap on %s: %m", device); |
| 348 |
bd_utils_report_finished (progress_id, l_error->message); |
| 349 |
g_propagate_error (error, l_error); |
| 350 |
return FALSE; |
| 351 |
} |
| 352 |
|
| 353 |
bd_utils_report_finished (progress_id, "Completed"); |
| 354 |
return TRUE; |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* bd_swap_swapoff: |
| 359 |
* @device: swap device to deactivate |
| 360 |
* @error: (out) (optional): place to store error (if any) |
| 361 |
* |
| 362 |
* Returns: whether the swap device was successfully deactivated or not |
| 363 |
* |
| 364 |
* Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_ACTIVATE_DEACTIVATE |
| 365 |
*/ |
| 366 |
gboolean bd_swap_swapoff (const gchar *device, GError **error) { |
| 367 |
gint ret = 0; |
| 368 |
guint64 progress_id = 0; |
| 369 |
gchar *msg = NULL; |
| 370 |
GError *l_error = NULL; |
| 371 |
|
| 372 |
msg = g_strdup_printf ("Started 'swapoff %s'", device); |
| 373 |
progress_id = bd_utils_report_started (msg); |
| 374 |
g_free (msg); |
| 375 |
|
| 376 |
ret = swapoff (device); |
| 377 |
if (ret != 0) { |
| 378 |
g_set_error (&l_error, BD_SWAP_ERROR, BD_SWAP_ERROR_ACTIVATE, |
| 379 |
"Failed to deactivate swap on %s: %m", device); |
| 380 |
bd_utils_report_finished (progress_id, l_error->message); |
| 381 |
g_propagate_error (error, l_error); |
| 382 |
return FALSE; |
| 383 |
} |
| 384 |
|
| 385 |
bd_utils_report_finished (progress_id, "Completed"); |
| 386 |
return TRUE; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* bd_swap_swapstatus: |
| 391 |
* @device: swap device to get status of |
| 392 |
* @error: (out) (optional): place to store error (if any) |
| 393 |
* |
| 394 |
* Returns: %TRUE if the swap device is active, %FALSE if not active or failed |
| 395 |
* to determine (@error) is set not a non-NULL value in such case) |
| 396 |
* |
| 397 |
* Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_QUERY |
| 398 |
*/ |
| 399 |
gboolean bd_swap_swapstatus (const gchar *device, GError **error) { |
| 400 |
gchar *file_content = NULL; |
| 401 |
gchar *real_device = NULL; |
| 402 |
gsize length; |
| 403 |
gchar *next_line; |
| 404 |
gboolean success; |
| 405 |
|
| 406 |
success = g_file_get_contents ("/proc/swaps", &file_content, &length, error); |
| 407 |
if (!success) { |
| 408 |
/* error is already populated */ |
| 409 |
return FALSE; |
| 410 |
} |
| 411 |
|
| 412 |
/* get the real device node for device-mapper devices since the ones |
| 413 |
with meaningful names are just dev_paths */ |
| 414 |
if (g_str_has_prefix (device, "/dev/mapper/") || g_str_has_prefix (device, "/dev/md/")) { |
| 415 |
real_device = bd_utils_resolve_device (device, NULL); |
| 416 |
if (!real_device) { |
| 417 |
/* the device doesn't exist and thus is not an active swap */ |
| 418 |
g_free (file_content); |
| 419 |
return FALSE; |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
if (g_str_has_prefix (file_content, real_device ? real_device : device)) { |
| 424 |
g_free (real_device); |
| 425 |
g_free (file_content); |
| 426 |
return TRUE; |
| 427 |
} |
| 428 |
|
| 429 |
next_line = strchr (file_content, '\n'); |
| 430 |
while (next_line && ((gsize)(next_line - file_content) < length)) { |
| 431 |
next_line++; |
| 432 |
if (g_str_has_prefix (next_line, real_device ? real_device : device)) { |
| 433 |
g_free (real_device); |
| 434 |
g_free (file_content); |
| 435 |
return TRUE; |
| 436 |
} |
| 437 |
|
| 438 |
next_line = strchr (next_line, '\n'); |
| 439 |
} |
| 440 |
|
| 441 |
g_free (real_device); |
| 442 |
g_free (file_content); |
| 443 |
return FALSE; |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* bd_swap_check_label: |
| 448 |
* @label: label to check |
| 449 |
* @error: (out) (optional): place to store error |
| 450 |
* |
| 451 |
* Returns: whether @label is a valid label for swap or not |
| 452 |
* (reason is provided in @error) |
| 453 |
* |
| 454 |
* Tech category: always available |
| 455 |
*/ |
| 456 |
gboolean bd_swap_check_label (const gchar *label, GError **error) { |
| 457 |
if (strlen (label) > 16) { |
| 458 |
g_set_error_literal (error, BD_SWAP_ERROR, BD_SWAP_ERROR_LABEL_INVALID, |
| 459 |
"Label for swap must be at most 16 characters long."); |
| 460 |
return FALSE; |
| 461 |
} |
| 462 |
|
| 463 |
return TRUE; |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* bd_swap_set_label: |
| 468 |
* @device: a device to set label on |
| 469 |
* @label: label that will be set |
| 470 |
* @error: (out) (optional): place to store error (if any) |
| 471 |
* |
| 472 |
* Returns: whether the label was successfully set or not |
| 473 |
* |
| 474 |
* Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_SET_LABEL |
| 475 |
*/ |
| 476 |
gboolean bd_swap_set_label (const gchar *device, const gchar *label, GError **error) { |
| 477 |
const gchar *argv[5] = {"swaplabel", "-L", label, device, NULL}; |
| 478 |
|
| 479 |
if (!check_deps (&avail_deps, DEPS_SWAPLABEL_MASK, deps, DEPS_LAST, &deps_check_lock, error)) |
| 480 |
return FALSE; |
| 481 |
|
| 482 |
return bd_utils_exec_and_report_error (argv, NULL, error); |
| 483 |
} |
| 484 |
|
| 485 |
/** |
| 486 |
* bd_swap_check_uuid: |
| 487 |
* @uuid: UUID to check |
| 488 |
* @error: (out) (optional): place to store error |
| 489 |
* |
| 490 |
* Returns: whether @uuid is a valid UUID for swap or not |
| 491 |
* (reason is provided in @error) |
| 492 |
* |
| 493 |
* Tech category: always available |
| 494 |
*/ |
| 495 |
gboolean bd_swap_check_uuid (const gchar *uuid, GError **error) { |
| 496 |
g_autofree gchar *lowercase = NULL; |
| 497 |
gint ret = 0; |
| 498 |
uuid_t uu; |
| 499 |
|
| 500 |
if (!g_str_is_ascii (uuid)) { |
| 501 |
g_set_error_literal (error, BD_SWAP_ERROR, BD_SWAP_ERROR_UUID_INVALID, |
| 502 |
"Provided UUID is not a valid RFC-4122 UUID."); |
| 503 |
return FALSE; |
| 504 |
} |
| 505 |
|
| 506 |
lowercase = g_ascii_strdown (uuid, -1); |
| 507 |
ret = uuid_parse (lowercase, uu); |
| 508 |
if (ret < 0){ |
| 509 |
g_set_error_literal (error, BD_SWAP_ERROR, BD_SWAP_ERROR_UUID_INVALID, |
| 510 |
"Provided UUID is not a valid RFC-4122 UUID."); |
| 511 |
return FALSE; |
| 512 |
} |
| 513 |
|
| 514 |
return TRUE; |
| 515 |
} |
| 516 |
|
| 517 |
/** |
| 518 |
* bd_swap_set_uuid: |
| 519 |
* @device: a device to set UUID on |
| 520 |
* @uuid: UUID that will be set |
| 521 |
* @error: (out) (optional): place to store error (if any) |
| 522 |
* |
| 523 |
* Returns: whether the UUID was successfully set or not |
| 524 |
* |
| 525 |
* Tech category: %BD_SWAP_TECH_SWAP-%BD_SWAP_TECH_MODE_SET_UUID |
| 526 |
*/ |
| 527 |
gboolean bd_swap_set_uuid (const gchar *device, const gchar *uuid, GError **error) { |
| 528 |
const gchar *argv[5] = {"swaplabel", "-U", uuid, device, NULL}; |
| 529 |
|
| 530 |
if (!check_deps (&avail_deps, DEPS_SWAPLABEL_MASK, deps, DEPS_LAST, &deps_check_lock, error)) |
| 531 |
return FALSE; |
| 532 |
|
| 533 |
return bd_utils_exec_and_report_error (argv, NULL, error); |
| 534 |
} |