1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "guestfs_protocol.h"
29 #include "optgroups.h"
31 #include "ignore-value.h"
33 /* Confirmed this is true for Linux swap partitions from the Linux sources. */
34 #define SWAP_LABEL_MAX 16
36 /* Convenient place to test for the later version of e2fsprogs
37 * and util-linux which supports -U parameters to specify UUIDs.
38 * (Not supported in RHEL 5).
41 optgroup_linuxfsuuid_available (void)
46 /* Upstream util-linux have been gradually changing '--help' to go
47 * from stderr to stdout, and changing the return code from 1 to 0.
48 * Thus we need to fold stdout and stderr together, and ignore the
51 ignore_value (commandf (NULL, &err, COMMAND_FLAG_FOLD_STDOUT_ON_STDERR,
52 "mkswap", "--help", NULL));
54 av = strstr (err, "-U") != NULL;
60 mkswap (const char *device, const char *flag, const char *value)
66 r = command (NULL, &err, "mkswap", "-f", device, NULL);
68 r = command (NULL, &err, "mkswap", "-f", flag, value, device, NULL);
71 reply_with_error ("%s", err);
82 do_mkswap (const char *device)
84 return mkswap (device, NULL, NULL);
88 do_mkswap_L (const char *label, const char *device)
90 if (strlen (label) > SWAP_LABEL_MAX) {
91 reply_with_error ("%s: Linux swap labels are limited to %d bytes",
92 label, SWAP_LABEL_MAX);
96 return mkswap (device, "-L", label);
100 do_mkswap_U (const char *uuid, const char *device)
102 return mkswap (device, "-U", uuid);
106 do_mkswap_file (const char *path)
111 buf = sysroot_path (path);
113 reply_with_perror ("malloc");
117 r = mkswap (buf, NULL, NULL);
123 swaponoff (const char *cmd, const char *flag, const char *value)
129 r = command (NULL, &err, cmd, value, NULL);
131 r = command (NULL, &err, cmd, flag, value, NULL);
134 reply_with_error ("%s: %s", value, err);
141 /* Possible fix for RHBZ#516096. It probably doesn't hurt to do
150 do_swapon_device (const char *device)
152 return swaponoff ("swapon", NULL, device);
156 do_swapoff_device (const char *device)
158 return swaponoff ("swapoff", NULL, device);
162 do_swapon_file (const char *path)
167 buf = sysroot_path (path);
169 reply_with_perror ("malloc");
173 r = swaponoff ("swapon", NULL, buf);
179 do_swapoff_file (const char *path)
184 buf = sysroot_path (path);
186 reply_with_perror ("malloc");
190 r = swaponoff ("swapoff", NULL, buf);
196 do_swapon_label (const char *label)
198 if (strlen (label) > SWAP_LABEL_MAX) {
199 reply_with_error ("%s: Linux swap labels are limited to %d bytes",
200 label, SWAP_LABEL_MAX);
204 return swaponoff ("swapon", "-L", label);
208 do_swapoff_label (const char *label)
210 if (strlen (label) > SWAP_LABEL_MAX) {
211 reply_with_error ("%s: Linux swap labels are limited to %d bytes",
212 label, SWAP_LABEL_MAX);
216 return swaponoff ("swapoff", "-L", label);
220 do_swapon_uuid (const char *uuid)
222 return swaponoff ("swapon", "-U", uuid);
226 do_swapoff_uuid (const char *uuid)
228 return swaponoff ("swapoff", "-U", uuid);