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 "../src/guestfs_protocol.h"
29 #include "optgroups.h"
31 /* Confirmed this is true for Linux swap partitions from the Linux sources. */
32 #define SWAP_LABEL_MAX 16
34 /* Convenient place to test for the later version of e2fsprogs
35 * and util-linux which supports -U parameters to specify UUIDs.
36 * (Not supported in RHEL 5).
39 optgroup_linuxfsuuid_available (void)
44 /* Ignore return code - mkswap --help *will* fail. */
45 command (NULL, &err, "mkswap", "--help", NULL);
47 av = strstr (err, "-U") != NULL;
53 mkswap (const char *device, const char *flag, const char *value)
59 r = command (NULL, &err, "mkswap", "-f", device, NULL);
61 r = command (NULL, &err, "mkswap", "-f", flag, value, device, NULL);
64 reply_with_error ("%s", err);
75 do_mkswap (const char *device)
77 return mkswap (device, NULL, NULL);
81 do_mkswap_L (const char *label, const char *device)
83 if (strlen (label) > SWAP_LABEL_MAX) {
84 reply_with_error ("%s: Linux swap labels are limited to %d bytes",
85 label, SWAP_LABEL_MAX);
89 return mkswap (device, "-L", label);
93 do_mkswap_U (const char *uuid, const char *device)
95 return mkswap (device, "-U", uuid);
99 do_mkswap_file (const char *path)
104 buf = sysroot_path (path);
106 reply_with_perror ("malloc");
110 r = mkswap (buf, NULL, NULL);
116 swaponoff (const char *cmd, const char *flag, const char *value)
122 r = command (NULL, &err, cmd, value, NULL);
124 r = command (NULL, &err, cmd, flag, value, NULL);
127 reply_with_error ("%s: %s", value, err);
134 /* Possible fix for RHBZ#516096. It probably doesn't hurt to do
143 do_swapon_device (const char *device)
145 return swaponoff ("swapon", NULL, device);
149 do_swapoff_device (const char *device)
151 return swaponoff ("swapoff", NULL, device);
155 do_swapon_file (const char *path)
160 buf = sysroot_path (path);
162 reply_with_perror ("malloc");
166 r = swaponoff ("swapon", NULL, buf);
172 do_swapoff_file (const char *path)
177 buf = sysroot_path (path);
179 reply_with_perror ("malloc");
183 r = swaponoff ("swapoff", NULL, buf);
189 do_swapon_label (const char *label)
191 if (strlen (label) > SWAP_LABEL_MAX) {
192 reply_with_error ("%s: Linux swap labels are limited to %d bytes",
193 label, SWAP_LABEL_MAX);
197 return swaponoff ("swapon", "-L", label);
201 do_swapoff_label (const char *label)
203 if (strlen (label) > SWAP_LABEL_MAX) {
204 reply_with_error ("%s: Linux swap labels are limited to %d bytes",
205 label, SWAP_LABEL_MAX);
209 return swaponoff ("swapoff", "-L", label);
213 do_swapon_uuid (const char *uuid)
215 return swaponoff ("swapon", "-U", uuid);
219 do_swapoff_uuid (const char *uuid)
221 return swaponoff ("swapoff", "-U", uuid);