New API: available-all-groups to return list of all optional groups.
[libguestfs.git] / daemon / swap.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "../src/guestfs_protocol.h"
27 #include "daemon.h"
28 #include "actions.h"
29 #include "optgroups.h"
30
31 /* Convenient place to test for the later version of e2fsprogs
32  * and util-linux which supports -U parameters to specify UUIDs.
33  * (Not supported in RHEL 5).
34  */
35 int
36 optgroup_linuxfsuuid_available (void)
37 {
38   char *err;
39   int av;
40
41   /* Ignore return code - mkswap --help *will* fail. */
42   command (NULL, &err, "mkswap", "--help", NULL);
43
44   av = strstr (err, "-U") != NULL;
45   free (err);
46   return av;
47 }
48
49 static int
50 mkswap (const char *device, const char *flag, const char *value)
51 {
52   char *err;
53   int r;
54
55   if (!flag)
56     r = command (NULL, &err, "mkswap", "-f", device, NULL);
57   else
58     r = command (NULL, &err, "mkswap", "-f", flag, value, device, NULL);
59
60   if (r == -1) {
61     reply_with_error ("%s", err);
62     free (err);
63     return -1;
64   }
65
66   free (err);
67
68   return 0;
69 }
70
71 int
72 do_mkswap (const char *device)
73 {
74   return mkswap (device, NULL, NULL);
75 }
76
77 int
78 do_mkswap_L (const char *label, const char *device)
79 {
80   return mkswap (device, "-L", label);
81 }
82
83 int
84 do_mkswap_U (const char *uuid, const char *device)
85 {
86   return mkswap (device, "-U", uuid);
87 }
88
89 int
90 do_mkswap_file (const char *path)
91 {
92   char *buf;
93   int r;
94
95   buf = sysroot_path (path);
96   if (!buf) {
97     reply_with_perror ("malloc");
98     return -1;
99   }
100
101   r = mkswap (buf, NULL, NULL);
102   free (buf);
103   return r;
104 }
105
106 static int
107 swaponoff (const char *cmd, const char *flag, const char *value)
108 {
109   char *err;
110   int r;
111
112   if (!flag)
113     r = command (NULL, &err, cmd, value, NULL);
114   else
115     r = command (NULL, &err, cmd, flag, value, NULL);
116
117   if (r == -1) {
118     reply_with_error ("%s: %s", value, err);
119     free (err);
120     return -1;
121   }
122
123   free (err);
124
125   /* Possible fix for RHBZ#516096.  It probably doesn't hurt to do
126    * this in any case.
127    */
128   udev_settle ();
129
130   return 0;
131 }
132
133 int
134 do_swapon_device (const char *device)
135 {
136   return swaponoff ("swapon", NULL, device);
137 }
138
139 int
140 do_swapoff_device (const char *device)
141 {
142   return swaponoff ("swapoff", NULL, device);
143 }
144
145 int
146 do_swapon_file (const char *path)
147 {
148   char *buf;
149   int r;
150
151   buf = sysroot_path (path);
152   if (!buf) {
153     reply_with_perror ("malloc");
154     return -1;
155   }
156
157   r = swaponoff ("swapon", NULL, buf);
158   free (buf);
159   return r;
160 }
161
162 int
163 do_swapoff_file (const char *path)
164 {
165   char *buf;
166   int r;
167
168   buf = sysroot_path (path);
169   if (!buf) {
170     reply_with_perror ("malloc");
171     return -1;
172   }
173
174   r = swaponoff ("swapoff", NULL, buf);
175   free (buf);
176   return r;
177 }
178
179 int
180 do_swapon_label (const char *label)
181 {
182   return swaponoff ("swapon", "-L", label);
183 }
184
185 int
186 do_swapoff_label (const char *label)
187 {
188   return swaponoff ("swapoff", "-L", label);
189 }
190
191 int
192 do_swapon_uuid (const char *uuid)
193 {
194   return swaponoff ("swapon", "-U", uuid);
195 }
196
197 int
198 do_swapoff_uuid (const char *uuid)
199 {
200   return swaponoff ("swapoff", "-U", uuid);
201 }