New partition APIs: part_del, part_get_bootable, part_get/set_mbr_id
[libguestfs.git] / daemon / blockdev.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 <stdint.h>
25 #include <inttypes.h>
26 #include <unistd.h>
27
28 #include "daemon.h"
29 #include "actions.h"
30
31 /* These functions are all about using the blockdev command, so
32  * we centralize it in one call.
33  */
34 static int64_t
35 call_blockdev (const char *device, const char *switc, int extraarg, int prints)
36 {
37   int r;
38   int64_t rv;
39   char *out, *err;
40   const char *argv[] = {
41     "blockdev",
42     switc,
43     NULL,
44     NULL,
45     NULL
46   };
47   char buf[64];
48
49   if (extraarg > 0) {
50     snprintf (buf, sizeof buf, "%d", extraarg);
51     argv[2] = buf;
52     argv[3] = device;
53   } else
54     argv[2] = device;
55
56   r = commandv (&out, &err, argv);
57
58   if (r == -1) {
59     reply_with_error ("%s: %s", argv[0], err);
60     free (err);
61     free (out);
62     return -1;
63   }
64
65   rv = 0;
66
67   if (prints) {
68     if (sscanf (out, "%" SCNi64, &rv) != 1) {
69       reply_with_error ("%s: expected output, but got nothing", argv[0]);
70       free (out);
71       return -1;
72     }
73   }
74
75   free (out);
76
77   return rv;
78 }
79
80 int
81 do_blockdev_setro (const char *device)
82 {
83   return (int) call_blockdev (device, "--setro", 0, 0);
84 }
85
86 int
87 do_blockdev_setrw (const char *device)
88 {
89   return (int) call_blockdev (device, "--setrw", 0, 0);
90 }
91
92 int
93 do_blockdev_getro (const char *device)
94 {
95   return (int) call_blockdev (device, "--getro", 0, 1);
96 }
97
98 int
99 do_blockdev_getss (const char *device)
100 {
101   return (int) call_blockdev (device, "--getss", 0, 1);
102 }
103
104 int
105 do_blockdev_getbsz (const char *device)
106 {
107   return (int) call_blockdev (device, "--getbsz", 0, 1);
108 }
109
110 int
111 do_blockdev_setbsz (const char *device, int blocksize)
112 {
113   if (blocksize <= 0 /* || blocksize >= what? */) {
114     reply_with_error ("blocksize must be > 0");
115     return -1;
116   }
117   return (int) call_blockdev (device, "--setbsz", blocksize, 0);
118 }
119
120 int64_t
121 do_blockdev_getsz (const char *device)
122 {
123   return call_blockdev (device, "--getsz", 0, 1);
124 }
125
126 int64_t
127 do_blockdev_getsize64 (const char *device)
128 {
129   return call_blockdev (device, "--getsize64", 0, 1);
130 }
131
132 int
133 do_blockdev_flushbufs (const char *device)
134 {
135   return call_blockdev (device, "--flushbufs", 0, 0);
136 }
137
138 int
139 do_blockdev_rereadpt (const char *device)
140 {
141   return call_blockdev (device, "--rereadpt", 0, 0);
142 }