Debian Codegraph @

src/plugins/dm.c

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Author: Vratislav Podzimek <[email protected]>
18 */
19
20 #include <glib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <blockdev/utils.h>
24 #include <libdevmapper.h>
25 #include <stdarg.h>
26 #include <syslog.h>
27
28 #include "dm.h"
29 #include "check_deps.h"
30 #include "dm_logging.h"
31
32 #define DM_MIN_VERSION "1.02.93"
33
34
35 /**
36 * SECTION: dm
37 * @short_description: plugin for basic operations with device mapper
38 * @title: DeviceMapper
39 * @include: dm.h
40 *
41 * A plugin for basic operations with device mapper.
42 */
43
44 /**
45 * bd_dm_error_quark: (skip)
46 */
47 GQuark bd_dm_error_quark (void)
48 {
49 return g_quark_from_static_string ("g-bd-dm-error-quark");
50 }
51
52 static volatile guint avail_deps = 0;
53 static GMutex deps_check_lock;
54
55 #define DEPS_DMSETUP 0
56 #define DEPS_DMSETUP_MASK (1 << DEPS_DMSETUP)
57 #define DEPS_LAST 1
58
59 static const UtilDep deps[DEPS_LAST] = {
60 {"dmsetup", DM_MIN_VERSION, NULL, "Library version:\\s+([\\d\\.]+)"},
61 };
62
63
64 /**
65 * bd_dm_init:
66 *
67 * Initializes the plugin. **This function is called automatically by the
68 * library's initialization functions.**
69 *
70 */
71 gboolean bd_dm_init (void) {
72 dm_log_with_errno_init ((dm_log_with_errno_fn) redirect_dm_log);
73 #ifdef DEBUG
74 dm_log_init_verbose (LOG_DEBUG);
75 #else
76 dm_log_init_verbose (LOG_INFO);
77 #endif
78
79 return TRUE;
80 }
81
82 /**
83 * bd_dm_close:
84 *
85 * Cleans up after the plugin. **This function is called automatically by the
86 * library's functions that unload it.**
87 *
88 */
89 void bd_dm_close (void) {
90 dm_log_with_errno_init (NULL);
91 dm_log_init_verbose (0);
92 g_atomic_int_set (&avail_deps, 0);
93 }
94
95 /**
96 * bd_dm_is_tech_avail:
97 * @tech: the queried tech
98 * @mode: a bit mask of queried modes of operation (#BDDMTechMode) for @tech
99 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
100 *
101 * Returns: whether the @tech-@mode combination is available -- supported by the
102 * plugin implementation and having all the runtime dependencies available
103 */
104 gboolean bd_dm_is_tech_avail (BDDMTech tech, guint64 mode G_GNUC_UNUSED, GError **error) {
105 /* all combinations are supported by this implementation of the plugin, but
106 BD_DM_TECH_MAP requires the 'dmsetup' utility */
107 switch (tech) {
108 case BD_DM_TECH_MAP:
109 return check_deps (&avail_deps, DEPS_DMSETUP_MASK, deps, DEPS_LAST, &deps_check_lock, error);
110 default:
111 return TRUE;
112 }
113 }
114
115 /**
116 * bd_dm_create_linear:
117 * @map_name: name of the map
118 * @device: device to create map for
119 * @length: length of the mapping in sectors
120 * @uuid: (nullable): UUID for the new dev mapper device or %NULL if not specified
121 * @error: (out) (optional): place to store error (if any)
122 *
123 * Returns: whether the new linear mapping @map_name was successfully created
124 * for the @device or not
125 *
126 * Tech category: %BD_DM_TECH_MAP-%BD_DM_TECH_MODE_CREATE_ACTIVATE
127 */
128 gboolean bd_dm_create_linear (const gchar *map_name, const gchar *device, guint64 length, const gchar *uuid, GError **error) {
129 gboolean success = FALSE;
130 const gchar *argv[8] = {"dmsetup", "create", map_name, "--table", NULL, NULL, NULL, NULL};
131
132 if (!check_deps (&avail_deps, DEPS_DMSETUP_MASK, deps, DEPS_LAST, &deps_check_lock, error))
133 return FALSE;
134
135 gchar *table = g_strdup_printf ("0 %"G_GUINT64_FORMAT" linear %s 0", length, device);
136 argv[4] = table;
137
138 if (uuid) {
139 argv[5] = "-u";
140 argv[6] = uuid;
141 }
142
143 success = bd_utils_exec_and_report_error (argv, NULL, error);
144 g_free (table);
145
146 return success;
147 }
148
149 /**
150 * bd_dm_remove:
151 * @map_name: name of the map to remove
152 * @error: (out) (optional): place to store error (if any)
153 *
154 * Returns: whether the @map_name map was successfully removed or not
155 *
156 * Tech category: %BD_DM_TECH_MAP-%BD_DM_TECH_MODE_REMOVE_DEACTIVATE
157 */
158 gboolean bd_dm_remove (const gchar *map_name, GError **error) {
159 const gchar *argv[4] = {"dmsetup", "remove", map_name, NULL};
160
161 if (!check_deps (&avail_deps, DEPS_DMSETUP_MASK, deps, DEPS_LAST, &deps_check_lock, error))
162 return FALSE;
163
164 return bd_utils_exec_and_report_error (argv, NULL, error);
165 }
166
167 /**
168 * bd_dm_name_from_node:
169 * @dm_node: name of the DM node (e.g. "dm-0")
170 * @error: (out) (optional): place to store error (if any)
171 *
172 * Returns: map name of the map providing the @dm_node device or %NULL
173 * (@error) contains the error in such cases
174 *
175 * Tech category: %BD_DM_TECH_MAP-%BD_DM_TECH_MODE_QUERY
176 */
177 gchar* bd_dm_name_from_node (const gchar *dm_node, GError **error) {
178 gchar *ret = NULL;
179 gboolean success = FALSE;
180 g_autofree gchar *sys_path = NULL;
181
182 if (!dm_node || strlen (dm_node) == 0) {
183 g_set_error_literal (error, BD_DM_ERROR, BD_DM_ERROR_DEVICE_NOEXIST,
184 "No DM node specified");
185 return NULL;
186 }
187
188 sys_path = g_strdup_printf ("/sys/class/block/%s/dm/name", dm_node);
189
190 if (access (sys_path, R_OK) != 0) {
191 g_set_error_literal (error, BD_DM_ERROR, BD_DM_ERROR_SYS,
192 "Failed to access dm node's parameters under /sys");
193 return NULL;
194 }
195
196 success = g_file_get_contents (sys_path, &ret, NULL, error);
197 if (!success) {
198 /* error is already populated */
199 g_free (ret);
200 return NULL;
201 }
202
203 return g_strstrip (ret);
204 }
205
206 /**
207 * bd_dm_node_from_name:
208 * @map_name: name of the queried DM map
209 * @error: (out) (optional): place to store error (if any)
210 *
211 * Returns: DM node name for the @map_name map or %NULL (@error) contains
212 * the error in such cases
213 *
214 * Tech category: %BD_DM_TECH_MAP-%BD_DM_TECH_MODE_QUERY
215 */
216 gchar* bd_dm_node_from_name (const gchar *map_name, GError **error) {
217 g_autofree gchar *dev_path = NULL;
218 g_autofree gchar *dev_mapper_path = g_strdup_printf ("/dev/mapper/%s", map_name);
219
220 if (!map_name || strlen (map_name) == 0) {
221 g_set_error_literal (error, BD_DM_ERROR, BD_DM_ERROR_DEVICE_NOEXIST,
222 "No DM name specified");
223 return NULL;
224 }
225
226 dev_path = bd_utils_resolve_device (dev_mapper_path, error);
227 if (!dev_path)
228 /* error is already populated */
229 return NULL;
230
231 return g_path_get_basename (dev_path);
232 }
233
234 /**
235 * bd_dm_get_subsystem_from_name:
236 * @device_name: name of the device
237 * @error: (out) (optional): place to store error (if any)
238 *
239 * Returns: subsystem of the given device
240 *
241 * Tech category: %BD_DM_TECH_MAP-%BD_DM_TECH_MODE_QUERY
242 */
243 gchar* bd_dm_get_subsystem_from_name (const gchar *device_name, GError **error) {
244 struct dm_task *task = NULL;
245 struct dm_info info;
246 const gchar *uuid = NULL;
247 gchar *subsystem = NULL;
248 gchar *hyphen_pos = NULL;
249
250 task = dm_task_create (DM_DEVICE_INFO);
251 if (!task) {
252 g_set_error_literal (error, BD_DM_ERROR, BD_DM_ERROR_TASK,
253 "Failed to create DM task");
254 return NULL;
255 }
256
257 if (!dm_task_set_name (task, device_name)) {
258 g_set_error_literal (error, BD_DM_ERROR, BD_DM_ERROR_TASK,
259 "Failed to set device name for DM task");
260 dm_task_destroy (task);
261 return NULL;
262 }
263
264 if (!dm_task_run (task)) {
265 g_set_error_literal (error, BD_DM_ERROR, BD_DM_ERROR_TASK,
266 "Failed to run DM task");
267 dm_task_destroy (task);
268 return NULL;
269 }
270
271 if (!dm_task_get_info (task, &info)) {
272 g_set_error_literal (error, BD_DM_ERROR, BD_DM_ERROR_TASK,
273 "Failed to get info from DM task");
274 dm_task_destroy (task);
275 return NULL;
276 }
277
278 if (!info.exists) {
279 g_set_error (error, BD_DM_ERROR, BD_DM_ERROR_DEVICE_NOEXIST,
280 "DM device %s does not exist", device_name);
281 dm_task_destroy (task);
282 return NULL;
283 }
284
285 uuid = dm_task_get_uuid (task);
286 if (!uuid || !(*uuid)) {
287 dm_task_destroy (task);
288 return g_strdup ("");
289 }
290
291 hyphen_pos = strchr (uuid, '-');
292 if (hyphen_pos)
293 subsystem = g_strndup (uuid, hyphen_pos - uuid);
294 else
295 subsystem = g_strdup ("");
296
297 dm_task_destroy (task);
298 return subsystem;
299 }
300
301 /**
302 * bd_dm_map_exists:
303 * @map_name: name of the queried map
304 * @live_only: whether to go through the live maps only or not
305 * @active_only: whether to ignore suspended maps or not
306 * @error: (out) (optional): place to store error (if any)
307 *
308 * Returns: whether the given @map_name exists (and is live if @live_only is
309 * %TRUE (and is active if @active_only is %TRUE)). If %FALSE is returned,
310 * @error indicates whether error appeared (non-%NULL) or not (%NULL).
311 *
312 * Tech category: %BD_DM_TECH_MAP-%BD_DM_TECH_MODE_QUERY
313 */
314 gboolean bd_dm_map_exists (const gchar *map_name, gboolean live_only, gboolean active_only, GError **error) {
315 struct dm_task *task_list = NULL;
316 struct dm_task *task_info = NULL;
317 struct dm_names *names = NULL;
318 struct dm_info info;
319 guint64 next = 0;
320 gboolean ret = FALSE;
321
322 if (geteuid () != 0) {
323 g_set_error_literal (error, BD_DM_ERROR, BD_DM_ERROR_NOT_ROOT,
324 "Not running as root, cannot query DM maps");
325 return FALSE;
326 }
327
328 task_list = dm_task_create (DM_DEVICE_LIST);
329 if (!task_list) {
330 g_set_error_literal (error, BD_DM_ERROR, BD_DM_ERROR_TASK,
331 "Failed to create DM task");
332 return FALSE;
333 }
334
335 dm_task_run (task_list);
336 names = dm_task_get_names (task_list);
337
338 if (!names || !names->dev) {
339 dm_task_destroy (task_list);
340 return FALSE;
341 }
342
343 do {
344 names = (void *)names + next;
345 next = names->next;
346 /* we are searching for the particular map_name map */
347 if (g_strcmp0 (map_name, names->name) != 0)
348 /* not matching, skip */
349 continue;
350
351 /* get device info */
352 task_info = dm_task_create (DM_DEVICE_INFO);
353 if (!task_info) {
354 g_set_error_literal (error, BD_DM_ERROR, BD_DM_ERROR_TASK,
355 "Failed to create DM task");
356 break;
357 }
358
359 /* something failed, try next one */
360 if (dm_task_set_name (task_info, names->name) == 0) {
361 dm_task_destroy (task_info);
362 continue;
363 }
364 if (dm_task_run (task_info) == 0) {
365 dm_task_destroy (task_info);
366 continue;
367 }
368 if (dm_task_get_info (task_info, &info) == 0) {
369 dm_task_destroy (task_info);
370 continue;
371 }
372
373 if (!info.exists) {
374 /* doesn't exist, try next one */
375 dm_task_destroy (task_info);
376 continue;
377 }
378
379 /* found existing name match, let's test the restrictions */
380 ret = TRUE;
381 if (live_only)
382 ret = info.live_table;
383 if (active_only)
384 ret = ret && !info.suspended;
385
386 dm_task_destroy (task_info);
387 if (ret)
388 /* found match according to restrictions */
389 break;
390 } while (next);
391
392 dm_task_destroy (task_list);
393
394 return ret;
395 }