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., 675 Mass Ave, Cambridge, MA 02139, 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 /* Ignore return code - mkswap --help *will* fail. */
47 ignore_value (command (NULL, &err, "mkswap", "--help", NULL));
49 av = strstr (err, "-U") != NULL;
55 mkswap (const char *device, const char *flag, const char *value)
61 r = command (NULL, &err, "mkswap", "-f", device, NULL);
63 r = command (NULL, &err, "mkswap", "-f", flag, value, device, NULL);
66 reply_with_error ("%s", err);
77 do_mkswap (const char *device)
79 return mkswap (device, NULL, NULL);
83 do_mkswap_L (const char *label, const char *device)
85 if (strlen (label) > SWAP_LABEL_MAX) {
86 reply_with_error ("%s: Linux swap labels are limited to %d bytes",
87 label, SWAP_LABEL_MAX);
91 return mkswap (device, "-L", label);
95 do_mkswap_U (const char *uuid, const char *device)
97 return mkswap (device, "-U", uuid);
101 do_mkswap_file (const char *path)
106 buf = sysroot_path (path);
108 reply_with_perror ("malloc");
112 r = mkswap (buf, NULL, NULL);
118 swaponoff (const char *cmd, const char *flag, const char *value)
124 r = command (NULL, &err, cmd, value, NULL);
126 r = command (NULL, &err, cmd, flag, value, NULL);
129 reply_with_error ("%s: %s", value, err);
136 /* Possible fix for RHBZ#516096. It probably doesn't hurt to do
145 do_swapon_device (const char *device)
147 return swaponoff ("swapon", NULL, device);
151 do_swapoff_device (const char *device)
153 return swaponoff ("swapoff", NULL, device);
157 do_swapon_file (const char *path)
162 buf = sysroot_path (path);
164 reply_with_perror ("malloc");
168 r = swaponoff ("swapon", NULL, buf);
174 do_swapoff_file (const char *path)
179 buf = sysroot_path (path);
181 reply_with_perror ("malloc");
185 r = swaponoff ("swapoff", NULL, buf);
191 do_swapon_label (const char *label)
193 if (strlen (label) > SWAP_LABEL_MAX) {
194 reply_with_error ("%s: Linux swap labels are limited to %d bytes",
195 label, SWAP_LABEL_MAX);
199 return swaponoff ("swapon", "-L", label);
203 do_swapoff_label (const char *label)
205 if (strlen (label) > SWAP_LABEL_MAX) {
206 reply_with_error ("%s: Linux swap labels are limited to %d bytes",
207 label, SWAP_LABEL_MAX);
211 return swaponoff ("swapoff", "-L", label);
215 do_swapon_uuid (const char *uuid)
217 return swaponoff ("swapon", "-U", uuid);
221 do_swapoff_uuid (const char *uuid)
223 return swaponoff ("swapoff", "-U", uuid);