generator.ml: use new "Pathname" designation
[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
30 static int
31 mkswap (const char *device, const char *flag, const char *value)
32 {
33   char *err;
34   int r;
35
36   if (!flag)
37     r = command (NULL, &err, "/sbin/mkswap", "-f", device, NULL);
38   else
39     r = command (NULL, &err, "/sbin/mkswap", "-f", flag, value, device, NULL);
40
41   if (r == -1) {
42     reply_with_error ("mkswap: %s", err);
43     free (err);
44     return -1;
45   }
46
47   free (err);
48
49   return 0;
50 }
51
52 int
53 do_mkswap (const char *device)
54 {
55   return mkswap (device, NULL, NULL);
56 }
57
58 int
59 do_mkswap_L (const char *label, const char *device)
60 {
61   return mkswap (device, "-L", label);
62 }
63
64 int
65 do_mkswap_U (const char *uuid, const char *device)
66 {
67   return mkswap (device, "-U", uuid);
68 }
69
70 int
71 do_mkswap_file (const char *path)
72 {
73   char *buf;
74   int r;
75
76   buf = sysroot_path (path);
77   if (!buf) {
78     reply_with_perror ("malloc");
79     return -1;
80   }
81
82   r = mkswap (buf, NULL, NULL);
83   free (buf);
84   return r;
85 }
86
87 static int
88 swaponoff (const char *cmd, const char *flag, const char *value)
89 {
90   char *err;
91   int r;
92
93   if (!flag)
94     r = command (NULL, &err, cmd, value, NULL);
95   else
96     r = command (NULL, &err, cmd, flag, value, NULL);
97
98   if (r == -1) {
99     reply_with_error ("%s: %s: %s", cmd, value, err);
100     free (err);
101     return -1;
102   }
103
104   free (err);
105
106   return 0;
107 }
108
109 int
110 do_swapon_device (const char *device)
111 {
112   return swaponoff ("/sbin/swapon", NULL, device);
113 }
114
115 int
116 do_swapoff_device (const char *device)
117 {
118   return swaponoff ("/sbin/swapoff", NULL, device);
119 }
120
121 int
122 do_swapon_file (const char *path)
123 {
124   char *buf;
125   int r;
126
127   buf = sysroot_path (path);
128   if (!buf) {
129     reply_with_perror ("malloc");
130     return -1;
131   }
132
133   r = swaponoff ("/sbin/swapon", NULL, buf);
134   free (buf);
135   return r;
136 }
137
138 int
139 do_swapoff_file (const char *path)
140 {
141   char *buf;
142   int r;
143
144   buf = sysroot_path (path);
145   if (!buf) {
146     reply_with_perror ("malloc");
147     return -1;
148   }
149
150   r = swaponoff ("/sbin/swapoff", NULL, buf);
151   free (buf);
152   return r;
153 }
154
155 int
156 do_swapon_label (const char *label)
157 {
158   return swaponoff ("/sbin/swapon", "-L", label);
159 }
160
161 int
162 do_swapoff_label (const char *label)
163 {
164   return swaponoff ("/sbin/swapoff", "-L", label);
165 }
166
167 int
168 do_swapon_uuid (const char *uuid)
169 {
170   return swaponoff ("/sbin/swapon", "-U", uuid);
171 }
172
173 int
174 do_swapoff_uuid (const char *uuid)
175 {
176   return swaponoff ("/sbin/swapoff", "-U", uuid);
177 }