Add progress messages to zero-device command.
[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 /* Confirmed this is true for Linux swap partitions from the Linux sources. */
32 #define SWAP_LABEL_MAX 16
33
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).
37  */
38 int
39 optgroup_linuxfsuuid_available (void)
40 {
41   char *err;
42   int av;
43
44   /* Ignore return code - mkswap --help *will* fail. */
45   command (NULL, &err, "mkswap", "--help", NULL);
46
47   av = strstr (err, "-U") != NULL;
48   free (err);
49   return av;
50 }
51
52 static int
53 mkswap (const char *device, const char *flag, const char *value)
54 {
55   char *err;
56   int r;
57
58   if (!flag)
59     r = command (NULL, &err, "mkswap", "-f", device, NULL);
60   else
61     r = command (NULL, &err, "mkswap", "-f", flag, value, device, NULL);
62
63   if (r == -1) {
64     reply_with_error ("%s", err);
65     free (err);
66     return -1;
67   }
68
69   free (err);
70
71   return 0;
72 }
73
74 int
75 do_mkswap (const char *device)
76 {
77   return mkswap (device, NULL, NULL);
78 }
79
80 int
81 do_mkswap_L (const char *label, const char *device)
82 {
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);
86     return -1;
87   }
88
89   return mkswap (device, "-L", label);
90 }
91
92 int
93 do_mkswap_U (const char *uuid, const char *device)
94 {
95   return mkswap (device, "-U", uuid);
96 }
97
98 int
99 do_mkswap_file (const char *path)
100 {
101   char *buf;
102   int r;
103
104   buf = sysroot_path (path);
105   if (!buf) {
106     reply_with_perror ("malloc");
107     return -1;
108   }
109
110   r = mkswap (buf, NULL, NULL);
111   free (buf);
112   return r;
113 }
114
115 static int
116 swaponoff (const char *cmd, const char *flag, const char *value)
117 {
118   char *err;
119   int r;
120
121   if (!flag)
122     r = command (NULL, &err, cmd, value, NULL);
123   else
124     r = command (NULL, &err, cmd, flag, value, NULL);
125
126   if (r == -1) {
127     reply_with_error ("%s: %s", value, err);
128     free (err);
129     return -1;
130   }
131
132   free (err);
133
134   /* Possible fix for RHBZ#516096.  It probably doesn't hurt to do
135    * this in any case.
136    */
137   udev_settle ();
138
139   return 0;
140 }
141
142 int
143 do_swapon_device (const char *device)
144 {
145   return swaponoff ("swapon", NULL, device);
146 }
147
148 int
149 do_swapoff_device (const char *device)
150 {
151   return swaponoff ("swapoff", NULL, device);
152 }
153
154 int
155 do_swapon_file (const char *path)
156 {
157   char *buf;
158   int r;
159
160   buf = sysroot_path (path);
161   if (!buf) {
162     reply_with_perror ("malloc");
163     return -1;
164   }
165
166   r = swaponoff ("swapon", NULL, buf);
167   free (buf);
168   return r;
169 }
170
171 int
172 do_swapoff_file (const char *path)
173 {
174   char *buf;
175   int r;
176
177   buf = sysroot_path (path);
178   if (!buf) {
179     reply_with_perror ("malloc");
180     return -1;
181   }
182
183   r = swaponoff ("swapoff", NULL, buf);
184   free (buf);
185   return r;
186 }
187
188 int
189 do_swapon_label (const char *label)
190 {
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);
194     return -1;
195   }
196
197   return swaponoff ("swapon", "-L", label);
198 }
199
200 int
201 do_swapoff_label (const char *label)
202 {
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);
206     return -1;
207   }
208
209   return swaponoff ("swapoff", "-L", label);
210 }
211
212 int
213 do_swapon_uuid (const char *uuid)
214 {
215   return swaponoff ("swapon", "-U", uuid);
216 }
217
218 int
219 do_swapoff_uuid (const char *uuid)
220 {
221   return swaponoff ("swapoff", "-U", uuid);
222 }