Debian Codegraph @

modules/lvm2/udiskslinuxvolumegroup.c

raw
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 *
3 * Copyright (C) 2007-2010 David Zeuthen <[email protected]>
4 * Copyright (C) 2013 Marius Vollmer <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include "config.h"
23 #include <glib/gi18n-lib.h>
24
25 #include <sys/types.h>
26 #include <pwd.h>
27 #include <grp.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <mntent.h>
32
33 #include <glib/gstdio.h>
34
35 #include <blockdev/fs.h>
36 #include <blockdev/lvm.h>
37
38 #include <src/udiskslogging.h>
39 #include <src/udiskslinuxprovider.h>
40 #include <src/udisksdaemon.h>
41 #include <src/udisksstate.h>
42 #include <src/udisksdaemonutil.h>
43 #include <src/udiskslinuxdevice.h>
44 #include <src/udiskslinuxblock.h>
45 #include <src/udiskslinuxblockobject.h>
46
47 #include "udiskslinuxvolumegroup.h"
48 #include "udiskslinuxvolumegroupobject.h"
49 #include "udiskslinuxlogicalvolume.h"
50 #include "udiskslinuxlogicalvolumeobject.h"
51
52 #include "udiskslvm2daemonutil.h"
53 #include "jobhelpers.h"
54
55 /**
56 * SECTION:udiskslinuxvolume_group
57 * @title: UDisksLinuxVolumeGroup
58 * @short_description: Linux implementation of #UDisksVolumeGroup
59 *
60 * This type provides an implementation of the #UDisksVolumeGroup interface
61 * on Linux.
62 */
63
64 typedef struct _UDisksLinuxVolumeGroupClass UDisksLinuxVolumeGroupClass;
65
66 /**
67 * UDisksLinuxVolumeGroup:
68 *
69 * The #UDisksLinuxVolumeGroup structure contains only private data and should
70 * only be accessed using the provided API.
71 */
72 struct _UDisksLinuxVolumeGroup
73 {
74 UDisksVolumeGroupSkeleton parent_instance;
75 };
76
77 struct _UDisksLinuxVolumeGroupClass
78 {
79 UDisksVolumeGroupSkeletonClass parent_class;
80 };
81
82 static void volume_group_iface_init (UDisksVolumeGroupIface *iface);
83
84 G_DEFINE_TYPE_WITH_CODE (UDisksLinuxVolumeGroup, udisks_linux_volume_group, UDISKS_TYPE_VOLUME_GROUP_SKELETON,
85 G_IMPLEMENT_INTERFACE (UDISKS_TYPE_VOLUME_GROUP, volume_group_iface_init));
86
87 /* ---------------------------------------------------------------------------------------------------- */
88
89 static void
90 udisks_linux_volume_group_finalize (GObject *object)
91 {
92 if (G_OBJECT_CLASS (udisks_linux_volume_group_parent_class)->finalize != NULL)
93 G_OBJECT_CLASS (udisks_linux_volume_group_parent_class)->finalize (object);
94 }
95
96 static void
97 udisks_linux_volume_group_init (UDisksLinuxVolumeGroup *volume_group)
98 {
99 g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (volume_group),
100 G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
101 }
102
103 static void
104 udisks_linux_volume_group_class_init (UDisksLinuxVolumeGroupClass *klass)
105 {
106 GObjectClass *gobject_class;
107
108 gobject_class = G_OBJECT_CLASS (klass);
109 gobject_class->finalize = udisks_linux_volume_group_finalize;
110 }
111
112 /**
113 * udisks_linux_volume_group_new:
114 *
115 * Creates a new #UDisksLinuxVolumeGroup instance.
116 *
117 * Returns: A new #UDisksLinuxVolumeGroup. Free with g_object_unref().
118 */
119 UDisksVolumeGroup *
120 udisks_linux_volume_group_new (void)
121 {
122 return UDISKS_VOLUME_GROUP (g_object_new (UDISKS_TYPE_LINUX_VOLUME_GROUP,
123 NULL));
124 }
125
126 /**
127 * udisks_linux_volume_group_update:
128 * @volume_group: A #UDisksLinuxVolumeGroup.
129 * @object: The enclosing #UDisksLinuxVolumeGroupObject instance.
130 *
131 * Updates the interface.
132 */
133 void
134 udisks_linux_volume_group_update (UDisksLinuxVolumeGroup *volume_group,
135 BDLVMVGdata *vg_info,
136 GSList *vg_pvs,
137 gboolean *needs_polling_ret)
138 {
139 UDisksVolumeGroup *iface = UDISKS_VOLUME_GROUP (volume_group);
140
141 udisks_volume_group_set_name (iface, vg_info->name);
142 udisks_volume_group_set_uuid (iface, vg_info->uuid);
143 udisks_volume_group_set_size (iface, vg_info->size);
144 udisks_volume_group_set_free_size (iface, vg_info->free);
145 udisks_volume_group_set_extent_size (iface, vg_info->extent_size);
146
147 {
148 g_autoptr(GStrvBuilder) missing_builder = g_strv_builder_new ();
149 g_auto(GStrv) missing = NULL;
150
151 for (GSList *vg_pvs_p=vg_pvs; vg_pvs_p; vg_pvs_p=vg_pvs_p->next)
152 {
153 BDLVMPVdata *pv_info = vg_pvs_p->data;
154 if (pv_info->missing)
155 g_strv_builder_add (missing_builder, pv_info->pv_uuid);
156 }
157
158 missing = g_strv_builder_end (missing_builder);
159 udisks_volume_group_set_missing_physical_volumes (iface, (const gchar *const *)missing);
160 }
161
162 g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (iface));
163 }
164
165 /* ---------------------------------------------------------------------------------------------------- */
166
167 static gboolean
168 handle_poll (UDisksVolumeGroup *_group,
169 GDBusMethodInvocation *invocation)
170 {
171 UDisksLinuxVolumeGroup *group = UDISKS_LINUX_VOLUME_GROUP (_group);
172 UDisksLinuxVolumeGroupObject *object = NULL;
173 GError *error = NULL;
174
175 object = udisks_daemon_util_dup_object (group, &error);
176 if (object == NULL)
177 {
178 g_dbus_method_invocation_take_error (invocation, error);
179 goto out;
180 }
181
182 udisks_linux_volume_group_object_poll (object);
183
184 udisks_volume_group_complete_poll (_group, invocation);
185
186 out:
187 g_clear_object (&object);
188 return TRUE;
189 }
190
191 /* ---------------------------------------------------------------------------------------------------- */
192
193 /* transfer-full */
194 GList *
195 udisks_linux_volume_group_get_logical_volumes (UDisksVolumeGroup *group,
196 UDisksDaemon *daemon)
197 {
198 GList *ret = NULL;
199 GList *l, *objects = NULL;
200 GDBusObject *object;
201 UDisksLogicalVolume *volume;
202
203 object = g_dbus_interface_get_object (G_DBUS_INTERFACE (group));
204 if (object == NULL)
205 goto out;
206
207 objects = udisks_daemon_get_objects (daemon);
208 for (l = objects; l != NULL; l = l->next)
209 {
210 volume = udisks_object_peek_logical_volume (UDISKS_OBJECT (l->data));
211 if (volume &&
212 g_strcmp0 (udisks_logical_volume_get_volume_group (volume),
213 g_dbus_object_get_object_path (object)) == 0)
214 ret = g_list_append (ret, g_object_ref (volume));
215 }
216
217 out:
218 g_list_free_full (objects, g_object_unref);
219 return ret;
220 }
221
222 /* ---------------------------------------------------------------------------------------------------- */
223
224 static gboolean
225 teardown_volume_group (UDisksVolumeGroup *group,
226 UDisksDaemon *daemon,
227 GDBusMethodInvocation *invocation,
228 GVariant *options,
229 GError **error)
230 {
231 GList *volumes;
232 GList *l;
233 UDisksLogicalVolume *volume;
234
235 volumes = udisks_linux_volume_group_get_logical_volumes (group, daemon);
236 for (l = volumes; l; l = l->next)
237 {
238 volume = UDISKS_LOGICAL_VOLUME (l->data);
239 if (g_strcmp0 (udisks_logical_volume_get_type_ (volume), "pool") != 0)
240 {
241 if (!udisks_linux_logical_volume_teardown_block (volume,
242 daemon,
243 invocation,
244 options,
245 error))
246 {
247 g_list_free_full (volumes, g_object_unref);
248 return FALSE;
249 }
250 }
251 }
252 g_list_free_full (volumes, g_object_unref);
253
254 return TRUE;
255 }
256
257 static gboolean
258 handle_delete (UDisksVolumeGroup *_group,
259 GDBusMethodInvocation *invocation,
260 gboolean arg_wipe,
261 GVariant *arg_options)
262 {
263 GError *error = NULL;
264 UDisksLinuxVolumeGroup *group = UDISKS_LINUX_VOLUME_GROUP (_group);
265 UDisksLinuxVolumeGroupObject *object = NULL;
266 UDisksLinuxModuleLVM2 *module;
267 UDisksDaemon *daemon;
268 uid_t caller_uid;
269 gboolean teardown_flag = FALSE;
270 GList *objects_to_wipe = NULL;
271 GList *l;
272 VGJobData data;
273 const gchar *device_file = NULL;
274
275 g_variant_lookup (arg_options, "tear-down", "b", &teardown_flag);
276
277 object = udisks_daemon_util_dup_object (group, &error);
278 if (object == NULL)
279 {
280 g_dbus_method_invocation_take_error (invocation, error);
281 goto out;
282 }
283
284 module = udisks_linux_volume_group_object_get_module (object);
285 daemon = udisks_module_get_daemon (UDISKS_MODULE (module));
286
287 /* Find physical volumes to wipe. */
288 if (arg_wipe)
289 {
290 GList *objects = udisks_daemon_get_objects (daemon);
291 for (l = objects; l; l = l->next)
292 {
293 UDisksPhysicalVolume *physical_volume;
294 physical_volume = udisks_object_peek_physical_volume (UDISKS_OBJECT (l->data));
295 if (physical_volume
296 && g_strcmp0 (udisks_physical_volume_get_volume_group (physical_volume),
297 g_dbus_object_get_object_path (G_DBUS_OBJECT (object))) == 0)
298 objects_to_wipe = g_list_append (objects_to_wipe, g_object_ref (l->data));
299 }
300 g_list_free_full (objects, g_object_unref);
301 }
302
303 if (!udisks_daemon_util_get_caller_uid_sync (daemon,
304 invocation,
305 NULL /* GCancellable */,
306 &caller_uid,
307 &error))
308 {
309 g_dbus_method_invocation_return_gerror (invocation, error);
310 g_clear_error (&error);
311 goto out;
312 }
313
314 /* Policy check. */
315 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
316 UDISKS_OBJECT (object),
317 LVM2_POLICY_ACTION_ID,
318 arg_options,
319 N_("Authentication is required to delete a volume group"),
320 invocation);
321
322 if (teardown_flag &&
323 !teardown_volume_group (_group,
324 daemon,
325 invocation,
326 arg_options,
327 &error))
328 {
329 g_dbus_method_invocation_take_error (invocation, error);
330 goto out;
331 }
332
333 data.vg_name = udisks_linux_volume_group_object_get_name (object);
334
335 if (!udisks_daemon_launch_threaded_job_sync (daemon,
336 UDISKS_OBJECT (object),
337 "lvm-vg-delete",
338 caller_uid,
339 FALSE,
340 vgremove_job_func,
341 &data,
342 NULL, /* user_data_free_func */
343 NULL, /* GCancellable */
344 &error))
345 {
346 g_dbus_method_invocation_return_error (invocation,
347 UDISKS_ERROR,
348 UDISKS_ERROR_FAILED,
349 "Error deleting volume group: %s",
350 error->message);
351 g_clear_error (&error);
352 goto out;
353 }
354
355 for (l = objects_to_wipe; l; l = l->next)
356 {
357 UDisksBlock *block = udisks_object_peek_block (l->data);
358 if (block)
359 {
360 device_file = udisks_block_get_device (block);
361 if (!bd_lvm_pvremove (device_file, NULL, &error))
362 {
363 udisks_warning ("Failed to wipe PV %s: %s", device_file, error->message);
364 g_clear_error (&error);
365 }
366 else
367 udisks_linux_block_object_trigger_uevent_sync (l->data, UDISKS_DEFAULT_WAIT_TIMEOUT);
368
369 if (bd_lvm_is_tech_avail (BD_LVM_TECH_DEVICES, 0, NULL) &&
370 !bd_lvm_devices_delete (device_file, NULL, NULL, &error))
371 {
372 udisks_warning ("Failed to remove %s from LVM devices file: %s", device_file, error->message);
373 g_clear_error (&error);
374 }
375 }
376 }
377
378 udisks_volume_group_complete_delete (_group, invocation);
379
380 out:
381 g_list_free_full (objects_to_wipe, g_object_unref);
382 g_clear_object (&object);
383 return TRUE;
384 }
385
386 /* ---------------------------------------------------------------------------------------------------- */
387
388 typedef struct
389 {
390 UDisksLinuxModuleLVM2 *module;
391 const gchar *name;
392 } WaitForVolumeGroupObjectData;
393
394 static UDisksObject *
395 wait_for_volume_group_object (UDisksDaemon *daemon,
396 gpointer user_data)
397 {
398 WaitForVolumeGroupObjectData *data = user_data;
399 UDisksLinuxVolumeGroupObject *object;
400
401 object = udisks_linux_module_lvm2_find_volume_group_object (data->module, data->name);
402
403 if (object == NULL)
404 return NULL;
405
406 return g_object_ref (UDISKS_OBJECT (object));
407 }
408
409 static gboolean
410 handle_rename (UDisksVolumeGroup *_group,
411 GDBusMethodInvocation *invocation,
412 const gchar *new_name,
413 GVariant *options)
414 {
415 GError *error = NULL;
416 UDisksLinuxVolumeGroup *group = UDISKS_LINUX_VOLUME_GROUP (_group);
417 UDisksLinuxVolumeGroupObject *object = NULL;
418 UDisksLinuxModuleLVM2 *module;
419 UDisksDaemon *daemon;
420 uid_t caller_uid;
421 UDisksObject *group_object = NULL;
422 VGJobData data;
423 WaitForVolumeGroupObjectData wait_data;
424
425 object = udisks_daemon_util_dup_object (group, &error);
426 if (object == NULL)
427 {
428 g_dbus_method_invocation_take_error (invocation, error);
429 goto out;
430 }
431
432 module = udisks_linux_volume_group_object_get_module (object);
433 daemon = udisks_module_get_daemon (UDISKS_MODULE (module));
434
435 if (!udisks_daemon_util_get_caller_uid_sync (daemon,
436 invocation,
437 NULL /* GCancellable */,
438 &caller_uid,
439 &error))
440 {
441 g_dbus_method_invocation_return_gerror (invocation, error);
442 g_clear_error (&error);
443 goto out;
444 }
445
446 /* Policy check. */
447 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
448 UDISKS_OBJECT (object),
449 LVM2_POLICY_ACTION_ID,
450 options,
451 N_("Authentication is required to rename a volume group"),
452 invocation);
453
454 data.vg_name = udisks_linux_volume_group_object_get_name (object);
455 data.new_vg_name = new_name;
456
457 if (!udisks_daemon_launch_threaded_job_sync (daemon,
458 UDISKS_OBJECT (object),
459 "lvm-vg-rename",
460 caller_uid,
461 FALSE,
462 vgrename_job_func,
463 &data,
464 NULL, /* user_data_free_func */
465 NULL, /* GCancellable */
466 &error))
467 {
468 g_dbus_method_invocation_return_error (invocation,
469 UDISKS_ERROR,
470 UDISKS_ERROR_FAILED,
471 "Error renaming volume group: %s",
472 error->message);
473 g_clear_error (&error);
474 goto out;
475 }
476
477 wait_data.module = module;
478 wait_data.name = new_name;
479 group_object = udisks_daemon_wait_for_object_sync (daemon,
480 wait_for_volume_group_object,
481 &wait_data,
482 NULL,
483 UDISKS_DEFAULT_WAIT_TIMEOUT,
484 &error);
485 if (group_object == NULL)
486 {
487 g_prefix_error (&error,
488 "Error waiting for volume group object for '%s': ",
489 new_name);
490 g_dbus_method_invocation_take_error (invocation, error);
491 goto out;
492 }
493
494 udisks_volume_group_complete_rename (_group,
495 invocation,
496 g_dbus_object_get_object_path (G_DBUS_OBJECT (group_object)));
497 g_clear_object (&group_object);
498
499 out:
500 g_clear_object (&object);
501 return TRUE;
502 }
503
504 /* ---------------------------------------------------------------------------------------------------- */
505
506 static gboolean
507 handle_add_device (UDisksVolumeGroup *_group,
508 GDBusMethodInvocation *invocation,
509 const gchar *new_member_device_objpath,
510 GVariant *options)
511 {
512 UDisksLinuxVolumeGroup *group = UDISKS_LINUX_VOLUME_GROUP (_group);
513 UDisksLinuxModuleLVM2 *module;
514 UDisksDaemon *daemon;
515 UDisksLinuxVolumeGroupObject *object;
516 uid_t caller_uid;
517 GError *error = NULL;
518 UDisksObject *new_member_device_object = NULL;
519 UDisksBlock *new_member_device = NULL;
520 UDisksPhysicalVolume *physical_volume = NULL;
521 VGJobData data;
522
523 object = udisks_daemon_util_dup_object (group, &error);
524 if (object == NULL)
525 {
526 g_dbus_method_invocation_take_error (invocation, error);
527 goto out;
528 }
529
530 module = udisks_linux_volume_group_object_get_module (object);
531 daemon = udisks_module_get_daemon (UDISKS_MODULE (module));
532
533 error = NULL;
534 if (!udisks_daemon_util_get_caller_uid_sync (daemon,
535 invocation,
536 NULL /* GCancellable */,
537 &caller_uid,
538 &error))
539 {
540 g_dbus_method_invocation_return_gerror (invocation, error);
541 g_clear_error (&error);
542 goto out;
543 }
544
545 new_member_device_object = udisks_daemon_find_object (daemon, new_member_device_objpath);
546 if (new_member_device_object == NULL)
547 {
548 g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
549 "No device for given object path");
550 goto out;
551 }
552
553 new_member_device = udisks_object_get_block (new_member_device_object);
554 if (new_member_device == NULL)
555 {
556 g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
557 "No block interface on given object");
558 goto out;
559 }
560
561 /* Policy check. */
562 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
563 UDISKS_OBJECT (object),
564 LVM2_POLICY_ACTION_ID,
565 options,
566 N_("Authentication is required to add a device to a volume group"),
567 invocation);
568
569 if (!udisks_daemon_util_lvm2_block_is_unused (new_member_device, &error))
570 {
571 g_dbus_method_invocation_take_error (invocation, error);
572 goto out;
573 }
574
575 if (!udisks_daemon_util_lvm2_wipe_block (daemon, new_member_device, &error))
576 {
577 g_dbus_method_invocation_take_error (invocation, error);
578 goto out;
579 }
580
581 physical_volume = udisks_object_peek_physical_volume (new_member_device_object);
582 if (!physical_volume)
583 {
584 PVJobData pv_data;
585 pv_data.path = udisks_block_get_device (new_member_device);
586 if (!udisks_daemon_launch_threaded_job_sync (daemon,
587 UDISKS_OBJECT (object),
588 "lvm-pv-create",
589 caller_uid,
590 FALSE,
591 pvcreate_job_func,
592 &pv_data,
593 NULL, /* user_data_free_func */
594 NULL, /* GCancellable */
595 &error))
596 {
597 g_dbus_method_invocation_return_error (invocation,
598 UDISKS_ERROR,
599 UDISKS_ERROR_FAILED,
600 "Error creating LVM metadata on %s: %s",
601 pv_data.path,
602 error->message);
603 g_clear_error (&error);
604 goto out;
605 }
606 }
607
608
609 data.vg_name = udisks_linux_volume_group_object_get_name (object);
610 data.pv_path = udisks_block_get_device (new_member_device);
611
612 if (!udisks_daemon_launch_threaded_job_sync (daemon,
613 UDISKS_OBJECT (object),
614 "lvm-vg-add-device",
615 caller_uid,
616 FALSE,
617 vgextend_job_func,
618 &data,
619 NULL, /* user_data_free_func */
620 NULL, /* GCancellable */
621 &error))
622 {
623 g_dbus_method_invocation_return_error (invocation,
624 UDISKS_ERROR,
625 UDISKS_ERROR_FAILED,
626 "Error adding %s to volume group: %s",
627 data.pv_path,
628 error->message);
629 g_clear_error (&error);
630 goto out;
631 }
632
633 udisks_volume_group_complete_add_device (_group, invocation);
634
635 out:
636 g_clear_object (&new_member_device_object);
637 g_clear_object (&new_member_device);
638 g_clear_object (&object);
639 return TRUE; /* returning TRUE means that we handled the method invocation */
640 }
641
642 /* ---------------------------------------------------------------------------------------------------- */
643 static gboolean
644 handle_remove_common (UDisksVolumeGroup *_group,
645 GDBusMethodInvocation *invocation,
646 const gchar *member_device_objpath,
647 GVariant *options,
648 gboolean is_remove,
649 gboolean arg_wipe)
650 {
651 UDisksLinuxVolumeGroup *group = UDISKS_LINUX_VOLUME_GROUP (_group);
652 UDisksLinuxModuleLVM2 *module;
653 UDisksDaemon *daemon;
654 UDisksLinuxVolumeGroupObject *object;
655 uid_t caller_uid;
656 GError *error = NULL;
657 UDisksObject *member_device_object = NULL;
658 UDisksBlock *member_device = NULL;
659 VGJobData data;
660 const gchar *authentication_error_msg = NULL;
661 const gchar *job_operation = NULL;
662 UDisksThreadedJobFunc job_func = NULL;
663 gboolean do_wipe = FALSE;
664
665 if (is_remove)
666 {
667 authentication_error_msg = N_("Authentication is required to remove a device from a volume group");
668 job_operation = "lvm-vg-rem-device";
669 job_func = vgreduce_job_func;
670
671 if (arg_wipe)
672 do_wipe = TRUE;
673 }
674 else
675 {
676 authentication_error_msg = N_("Authentication is required to empty a device in a volume group");
677 job_operation = "lvm-vg-empty-device";
678 job_func = pvmove_job_func;
679 }
680
681 object = udisks_daemon_util_dup_object (group, &error);
682 if (object == NULL)
683 {
684 g_dbus_method_invocation_take_error (invocation, error);
685 goto out;
686 }
687
688 module = udisks_linux_volume_group_object_get_module (object);
689 daemon = udisks_module_get_daemon (UDISKS_MODULE (module));
690
691 error = NULL;
692 if (!udisks_daemon_util_get_caller_uid_sync (daemon,
693 invocation,
694 NULL /* GCancellable */,
695 &caller_uid,
696 &error))
697 {
698 g_dbus_method_invocation_return_gerror (invocation, error);
699 g_clear_error (&error);
700 goto out;
701 }
702
703 member_device_object = udisks_daemon_find_object (daemon, member_device_objpath);
704 if (member_device_object == NULL)
705 {
706 g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
707 "No device for given object path");
708 goto out;
709 }
710
711 member_device = udisks_object_get_block (member_device_object);
712 if (member_device == NULL)
713 {
714 g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
715 "No block interface on given object");
716 goto out;
717 }
718
719 /* Policy check. */
720 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
721 UDISKS_OBJECT (object),
722 LVM2_POLICY_ACTION_ID,
723 options,
724 authentication_error_msg,
725 invocation);
726
727 if (is_remove)
728 data.vg_name = udisks_linux_volume_group_object_get_name (object);
729
730 data.pv_path = udisks_block_get_device (member_device);
731
732 if (!udisks_daemon_launch_threaded_job_sync (daemon,
733 UDISKS_OBJECT (object),
734 job_operation,
735 caller_uid,
736 FALSE,
737 job_func,
738 &data,
739 NULL, /* user_data_free_func */
740 NULL, /* GCancellable */
741 &error))
742 {
743 g_dbus_method_invocation_return_error (invocation,
744 UDISKS_ERROR,
745 UDISKS_ERROR_FAILED,
746 (is_remove) ? "Error remove %s from volume group: %s" : "Error emptying %s: %s",
747 data.pv_path,
748 error->message);
749 g_clear_error (&error);
750 goto out;
751 }
752
753 if (do_wipe)
754 {
755 if (!udisks_daemon_launch_threaded_job_sync (daemon,
756 UDISKS_OBJECT (object),
757 "pv-format-erase",
758 caller_uid,
759 FALSE,
760 pvremove_job_func,
761 &data,
762 NULL, /* user_data_free_func */
763 NULL, /* GCancellable */
764 &error))
765 {
766 g_dbus_method_invocation_return_error (invocation,
767 UDISKS_ERROR,
768 UDISKS_ERROR_FAILED,
769 "Error wiping %s after removal from volume group %s: %s",
770 data.pv_path,
771 udisks_linux_volume_group_object_get_name (object),
772 error->message);
773 g_clear_error (&error);
774 goto out;
775 }
776 }
777
778 udisks_volume_group_complete_remove_device (_group, invocation);
779
780 out:
781 g_clear_object (&member_device_object);
782 g_clear_object (&member_device);
783 g_clear_object (&object);
784 return TRUE; /* returning TRUE means that we handled the method invocation */
785 }
786
787
788 static gboolean
789 handle_remove_device (UDisksVolumeGroup *_group,
790 GDBusMethodInvocation *invocation,
791 const gchar *member_device_objpath,
792 gboolean arg_wipe,
793 GVariant *options)
794 {
795 return handle_remove_common (_group, invocation, member_device_objpath,
796 options, TRUE, arg_wipe);
797 }
798
799 /* ---------------------------------------------------------------------------------------------------- */
800
801 static gboolean
802 handle_empty_device (UDisksVolumeGroup *_group,
803 GDBusMethodInvocation *invocation,
804 const gchar *member_device_objpath,
805 GVariant *options)
806 {
807 return handle_remove_common (_group, invocation, member_device_objpath,
808 options, FALSE, FALSE);
809 }
810
811 /* ---------------------------------------------------------------------------------------------------- */
812
813 static gboolean
814 handle_remove_missing_physical_volumes (UDisksVolumeGroup *_group,
815 GDBusMethodInvocation *invocation,
816 GVariant *options)
817 {
818 UDisksLinuxVolumeGroup *group = UDISKS_LINUX_VOLUME_GROUP (_group);
819 UDisksLinuxModuleLVM2 *module;
820 UDisksDaemon *daemon;
821 UDisksLinuxVolumeGroupObject *object;
822 uid_t caller_uid;
823 GError *error = NULL;
824 VGJobData data;
825
826 object = udisks_daemon_util_dup_object (group, &error);
827 if (object == NULL)
828 {
829 g_dbus_method_invocation_take_error (invocation, error);
830 goto out;
831 }
832
833 module = udisks_linux_volume_group_object_get_module (object);
834 daemon = udisks_module_get_daemon (UDISKS_MODULE (module));
835
836 error = NULL;
837 if (!udisks_daemon_util_get_caller_uid_sync (daemon,
838 invocation,
839 NULL /* GCancellable */,
840 &caller_uid,
841 &error))
842 {
843 g_dbus_method_invocation_return_gerror (invocation, error);
844 g_clear_error (&error);
845 goto out;
846 }
847
848 /* Policy check. */
849 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
850 UDISKS_OBJECT (object),
851 LVM2_POLICY_ACTION_ID,
852 options,
853 N_("Authentication is required to remove missing physical volumes from a volume group"),
854 invocation);
855
856 data.vg_name = udisks_linux_volume_group_object_get_name (object);
857 data.pv_path = NULL;
858
859 if (!udisks_daemon_launch_threaded_job_sync (daemon,
860 UDISKS_OBJECT (object),
861 "lvm-vg-rem-device",
862 caller_uid,
863 FALSE,
864 vgreduce_job_func,
865 &data,
866 NULL, /* user_data_free_func */
867 NULL, /* GCancellable */
868 &error))
869 {
870 g_dbus_method_invocation_return_error (invocation,
871 UDISKS_ERROR,
872 UDISKS_ERROR_FAILED,
873 "Error removing missing physical volumes: %s",
874 error->message);
875 g_clear_error (&error);
876 goto out;
877 }
878
879 udisks_volume_group_complete_remove_missing_physical_volumes (_group, invocation);
880
881 out:
882 g_clear_object (&object);
883 return TRUE; /* returning TRUE means that we handled the method invocation */
884 }
885
886 /* ---------------------------------------------------------------------------------------------------- */
887
888 struct WaitData {
889 UDisksLinuxVolumeGroupObject *group_object;
890 const gchar *name;
891 };
892
893 static UDisksObject *
894 wait_for_logical_volume_object (UDisksDaemon *daemon,
895 gpointer user_data)
896 {
897 struct WaitData *data = user_data;
898 UDisksLinuxLogicalVolumeObject *object;
899
900 object = udisks_linux_volume_group_object_find_logical_volume_object (data->group_object, data->name);
901 if (object == NULL)
902 return NULL;
903
904 return g_object_ref (UDISKS_OBJECT (object));
905 }
906
907 static const gchar *
908 wait_for_logical_volume_path (UDisksLinuxVolumeGroupObject *group_object,
909 const gchar *name,
910 GError **error)
911 {
912 struct WaitData data;
913 UDisksLinuxModuleLVM2 *module;
914 UDisksDaemon *daemon;
915 UDisksObject *volume_object;
916 const gchar *object_path;
917
918 data.group_object = group_object;
919 data.name = name;
920 module = udisks_linux_volume_group_object_get_module (group_object);
921 daemon = udisks_module_get_daemon (UDISKS_MODULE (module));
922 volume_object = udisks_daemon_wait_for_object_sync (daemon,
923 wait_for_logical_volume_object,
924 &data,
925 NULL,
926 UDISKS_DEFAULT_WAIT_TIMEOUT,
927 error);
928 if (volume_object == NULL)
929 return NULL;
930
931 object_path = g_dbus_object_get_object_path (G_DBUS_OBJECT (volume_object));
932 g_object_unref (volume_object);
933
934 return object_path;
935 }
936
937 /* ---------------------------------------------------------------------------------------------------- */
938
939 enum VolumeType { VOL_PLAIN, VOL_THIN_POOL, VOL_THIN_VOLUME, VOL_VDO_VOLUME };
940 typedef void (*VolumeCompletionFunc) (UDisksVolumeGroup *object,
941 GDBusMethodInvocation *invocation,
942 const gchar *result);
943
944 static gboolean
945 handle_create_volume (UDisksVolumeGroup *_group,
946 GDBusMethodInvocation *invocation,
947 const gchar *arg_name,
948 guint64 arg_size,
949 GVariant *options,
950 enum VolumeType vol_creation_type,
951 const gchar *arg_pool,
952 guint64 arg_virtual_size,
953 guint64 arg_index_memory,
954 gboolean arg_compression,
955 gboolean arg_deduplication,
956 const gchar *arg_write_policy,
957 const gchar *arg_layout,
958 const gchar *const *arg_pvs)
959 {
960 GError *error = NULL;
961 UDisksLinuxVolumeGroup *group = UDISKS_LINUX_VOLUME_GROUP (_group);
962 UDisksLinuxVolumeGroupObject *object = NULL;
963 UDisksLinuxModuleLVM2 *module;
964 UDisksDaemon *daemon;
965 uid_t caller_uid;
966 const gchar *lv_objpath;
967 LVJobData data = {0};
968 g_auto(GStrv) pvs = NULL;
969 UDisksLinuxLogicalVolumeObject *pool_object = NULL;
970 const gchar *auth_error_msg = NULL;
971 UDisksThreadedJobFunc create_function = NULL;
972 VolumeCompletionFunc completion_function = NULL;
973
974 if (VOL_PLAIN == vol_creation_type)
975 {
976 auth_error_msg = N_("Authentication is required to create a logical volume");
977 create_function = lvcreate_job_func;
978 completion_function = udisks_volume_group_complete_create_plain_volume;
979 }
980 else if (VOL_THIN_VOLUME == vol_creation_type)
981 {
982 auth_error_msg = N_("Authentication is required to create a thin volume");
983 create_function = lvcreate_thin_job_func;
984 completion_function = udisks_volume_group_complete_create_thin_volume;
985 }
986 else if (VOL_THIN_POOL == vol_creation_type)
987 {
988 auth_error_msg = N_("Authentication is required to create a thin pool volume");
989 create_function = lvcreate_thin_pool_job_func;
990 completion_function = udisks_volume_group_complete_create_thin_pool_volume;
991 }
992 else if (VOL_VDO_VOLUME == vol_creation_type)
993 {
994 auth_error_msg = N_("Authentication is required to create a VDO volume");
995 create_function = lvcreate_vdo_job_func;
996 completion_function = udisks_volume_group_complete_create_vdo_volume;
997 }
998
999 object = udisks_daemon_util_dup_object (group, &error);
1000 if (object == NULL)
1001 {
1002 g_dbus_method_invocation_take_error (invocation, error);
1003 goto out;
1004 }
1005
1006 module = udisks_linux_volume_group_object_get_module (object);
1007 daemon = udisks_module_get_daemon (UDISKS_MODULE (module));
1008 if (!udisks_daemon_util_get_caller_uid_sync (daemon,
1009 invocation,
1010 NULL /* GCancellable */,
1011 &caller_uid,
1012 &error))
1013 {
1014 g_dbus_method_invocation_return_gerror (invocation, error);
1015 g_clear_error (&error);
1016 goto out;
1017 }
1018
1019 /* Policy check. */
1020 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
1021 UDISKS_OBJECT (object),
1022 LVM2_POLICY_ACTION_ID,
1023 options,
1024 auth_error_msg,
1025 invocation);
1026
1027 data.vg_name = udisks_linux_volume_group_object_get_name (object);
1028 data.new_lv_name = arg_name;
1029 data.new_lv_size = arg_size;
1030
1031 if (VOL_PLAIN == vol_creation_type)
1032 {
1033 if (arg_layout != NULL
1034 && g_strcmp0 (arg_layout, "linear") != 0
1035 && !g_str_has_prefix (arg_layout, "raid")
1036 && g_strcmp0 (arg_layout, "mirror") != 0)
1037 {
1038 g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
1039 "Not a valid layout for a plain volume: %s", arg_layout);
1040 goto out;
1041 }
1042
1043 data.new_lv_layout = arg_layout;
1044 data.new_lv_stripes = 0;
1045 data.new_lv_mirrors = 0;
1046
1047 if (arg_pvs)
1048 {
1049 gint32 n_subvolumes = g_strv_length ((gchar **)arg_pvs);
1050 g_variant_lookup (options, "subvolumes", "i", &n_subvolumes);
1051
1052 if (g_strcmp0 (arg_layout, "raid0") == 0)
1053 data.new_lv_stripes = n_subvolumes;
1054 else if (g_strcmp0 (arg_layout, "raid1") == 0)
1055 data.new_lv_mirrors = n_subvolumes - 1;
1056 else if (g_strcmp0 (arg_layout, "raid10") == 0) {
1057 if (n_subvolumes % 2 != 0)
1058 {
1059 g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
1060 "Number of subvolumes for raid10 must be even");
1061 goto out;
1062 }
1063 data.new_lv_stripes = n_subvolumes / 2;
1064 } else if (g_strcmp0 (arg_layout, "raid4") == 0 ||g_strcmp0 (arg_layout, "raid5") == 0)
1065 data.new_lv_stripes = n_subvolumes - 1;
1066 else if (g_strcmp0 (arg_layout, "raid6") == 0)
1067 data.new_lv_stripes = n_subvolumes - 2;
1068
1069 pvs = udisks_daemon_util_lvm2_gather_pvs (daemon, object, arg_pvs, &error);
1070 if (pvs == NULL)
1071 {
1072 g_dbus_method_invocation_take_error (invocation, error);
1073 goto out;
1074 }
1075 data.new_lv_pvs = (const gchar **)pvs;
1076 }
1077 }
1078
1079 if (VOL_THIN_POOL == vol_creation_type)
1080 data.extent_size = udisks_volume_group_get_extent_size (UDISKS_VOLUME_GROUP (group));
1081
1082 if (VOL_THIN_VOLUME == vol_creation_type)
1083 {
1084 pool_object = UDISKS_LINUX_LOGICAL_VOLUME_OBJECT (udisks_daemon_find_object (daemon, arg_pool));
1085 if (pool_object == NULL || !UDISKS_IS_LINUX_LOGICAL_VOLUME_OBJECT (pool_object))
1086 {
1087 g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
1088 "Not a logical volume");
1089 goto out;
1090 }
1091 data.pool_name = udisks_linux_logical_volume_object_get_name (pool_object);
1092 }
1093
1094 if (VOL_VDO_VOLUME == vol_creation_type)
1095 {
1096 data.pool_name = arg_pool;
1097 data.virtual_size = arg_virtual_size;
1098 data.index_memory = arg_index_memory;
1099 data.compression = arg_compression;
1100 data.deduplication = arg_deduplication;
1101 data.write_policy = arg_write_policy;
1102 }
1103
1104 if (!udisks_daemon_launch_threaded_job_sync (daemon,
1105 UDISKS_OBJECT (object),
1106 "lvm-vg-create-volume",
1107 caller_uid,
1108 FALSE,
1109 create_function,
1110 &data,
1111 NULL, /* user_data_free_func */
1112 NULL, /* GCancellable */
1113 &error))
1114 {
1115 g_dbus_method_invocation_return_error (invocation,
1116 UDISKS_ERROR,
1117 UDISKS_ERROR_FAILED,
1118 "Error creating volume: %s",
1119 error->message);
1120 g_clear_error (&error);
1121 goto out;
1122 }
1123
1124 lv_objpath = wait_for_logical_volume_path (object, arg_name, &error);
1125 if (lv_objpath == NULL)
1126 {
1127 g_prefix_error (&error,
1128 "Error waiting for logical volume object for '%s': ",
1129 arg_name);
1130 g_dbus_method_invocation_take_error (invocation, error);
1131 goto out;
1132 }
1133
1134 completion_function (_group, invocation, lv_objpath);
1135
1136 out:
1137 g_clear_object (&pool_object);
1138 g_clear_object (&object);
1139 return TRUE;
1140 }
1141
1142 static gboolean
1143 handle_create_plain_volume (UDisksVolumeGroup *_group,
1144 GDBusMethodInvocation *invocation,
1145 const gchar *arg_name,
1146 guint64 arg_size,
1147 GVariant *options)
1148 {
1149 return handle_create_volume(_group, invocation, arg_name, arg_size, options,
1150 VOL_PLAIN, NULL, 0, 0, FALSE, FALSE, NULL,
1151 NULL, NULL);
1152 }
1153
1154 static gboolean
1155 handle_create_plain_volume_with_layout (UDisksVolumeGroup *_group,
1156 GDBusMethodInvocation *invocation,
1157 const gchar *arg_name,
1158 guint64 arg_size,
1159 const gchar *arg_layout,
1160 const gchar *const *arg_pvs,
1161 GVariant *options)
1162 {
1163 return handle_create_volume(_group, invocation, arg_name, arg_size, options,
1164 VOL_PLAIN, NULL, 0, 0, FALSE, FALSE, NULL,
1165 arg_layout, arg_pvs);
1166 }
1167
1168 /* ---------------------------------------------------------------------------------------------------- */
1169
1170 static gboolean
1171 handle_create_thin_pool_volume (UDisksVolumeGroup *_group,
1172 GDBusMethodInvocation *invocation,
1173 const gchar *arg_name,
1174 guint64 arg_size,
1175 GVariant *options)
1176 {
1177 return handle_create_volume(_group, invocation, arg_name, arg_size, options,
1178 VOL_THIN_POOL, NULL, 0, 0, FALSE, FALSE, NULL,
1179 NULL, NULL);
1180 }
1181
1182 /* ---------------------------------------------------------------------------------------------------- */
1183
1184 static gboolean
1185 handle_create_thin_volume (UDisksVolumeGroup *_group,
1186 GDBusMethodInvocation *invocation,
1187 const gchar *arg_name,
1188 guint64 arg_size,
1189 const gchar *arg_pool,
1190 GVariant *options)
1191 {
1192 return handle_create_volume(_group, invocation, arg_name, arg_size, options,
1193 VOL_THIN_VOLUME, arg_pool, 0, 0, FALSE, FALSE, NULL,
1194 NULL, NULL);
1195 }
1196
1197 static gboolean
1198 handle_create_vdo_volume (UDisksVolumeGroup *_group,
1199 GDBusMethodInvocation *invocation,
1200 const gchar *arg_name,
1201 const gchar *arg_pool,
1202 guint64 arg_size,
1203 guint64 arg_virtual_size,
1204 guint64 arg_index_memory,
1205 gboolean arg_compression,
1206 gboolean arg_deduplication,
1207 const gchar *arg_write_policy,
1208 GVariant *options)
1209 {
1210 return handle_create_volume (_group, invocation, arg_name, arg_size, options,
1211 VOL_VDO_VOLUME, arg_pool, arg_virtual_size, arg_index_memory,
1212 arg_compression, arg_deduplication, arg_write_policy,
1213 NULL, NULL);
1214 }
1215
1216 /* ---------------------------------------------------------------------------------------------------- */
1217
1218 static void
1219 volume_group_iface_init (UDisksVolumeGroupIface *iface)
1220 {
1221 iface->handle_poll = handle_poll;
1222
1223 iface->handle_delete = handle_delete;
1224 iface->handle_rename = handle_rename;
1225
1226 iface->handle_add_device = handle_add_device;
1227 iface->handle_remove_device = handle_remove_device;
1228 iface->handle_empty_device = handle_empty_device;
1229 iface->handle_remove_missing_physical_volumes = handle_remove_missing_physical_volumes;
1230
1231 iface->handle_create_plain_volume = handle_create_plain_volume;
1232 iface->handle_create_plain_volume_with_layout = handle_create_plain_volume_with_layout;
1233 iface->handle_create_thin_pool_volume = handle_create_thin_pool_volume;
1234 iface->handle_create_thin_volume = handle_create_thin_volume;
1235
1236 iface->handle_create_vdo_volume = handle_create_vdo_volume;
1237 }