| 1 |
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2013 Marius Vollmer <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
#include <glib/gi18n-lib.h> |
| 23 |
#include <glib/gstdio.h> |
| 24 |
#include <gio/gunixfdlist.h> |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <ctype.h> |
| 28 |
|
| 29 |
#include <sys/types.h> |
| 30 |
#include <sys/stat.h> |
| 31 |
#include <fcntl.h> |
| 32 |
#include <errno.h> |
| 33 |
#include <pwd.h> |
| 34 |
#include <fcntl.h> |
| 35 |
#include <sys/ioctl.h> |
| 36 |
#include <linux/fs.h> |
| 37 |
|
| 38 |
#include <blockdev/fs.h> |
| 39 |
#include <blockdev/lvm.h> |
| 40 |
|
| 41 |
#include <limits.h> |
| 42 |
#include <stdlib.h> |
| 43 |
|
| 44 |
#include <src/udisksdaemon.h> |
| 45 |
#include <src/udisksdaemonutil.h> |
| 46 |
#include <src/udisksstate.h> |
| 47 |
#include <src/udiskslogging.h> |
| 48 |
#include <src/udiskslinuxblockobject.h> |
| 49 |
#include <src/udiskslinuxdriveobject.h> |
| 50 |
#include <src/udisksmodulemanager.h> |
| 51 |
|
| 52 |
#include "udiskslvm2daemonutil.h" |
| 53 |
|
| 54 |
/** |
| 55 |
* SECTION:udiskslvm2daemonutil |
| 56 |
* @title: Utilities |
| 57 |
* @short_description: Various utility routines |
| 58 |
* |
| 59 |
* Various utility routines. |
| 60 |
*/ |
| 61 |
|
| 62 |
gboolean |
| 63 |
udisks_daemon_util_lvm2_block_is_unused (UDisksBlock *block, |
| 64 |
GError **error) |
| 65 |
{ |
| 66 |
const gchar *device_file; |
| 67 |
int fd; |
| 68 |
gint num_tries = 0; |
| 69 |
|
| 70 |
device_file = udisks_block_get_device (block); |
| 71 |
|
| 72 |
while ((fd = open (device_file, O_RDONLY | O_EXCL)) < 0) |
| 73 |
{ |
| 74 |
g_usleep (100 * 1000); /* microseconds */ |
| 75 |
if (num_tries++ > 10) |
| 76 |
break; |
| 77 |
} |
| 78 |
if (fd < 0) |
| 79 |
{ |
| 80 |
g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_FAILED, |
| 81 |
"Error opening device %s for unused block device detection: %m", |
| 82 |
device_file); |
| 83 |
return FALSE; |
| 84 |
} |
| 85 |
close (fd); |
| 86 |
|
| 87 |
return TRUE; |
| 88 |
} |
| 89 |
|
| 90 |
gboolean |
| 91 |
udisks_daemon_util_lvm2_wipe_block (UDisksDaemon *daemon, |
| 92 |
UDisksBlock *block, |
| 93 |
GError **error) |
| 94 |
{ |
| 95 |
UDisksObject *block_object; |
| 96 |
UDisksPhysicalVolume *physical_volume; |
| 97 |
const gchar *volume_group_objpath; |
| 98 |
UDisksObject *volume_group_object = NULL; |
| 99 |
UDisksVolumeGroup *volume_group; |
| 100 |
gchar *volume_group_name = NULL; |
| 101 |
gboolean was_partitioned; |
| 102 |
const gchar *device_file; |
| 103 |
gboolean ret = TRUE; |
| 104 |
GError *local_error = NULL; |
| 105 |
|
| 106 |
/* Find the name of the volume group that this device is a physical |
| 107 |
* member of, if any. Easy. |
| 108 |
*/ |
| 109 |
block_object = udisks_daemon_util_dup_object (block, error); |
| 110 |
if (block_object == NULL) |
| 111 |
{ |
| 112 |
ret = FALSE; |
| 113 |
goto out; |
| 114 |
} |
| 115 |
physical_volume = udisks_object_peek_physical_volume (block_object); |
| 116 |
if (physical_volume) |
| 117 |
{ |
| 118 |
volume_group_objpath = udisks_physical_volume_get_volume_group (physical_volume); |
| 119 |
volume_group_object = udisks_daemon_find_object (daemon, volume_group_objpath); |
| 120 |
if (volume_group_object) |
| 121 |
{ |
| 122 |
volume_group = udisks_object_peek_volume_group (volume_group_object); |
| 123 |
if (volume_group) |
| 124 |
volume_group_name = g_strdup (udisks_volume_group_get_name (volume_group)); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
was_partitioned = (udisks_object_peek_partition_table (block_object) != NULL); |
| 129 |
|
| 130 |
device_file = udisks_block_get_device (block); |
| 131 |
|
| 132 |
if (!bd_fs_clean (device_file, FALSE, &local_error)) |
| 133 |
{ |
| 134 |
g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_FAILED, |
| 135 |
"%s", local_error->message); |
| 136 |
g_clear_error (&local_error); |
| 137 |
ret = FALSE; |
| 138 |
goto out; |
| 139 |
} |
| 140 |
|
| 141 |
if (was_partitioned && |
| 142 |
!udisks_linux_block_object_reread_partition_table (UDISKS_LINUX_BLOCK_OBJECT (block_object), error)) |
| 143 |
{ |
| 144 |
ret = FALSE; |
| 145 |
goto out; |
| 146 |
} |
| 147 |
|
| 148 |
/* Try to bring affected volume group back into consistency. */ |
| 149 |
if (volume_group_name != NULL && |
| 150 |
!bd_lvm_vgreduce (volume_group_name, NULL /* device */, NULL /* extra */, &local_error)) |
| 151 |
{ |
| 152 |
udisks_warning ("%s", local_error->message); |
| 153 |
g_clear_error (&local_error); |
| 154 |
} |
| 155 |
|
| 156 |
/* Make sure lvmetad knows about all this. |
| 157 |
* |
| 158 |
* XXX - We need to do this because of a bug in the LVM udev rules |
| 159 |
* which often fail to run pvscan on "change" events. |
| 160 |
* |
| 161 |
* https://bugzilla.redhat.com/show_bug.cgi?id=1063813 |
| 162 |
*/ |
| 163 |
if (!bd_lvm_pvscan (device_file, TRUE, NULL, &local_error)) |
| 164 |
{ |
| 165 |
udisks_warning ("%s", local_error->message); |
| 166 |
g_clear_error (&local_error); |
| 167 |
} |
| 168 |
|
| 169 |
out: |
| 170 |
g_clear_object (&volume_group_object); |
| 171 |
g_clear_object (&block_object); |
| 172 |
g_free (volume_group_name); |
| 173 |
return ret; |
| 174 |
} |
| 175 |
|
| 176 |
/* -------------------------------------------------------------------------------- */ |
| 177 |
|
| 178 |
gboolean |
| 179 |
udisks_daemon_util_lvm2_name_is_reserved (const gchar *name) |
| 180 |
{ |
| 181 |
return (strchr (name, '[') |
| 182 |
|| strchr (name, ']') |
| 183 |
|| strstr (name, "_mlog") |
| 184 |
|| strstr (name, "_mimage") |
| 185 |
|| strstr (name, "_rimage") |
| 186 |
|| strstr (name, "_rmeta") |
| 187 |
|| strstr (name, "_tdata") |
| 188 |
|| strstr (name, "_tmeta") |
| 189 |
|| strstr (name, "_pmspare") |
| 190 |
|| g_str_has_prefix (name, "pvmove") |
| 191 |
|| g_str_has_prefix (name, "snapshot")); |
| 192 |
} |
| 193 |
|
| 194 |
/* -------------------------------------------------------------------------------- */ |
| 195 |
|
| 196 |
void |
| 197 |
udisks_daemon_util_lvm2_trigger_udev (const gchar *device_file) |
| 198 |
{ |
| 199 |
int fd = open (device_file, O_RDWR); |
| 200 |
if (fd >= 0) |
| 201 |
close (fd); |
| 202 |
} |
| 203 |
|
| 204 |
/* -------------------------------------------------------------------------------- */ |
| 205 |
|
| 206 |
GStrv |
| 207 |
udisks_daemon_util_lvm2_gather_pvs (UDisksDaemon *daemon, |
| 208 |
UDisksLinuxVolumeGroupObject *vgroup_object, |
| 209 |
const gchar *const *arg_pvs, |
| 210 |
GError **error) |
| 211 |
{ |
| 212 |
GStrv result = g_new0 (gchar *, g_strv_length ((GStrv)arg_pvs)+1); |
| 213 |
|
| 214 |
for (int i = 0; arg_pvs[i] != NULL; i++) |
| 215 |
{ |
| 216 |
UDisksObject *pvol_object = NULL; |
| 217 |
UDisksBlock *block = NULL; |
| 218 |
UDisksPhysicalVolume *pvol = NULL; |
| 219 |
|
| 220 |
pvol_object = udisks_daemon_find_object (daemon, arg_pvs[i]); |
| 221 |
if (pvol_object == NULL) |
| 222 |
{ |
| 223 |
g_set_error (error, |
| 224 |
UDISKS_ERROR, |
| 225 |
UDISKS_ERROR_FAILED, |
| 226 |
"Invalid object path %s at index %u", |
| 227 |
arg_pvs[i], i); |
| 228 |
goto out; |
| 229 |
} |
| 230 |
|
| 231 |
block = udisks_object_get_block (pvol_object); |
| 232 |
pvol = udisks_object_get_physical_volume (pvol_object); |
| 233 |
if (block == NULL || pvol == NULL) |
| 234 |
{ |
| 235 |
g_set_error (error, |
| 236 |
UDISKS_ERROR, |
| 237 |
UDISKS_ERROR_FAILED, |
| 238 |
"Object path %s for index %u is not a physical volume", |
| 239 |
arg_pvs[i], i); |
| 240 |
if (block) |
| 241 |
g_object_unref (block); |
| 242 |
if (pvol) |
| 243 |
g_object_unref (pvol); |
| 244 |
g_object_unref (pvol_object); |
| 245 |
goto out; |
| 246 |
} |
| 247 |
|
| 248 |
if (g_strcmp0 (udisks_physical_volume_get_volume_group (pvol), |
| 249 |
g_dbus_object_get_object_path (G_DBUS_OBJECT (vgroup_object))) != 0) |
| 250 |
{ |
| 251 |
g_set_error (error, |
| 252 |
UDISKS_ERROR, |
| 253 |
UDISKS_ERROR_FAILED, |
| 254 |
"Physical volume %s for index %u does not belong to this volume group", |
| 255 |
arg_pvs[i], i); |
| 256 |
g_object_unref (pvol_object); |
| 257 |
g_object_unref (pvol); |
| 258 |
g_object_unref (block); |
| 259 |
goto out; |
| 260 |
} |
| 261 |
|
| 262 |
result[i] = udisks_block_dup_device (block); |
| 263 |
g_object_unref (block); |
| 264 |
g_object_unref (pvol); |
| 265 |
g_object_unref (pvol_object); |
| 266 |
} |
| 267 |
|
| 268 |
return result; |
| 269 |
|
| 270 |
out: |
| 271 |
g_strfreev (result); |
| 272 |
return NULL; |
| 273 |
} |