daemon: Split out checksum type to program name mapping into function.
[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   return 0;
126 }
127
128 int
129 do_swapon_device (const char *device)
130 {
131   return swaponoff ("swapon", NULL, device);
132 }
133
134 int
135 do_swapoff_device (const char *device)
136 {
137   return swaponoff ("swapoff", NULL, device);
138 }
139
140 int
141 do_swapon_file (const char *path)
142 {
143   char *buf;
144   int r;
145
146   buf = sysroot_path (path);
147   if (!buf) {
148     reply_with_perror ("malloc");
149     return -1;
150   }
151
152   r = swaponoff ("swapon", NULL, buf);
153   free (buf);
154   return r;
155 }
156
157 int
158 do_swapoff_file (const char *path)
159 {
160   char *buf;
161   int r;
162
163   buf = sysroot_path (path);
164   if (!buf) {
165     reply_with_perror ("malloc");
166     return -1;
167   }
168
169   r = swaponoff ("swapoff", NULL, buf);
170   free (buf);
171   return r;
172 }
173
174 int
175 do_swapon_label (const char *label)
176 {
177   return swaponoff ("swapon", "-L", label);
178 }
179
180 int
181 do_swapoff_label (const char *label)
182 {
183   return swaponoff ("swapoff", "-L", label);
184 }
185
186 int
187 do_swapon_uuid (const char *uuid)
188 {
189   return swaponoff ("swapon", "-U", uuid);
190 }
191
192 int
193 do_swapoff_uuid (const char *uuid)
194 {
195   return swaponoff ("swapoff", "-U", uuid);
196 }