Debian Codegraph @

src/plugins/fs/ext.c

raw
1 /*
2 * Copyright (C) 2017 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 <ext2fs.h>
21 #include <e2p.h>
22
23 #include <blockdev/utils.h>
24 #include <check_deps.h>
25
26 #include "common.h"
27 #include "fs.h"
28 #include "ext.h"
29
30 #define EXT2 "ext2"
31 #define EXT3 "ext3"
32 #define EXT4 "ext4"
33
34 static volatile guint avail_deps = 0;
35 static GMutex deps_check_lock;
36
37 G_GNUC_INTERNAL
38 void _fs_ext_reset_avail_deps (void) {
39 g_atomic_int_set (&avail_deps, 0);
40 }
41
42 #define DEPS_MKE2FS 0
43 #define DEPS_MKE2FS_MASK (1 << DEPS_MKE2FS)
44 #define DEPS_E2FSCK 1
45 #define DEPS_E2FSCK_MASK (1 << DEPS_E2FSCK)
46 #define DEPS_TUNE2FS 2
47 #define DEPS_TUNE2FS_MASK (1 << DEPS_TUNE2FS)
48 #define DEPS_RESIZE2FS 3
49 #define DEPS_RESIZE2FS_MASK (1 << DEPS_RESIZE2FS)
50
51 #define DEPS_LAST 4
52
53 static const UtilDep deps[DEPS_LAST] = {
54 {"mke2fs", NULL, NULL, NULL},
55 {"e2fsck", NULL, NULL, NULL},
56 {"tune2fs", NULL, NULL, NULL},
57 {"resize2fs", NULL, NULL, NULL},
58 };
59
60 static guint32 fs_mode_util[BD_FS_MODE_LAST+1] = {
61 DEPS_MKE2FS_MASK, /* mkfs */
62 0, /* wipe */
63 DEPS_E2FSCK_MASK, /* check */
64 DEPS_E2FSCK_MASK, /* repair */
65 DEPS_TUNE2FS_MASK, /* set-label */
66 0, /* query */
67 DEPS_RESIZE2FS_MASK, /* resize */
68 DEPS_TUNE2FS_MASK /* set-uuid */
69 };
70
71
72 static gint8 compute_percents (guint8 pass_cur, guint8 pass_total, gint val_cur, gint val_total) {
73 gint perc;
74 gint one_pass;
75 /* first get a percentage in the current pass/stage */
76 perc = (val_cur * 100) / val_total;
77
78 /* now map it to the total progress, splitting the stages equally */
79 one_pass = 100 / pass_total;
80 perc = ((pass_cur - 1) * one_pass) + (perc / pass_total);
81
82 return perc;
83 }
84
85 /**
86 * filter_line_fsck: (skip)
87 * Filter one line - decide what to do with it.
88 *
89 * Returns: Zero or positive number as a percentage, -1 if not a percentage, -2 on an error
90 */
91 static gint8 filter_line_fsck (const gchar * line, guint8 total_stages) {
92 static GRegex *output_regex = NULL;
93 GMatchInfo *match_info;
94 gint8 perc = -1;
95 GError *l_error = NULL;
96
97 if (output_regex == NULL) {
98 /* Compile regular expression that matches to e2fsck progress output */
99 output_regex = g_regex_new ("^([0-9][0-9]*) ([0-9][0-9]*) ([0-9][0-9]*) (/.*)", 0, 0, &l_error);
100 if (output_regex == NULL) {
101 bd_utils_log_format (BD_UTILS_LOG_ERR,
102 "Failed to create regex for parsing progress: %s", l_error->message);
103 g_clear_error (&l_error);
104 return -2;
105 }
106 }
107
108 /* Execute regular expression */
109 if (g_regex_match (output_regex, line, 0, &match_info)) {
110 guint8 stage;
111 gint64 val_cur;
112 gint64 val_total;
113 gchar *s;
114
115 /* The output_regex ensures we have a number in these matches, so we can skip
116 * tests for conversion errors.
117 */
118 s = g_match_info_fetch (match_info, 1);
119 stage = (guint8) g_ascii_strtoull (s, (char **)NULL, 10);
120 g_free (s);
121
122 s = g_match_info_fetch (match_info, 2);
123 val_cur = g_ascii_strtoll (s, (char **)NULL, 10);
124 g_free (s);
125
126 s = g_match_info_fetch (match_info, 3);
127 val_total = g_ascii_strtoll (s, (char **)NULL, 10);
128 g_free (s);
129
130 perc = compute_percents (stage, total_stages, val_cur, val_total);
131 } else {
132 g_match_info_free (match_info);
133 bd_utils_log_format (BD_UTILS_LOG_DEBUG,
134 "Failed to parse progress from: %s", line);
135 return -1;
136 }
137 g_match_info_free (match_info);
138 return perc;
139 }
140
141 static gboolean extract_e2fsck_progress (const gchar *line, guint8 *completion) {
142 /* A magic number 5, e2fsck has 5 stages, but this can't be read from the output in advance. */
143 gint8 perc;
144
145 perc = filter_line_fsck (line, 5);
146 if (perc < 0)
147 return FALSE;
148
149 *completion = perc;
150 return TRUE;
151 }
152
153
154 /**
155 * bd_fs_ext_is_tech_avail:
156 * @tech: the queried tech
157 * @mode: a bit mask of queried modes of operation (#BDFSTechMode) for @tech
158 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
159 *
160 * Returns: whether the @tech-@mode combination is available -- supported by the
161 * plugin implementation and having all the runtime dependencies available
162 */
163 G_GNUC_INTERNAL gboolean
164 bd_fs_ext_is_tech_avail (BDFSTech tech G_GNUC_UNUSED, guint64 mode, GError **error) {
165 guint32 required = 0;
166 guint i = 0;
167 for (i = 0; i <= BD_FS_MODE_LAST; i++)
168 if (mode & (1 << i))
169 required |= fs_mode_util[i];
170
171 return check_deps (&avail_deps, required, deps, DEPS_LAST, &deps_check_lock, error);
172 }
173
174 /**
175 * bd_fs_ext2_info_copy: (skip)
176 * @data: (nullable): %BDFSExt2Info to copy
177 *
178 * Creates a new copy of @data.
179 */
180 BDFSExt2Info* bd_fs_ext2_info_copy (BDFSExt2Info *data) {
181 if (data == NULL)
182 return NULL;
183
184 BDFSExt2Info *ret = g_new0 (BDFSExt2Info, 1);
185
186 ret->label = g_strdup (data->label);
187 ret->uuid = g_strdup (data->uuid);
188 ret->state = g_strdup (data->state);
189 ret->block_size = data->block_size;
190 ret->block_count = data->block_count;
191 ret->free_blocks = data->free_blocks;
192
193 return ret;
194 }
195
196 /**
197 * bd_fs_ext3_info_copy: (skip)
198 * @data: (nullable): %BDFSExt3Info to copy
199 *
200 * Creates a new copy of @data.
201 */
202 BDFSExt3Info* bd_fs_ext3_info_copy (BDFSExt3Info *data) {
203 return (BDFSExt3Info*) bd_fs_ext2_info_copy (data);
204 }
205
206 /**
207 * bd_fs_ext4_info_copy: (skip)
208 * @data: (nullable): %BDFSExt4Info to copy
209 *
210 * Creates a new copy of @data.
211 */
212 BDFSExt4Info* bd_fs_ext4_info_copy (BDFSExt4Info *data) {
213 return (BDFSExt4Info*) bd_fs_ext2_info_copy (data);
214 }
215
216 /**
217 * bd_fs_ext2_info_free: (skip)
218 * @data: (nullable): %BDFSExt2Info to free
219 *
220 * Frees @data.
221 */
222 void bd_fs_ext2_info_free (BDFSExt2Info *data) {
223 if (data == NULL)
224 return;
225
226 g_free (data->label);
227 g_free (data->uuid);
228 g_free (data->state);
229 g_free (data);
230 }
231
232 /**
233 * bd_fs_ext3_info_free: (skip)
234 * @data: (nullable): %BDFSExt3Info to free
235 *
236 * Frees @data.
237 */
238 void bd_fs_ext3_info_free (BDFSExt3Info *data) {
239 bd_fs_ext2_info_free ((BDFSExt2Info*) data);
240 }
241
242 /**
243 * bd_fs_ext4_info_free: (skip)
244 * @data: (nullable): %BDFSExt4Info to free
245 *
246 * Frees @data.
247 */
248 void bd_fs_ext4_info_free (BDFSExt4Info *data) {
249 bd_fs_ext2_info_free ((BDFSExt2Info*) data);
250 }
251
252 static BDExtraArg **ext_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra) {
253 GPtrArray *options_array = g_ptr_array_new ();
254 const BDExtraArg **extra_p = NULL;
255
256 if (options->label && g_strcmp0 (options->label, "") != 0)
257 g_ptr_array_add (options_array, bd_extra_arg_new ("-L", options->label));
258
259 if (options->uuid && g_strcmp0 (options->uuid, "") != 0)
260 g_ptr_array_add (options_array, bd_extra_arg_new ("-U", options->uuid));
261
262 if (options->dry_run)
263 g_ptr_array_add (options_array, bd_extra_arg_new ("-n", ""));
264
265 if (options->no_discard)
266 g_ptr_array_add (options_array, bd_extra_arg_new ("-E", "nodiscard"));
267
268 if (options->force)
269 g_ptr_array_add (options_array, bd_extra_arg_new ("-F", ""));
270
271 if (extra) {
272 for (extra_p = extra; *extra_p; extra_p++)
273 g_ptr_array_add (options_array, bd_extra_arg_copy ((BDExtraArg *) *extra_p));
274 }
275
276 g_ptr_array_add (options_array, NULL);
277
278 return (BDExtraArg **) g_ptr_array_free (options_array, FALSE);
279 }
280
281 G_GNUC_INTERNAL BDExtraArg **
282 bd_fs_ext2_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra) {
283 return ext_mkfs_options (options, extra);
284 }
285
286 G_GNUC_INTERNAL BDExtraArg **
287 bd_fs_ext3_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra) {
288 return ext_mkfs_options (options, extra);
289 }
290
291 G_GNUC_INTERNAL BDExtraArg **
292 bd_fs_ext4_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra) {
293 return ext_mkfs_options (options, extra);
294 }
295
296 static gboolean ext_mkfs (const gchar *device, const BDExtraArg **extra, const gchar *ext_version, GError **error) {
297 const gchar *args[5] = {"mke2fs", "-t", ext_version, device, NULL};
298
299 if (!check_deps (&avail_deps, DEPS_MKE2FS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
300 return FALSE;
301
302 return bd_utils_exec_and_report_error (args, extra, error);
303 }
304
305 /**
306 * bd_fs_ext2_mkfs:
307 * @device: the device to create a new ext2 fs on
308 * @extra: (nullable) (array zero-terminated=1): extra options for the creation (right now
309 * passed to the 'mke2fs' utility)
310 * @error: (out) (optional): place to store error (if any)
311 *
312 * Returns: whether a new ext2 fs was successfully created on @device or not
313 *
314 * Tech category: %BD_FS_TECH_EXT2-%BD_FS_TECH_MODE_MKFS
315 */
316 gboolean bd_fs_ext2_mkfs (const gchar *device, const BDExtraArg **extra, GError **error) {
317 return ext_mkfs (device, extra, EXT2, error);
318 }
319
320 /**
321 * bd_fs_ext3_mkfs:
322 * @device: the device to create a new ext3 fs on
323 * @extra: (nullable) (array zero-terminated=1): extra options for the creation (right now
324 * passed to the 'mke2fs' utility)
325 * @error: (out) (optional): place to store error (if any)
326 *
327 * Returns: whether a new ext3 fs was successfully created on @device or not
328 *
329 * Tech category: %BD_FS_TECH_EXT3-%BD_FS_TECH_MODE_MKFS
330 */
331 gboolean bd_fs_ext3_mkfs (const gchar *device, const BDExtraArg **extra, GError **error) {
332 return ext_mkfs (device, extra, EXT3, error);
333 }
334
335 /**
336 * bd_fs_ext4_mkfs:
337 * @device: the device to create a new ext4 fs on
338 * @extra: (nullable) (array zero-terminated=1): extra options for the creation (right now
339 * passed to the 'mkfs.ext4' utility)
340 * @error: (out) (optional): place to store error (if any)
341 *
342 * Returns: whether a new ext4 fs was successfully created on @device or not
343 *
344 * Tech category: %BD_FS_TECH_EXT4-%BD_FS_TECH_MODE_MKFS
345 */
346 gboolean bd_fs_ext4_mkfs (const gchar *device, const BDExtraArg **extra, GError **error) {
347 return ext_mkfs (device, extra, EXT4, error);
348 }
349
350 static gboolean ext_check (const gchar *device, const BDExtraArg **extra, GError **error) {
351 /* Force checking even if the file system seems clean. AND
352 * Open the filesystem read-only, and assume an answer of no to all
353 * questions. */
354 const gchar *args_progress[7] = {"e2fsck", "-f", "-n", "-C", "1", device, NULL};
355 const gchar *args[5] = {"e2fsck", "-f", "-n", device, NULL};
356 gint status = 0;
357 gboolean ret = FALSE;
358
359 if (!check_deps (&avail_deps, DEPS_E2FSCK_MASK, deps, DEPS_LAST, &deps_check_lock, error))
360 return FALSE;
361
362 if (bd_utils_prog_reporting_initialized ()) {
363 ret = bd_utils_exec_and_report_progress (args_progress, extra, extract_e2fsck_progress, &status, error);
364 } else {
365 ret = bd_utils_exec_and_report_status_error (args, extra, &status, error);
366 }
367
368 if (!ret && (status == 4)) {
369 /* no error should be reported for exit code 4 - File system errors left uncorrected */
370 g_clear_error (error);
371 }
372 return ret;
373 }
374
375 /**
376 * bd_fs_ext2_check:
377 * @device: the device the file system on which to check
378 * @extra: (nullable) (array zero-terminated=1): extra options for the check (right now
379 * passed to the 'e2fsck' utility)
380 * @error: (out) (optional): place to store error (if any)
381 *
382 * Returns: whether an ext2 file system on the @device is clean or not
383 *
384 * Tech category: %BD_FS_TECH_EXT2-%BD_FS_TECH_MODE_CHECK
385 */
386 gboolean bd_fs_ext2_check (const gchar *device, const BDExtraArg **extra, GError **error) {
387 return ext_check (device, extra, error);
388 }
389
390 /**
391 * bd_fs_ext3_check:
392 * @device: the device the file system on which to check
393 * @extra: (nullable) (array zero-terminated=1): extra options for the check (right now
394 * passed to the 'e2fsck' utility)
395 * @error: (out) (optional): place to store error (if any)
396 *
397 * Returns: whether an ext3 file system on the @device is clean or not
398 *
399 * Tech category: %BD_FS_TECH_EXT3-%BD_FS_TECH_MODE_CHECK
400 */
401 gboolean bd_fs_ext3_check (const gchar *device, const BDExtraArg **extra, GError **error) {
402 return ext_check (device, extra, error);
403 }
404
405 /**
406 * bd_fs_ext4_check:
407 * @device: the device the file system on which to check
408 * @extra: (nullable) (array zero-terminated=1): extra options for the check (right now
409 * passed to the 'e2fsck' utility)
410 * @error: (out) (optional): place to store error (if any)
411 *
412 * Returns: whether an ext4 file system on the @device is clean or not
413 *
414 * Tech category: %BD_FS_TECH_EXT4-%BD_FS_TECH_MODE_CHECK
415 */
416 gboolean bd_fs_ext4_check (const gchar *device, const BDExtraArg **extra, GError **error) {
417 return ext_check (device, extra, error);
418 }
419
420 static gboolean ext_repair (const gchar *device, gboolean unsafe, const BDExtraArg **extra, GError **error) {
421 /* Force checking even if the file system seems clean. AND
422 * Automatically repair what can be safely repaired. OR
423 * Assume an answer of `yes' to all questions. */
424 const gchar *args_progress[7] = {"e2fsck", "-f", unsafe ? "-y" : "-p", "-C", "1", device, NULL};
425 const gchar *args[5] = {"e2fsck", "-f", unsafe ? "-y" : "-p", device, NULL};
426 gint status = 0;
427 gboolean ret = FALSE;
428
429 if (!check_deps (&avail_deps, DEPS_E2FSCK_MASK, deps, DEPS_LAST, &deps_check_lock, error))
430 return FALSE;
431
432 if (bd_utils_prog_reporting_initialized ()) {
433 ret = bd_utils_exec_and_report_progress (args_progress, extra, extract_e2fsck_progress, &status, error);
434 } else {
435 ret = bd_utils_exec_and_report_status_error (args, extra, &status, error);
436 }
437
438 if (!ret) {
439 if (status == 1) {
440 /* no error should be reported for exit code 1 - File system errors corrected */
441 g_clear_error (error);
442 ret = TRUE;
443 } else if (status == 2) {
444 /* no error should be reported for exit code 2 - File system errors corrected, system should be rebooted */
445 bd_utils_log_format (BD_UTILS_LOG_WARNING,
446 "File system errors on %s were successfully corrected, but system reboot is advised.",
447 device);
448 g_clear_error (error);
449 ret = TRUE;
450 }
451 }
452 return ret;
453 }
454
455 /**
456 * bd_fs_ext2_repair:
457 * @device: the device the file system on which to repair
458 * @unsafe: whether to do unsafe operations too
459 * @extra: (nullable) (array zero-terminated=1): extra options for the repair (right now
460 * passed to the 'e2fsck' utility)
461 * @error: (out) (optional): place to store error (if any)
462 *
463 * Returns: whether an ext2 file system on the @device was successfully repaired
464 * (if needed) or not (error is set in that case)
465 *
466 * Tech category: %BD_FS_TECH_EXT2-%BD_FS_TECH_MODE_REPAIR
467 */
468 gboolean bd_fs_ext2_repair (const gchar *device, gboolean unsafe, const BDExtraArg **extra, GError **error) {
469 return ext_repair (device, unsafe, extra, error);
470 }
471
472 /**
473 * bd_fs_ext3_repair:
474 * @device: the device the file system on which to repair
475 * @unsafe: whether to do unsafe operations too
476 * @extra: (nullable) (array zero-terminated=1): extra options for the repair (right now
477 * passed to the 'e2fsck' utility)
478 * @error: (out) (optional): place to store error (if any)
479 *
480 * Returns: whether an ext3 file system on the @device was successfully repaired
481 * (if needed) or not (error is set in that case)
482 *
483 * Tech category: %BD_FS_TECH_EXT3-%BD_FS_TECH_MODE_REPAIR
484 */
485 gboolean bd_fs_ext3_repair (const gchar *device, gboolean unsafe, const BDExtraArg **extra, GError **error) {
486 return ext_repair (device, unsafe, extra, error);
487 }
488
489 /**
490 * bd_fs_ext4_repair:
491 * @device: the device the file system on which to repair
492 * @unsafe: whether to do unsafe operations too
493 * @extra: (nullable) (array zero-terminated=1): extra options for the repair (right now
494 * passed to the 'e2fsck' utility)
495 * @error: (out) (optional): place to store error (if any)
496 *
497 * Returns: whether an ext4 file system on the @device was successfully repaired
498 * (if needed) or not (error is set in that case)
499 *
500 * Tech category: %BD_FS_TECH_EXT4-%BD_FS_TECH_MODE_REPAIR
501 */
502 gboolean bd_fs_ext4_repair (const gchar *device, gboolean unsafe, const BDExtraArg **extra, GError **error) {
503 return ext_repair (device, unsafe, extra, error);
504 }
505
506 static gboolean ext_set_label (const gchar *device, const gchar *label, GError **error) {
507 const gchar *args[5] = {"tune2fs", "-L", label, device, NULL};
508
509 if (!check_deps (&avail_deps, DEPS_TUNE2FS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
510 return FALSE;
511
512 return bd_utils_exec_and_report_error (args, NULL, error);
513 }
514
515 /**
516 * bd_fs_ext2_set_label:
517 * @device: the device the file system on which to set label for
518 * @label: label to set
519 * @error: (out) (optional): place to store error (if any)
520 *
521 * Returns: whether the label of ext2 file system on the @device was
522 * successfully set or not
523 *
524 * Tech category: %BD_FS_TECH_EXT2-%BD_FS_TECH_MODE_SET_LABEL
525 */
526 gboolean bd_fs_ext2_set_label (const gchar *device, const gchar *label, GError **error) {
527 return ext_set_label (device, label, error);
528 }
529
530 /**
531 * bd_fs_ext3_set_label:
532 * @device: the device the file system on which to set label for
533 * @label: label to set
534 * @error: (out) (optional): place to store error (if any)
535 *
536 * Returns: whether the label of ext3 file system on the @device was
537 * successfully set or not
538 *
539 * Tech category: %BD_FS_TECH_EXT3-%BD_FS_TECH_MODE_SET_LABEL
540 */
541 gboolean bd_fs_ext3_set_label (const gchar *device, const gchar *label, GError **error) {
542 return ext_set_label (device, label, error);
543 }
544
545 /**
546 * bd_fs_ext4_set_label:
547 * @device: the device the file system on which to set label for
548 * @label: label to set
549 * @error: (out) (optional): place to store error (if any)
550 *
551 * Returns: whether the label of ext4 file system on the @device was
552 * successfully set or not
553 *
554 * Tech category: %BD_FS_TECH_EXT4-%BD_FS_TECH_MODE_SET_LABEL
555 */
556 gboolean bd_fs_ext4_set_label (const gchar *device, const gchar *label, GError **error) {
557 return ext_set_label (device, label, error);
558 }
559
560 /**
561 * bd_fs_ext2_check_label:
562 * @label: label to check
563 * @error: (out) (optional): place to store error
564 *
565 * Returns: whether @label is a valid label for the ext2 file system or not
566 * (reason is provided in @error)
567 *
568 * Tech category: always available
569 */
570 gboolean bd_fs_ext2_check_label (const gchar *label, GError **error) {
571 if (strlen (label) > 16) {
572 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID,
573 "Label for ext filesystem must be at most 16 characters long.");
574 return FALSE;
575 }
576
577 return TRUE;
578 }
579
580 /**
581 * bd_fs_ext3_check_label:
582 * @label: label to check
583 * @error: (out) (optional): place to store error (if any)
584 *
585 * Returns: whether @label is a valid label for the ext3 file system or not
586 * (reason is provided in @error)
587 *
588 * Tech category: always available
589 */
590 gboolean bd_fs_ext3_check_label (const gchar *label, GError **error) {
591 return bd_fs_ext2_check_label (label, error);
592 }
593
594 /**
595 * bd_fs_ext4_check_label:
596 * @label: label to check
597 * @error: (out) (optional): place to store error (if any)
598 *
599 * Returns: whether @label is a valid label for the ext4 file system or not
600 * (reason is provided in @error)
601 *
602 * Tech category: always available
603 */
604 gboolean bd_fs_ext4_check_label (const gchar *label, GError **error) {
605 return bd_fs_ext2_check_label (label, error);
606 }
607
608 static gboolean ext_set_uuid (const gchar *device, const gchar *uuid, GError **error) {
609 const gchar *args[5] = {"tune2fs", "-U", NULL, device, NULL};
610
611 if (!check_deps (&avail_deps, DEPS_TUNE2FS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
612 return FALSE;
613
614 if (!uuid)
615 args[2] = "random";
616 else
617 args[2] = uuid;
618
619 return bd_utils_exec_and_report_error (args, NULL, error);
620 }
621
622 /**
623 * bd_fs_ext2_set_uuid:
624 * @device: the device the file system on which to set UUID for
625 * @uuid: (nullable): UUID to set %NULL to generate a new one
626 * UUID can also be one of "clear", "random" and "time" to clear,
627 * generate a new random/time-based UUID
628 * @error: (out) (optional): place to store error (if any)
629 *
630 * Returns: whether the UUID of ext2 file system on the @device was
631 * successfully set or not
632 *
633 * Tech category: %BD_FS_TECH_EXT2-%BD_FS_TECH_MODE_SET_UUID
634 */
635 gboolean bd_fs_ext2_set_uuid (const gchar *device, const gchar *uuid, GError **error) {
636 return ext_set_uuid (device, uuid, error);
637 }
638
639 /**
640 * bd_fs_ext3_set_uuid:
641 * @device: the device the file system on which to set UUID for
642 * @uuid: (nullable): UUID to set %NULL to generate a new one
643 * UUID can also be one of "clear", "random" and "time" to clear,
644 * generate a new random/time-based UUID
645 * @error: (out) (optional): place to store error (if any)
646 *
647 * Returns: whether the UUID of ext3 file system on the @device was
648 * successfully set or not
649 *
650 * Tech category: %BD_FS_TECH_EXT3-%BD_FS_TECH_MODE_SET_UUID
651 */
652 gboolean bd_fs_ext3_set_uuid (const gchar *device, const gchar *uuid, GError **error) {
653 return ext_set_uuid (device, uuid, error);
654 }
655
656 /**
657 * bd_fs_ext4_set_uuid:
658 * @device: the device the file system on which to set UUID for
659 * @uuid: (nullable): UUID to set %NULL to generate a new one
660 * UUID can also be one of "clear", "random" and "time" to clear,
661 * generate a new random/time-based UUID
662 * @error: (out) (optional): place to store error (if any)
663 *
664 * Returns: whether the UUID of ext4 file system on the @device was
665 * successfully set or not
666 *
667 * Tech category: %BD_FS_TECH_EXT4-%BD_FS_TECH_MODE_SET_UUID
668 */
669 gboolean bd_fs_ext4_set_uuid (const gchar *device, const gchar *uuid, GError **error) {
670 return ext_set_uuid (device, uuid, error);
671 }
672
673 /**
674 * bd_fs_ext2_check_uuid:
675 * @uuid: UUID to check
676 * @error: (out) (optional): place to store error
677 *
678 * Returns: whether @uuid is a valid UUID for the ext2 file system or not
679 * (reason is provided in @error)
680 *
681 * Tech category: always available
682 */
683 gboolean bd_fs_ext2_check_uuid (const gchar *uuid, GError **error) {
684 return check_uuid (uuid, error);
685 }
686
687 /**
688 * bd_fs_ext3_check_uuid:
689 * @uuid: UUID to check
690 * @error: (out) (optional): place to store error
691 *
692 * Returns: whether @uuid is a valid UUID for the ext3 file system or not
693 * (reason is provided in @error)
694 *
695 * Tech category: always available
696 */
697 gboolean bd_fs_ext3_check_uuid (const gchar *uuid, GError **error) {
698 return check_uuid (uuid, error);
699 }
700
701 /**
702 * bd_fs_ext4_check_uuid:
703 * @uuid: UUID to check
704 * @error: (out) (optional): place to store error
705 *
706 * Returns: whether @uuid is a valid UUID for the ext4 file system or not
707 * (reason is provided in @error)
708 *
709 * Tech category: always available
710 */
711 gboolean bd_fs_ext4_check_uuid (const gchar *uuid, GError **error) {
712 return check_uuid (uuid, error);
713 }
714
715 static gchar *decode_fs_state (unsigned short state) {
716 return g_strdup_printf ("%s%s",
717 (state & EXT2_VALID_FS) ? "clean" : "not clean",
718 (state & EXT2_ERROR_FS) ? " with errors" : "");
719 }
720
721 static gchar *decode_uuid (void *uuid) {
722 const char *str = e2p_uuid2str (uuid);
723 if (g_strcmp0 (str, "<none>") == 0)
724 str = "";
725 return g_strdup (str);
726 }
727
728 static BDFSExtInfo* ext_get_info (const gchar *device, GError **error) {
729 errcode_t retval;
730 ext2_filsys fs;
731 struct ext2_super_block *sb;
732 BDFSExtInfo *ret = NULL;
733
734 int flags = (EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES |
735 EXT2_FLAG_64BITS | EXT2_FLAG_SUPER_ONLY |
736 EXT2_FLAG_IGNORE_CSUM_ERRORS);
737
738 #ifdef EXT2_FLAG_THREADS
739 flags |= EXT2_FLAG_THREADS;
740 #endif
741
742 retval = ext2fs_open (device,
743 flags,
744 0, /* use_superblock */
745 0, /* use_blocksize */
746 unix_io_manager,
747 &fs);
748 if (retval) {
749 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, "Failed to open ext4 file system");
750 return NULL;
751 }
752
753 sb = fs->super;
754 ret = g_new0 (BDFSExtInfo, 1);
755
756 ret->label = g_strndup ((gchar *)sb->s_volume_name, sizeof (sb->s_volume_name));
757 ret->uuid = decode_uuid (sb->s_uuid);
758 ret->state = decode_fs_state (sb->s_state);
759 ret->block_size = EXT2_BLOCK_SIZE (sb);
760 ret->block_count = ext2fs_blocks_count (sb);
761 ret->free_blocks = ext2fs_free_blocks_count (sb);
762
763 ext2fs_close_free (&fs);
764 return ret;
765 }
766
767 /**
768 * bd_fs_ext2_get_info:
769 * @device: the device the file system of which to get info for
770 * @error: (out) (optional): place to store error (if any)
771 *
772 * Returns: (transfer full): information about the file system on @device or
773 * %NULL in case of error
774 *
775 * Tech category: %BD_FS_TECH_EXT2-%BD_FS_TECH_MODE_QUERY
776 */
777 BDFSExt2Info* bd_fs_ext2_get_info (const gchar *device, GError **error) {
778 return (BDFSExt2Info*) ext_get_info (device, error);
779 }
780
781 /**
782 * bd_fs_ext3_get_info:
783 * @device: the device the file system of which to get info for
784 * @error: (out) (optional): place to store error (if any)
785 *
786 * Returns: (transfer full): information about the file system on @device or
787 * %NULL in case of error
788 *
789 * Tech category: %BD_FS_TECH_EXT3-%BD_FS_TECH_MODE_QUERY
790 */
791 BDFSExt3Info* bd_fs_ext3_get_info (const gchar *device, GError **error) {
792 return (BDFSExt3Info*) ext_get_info (device, error);
793 }
794
795 /**
796 * bd_fs_ext4_get_info:
797 * @device: the device the file system of which to get info for
798 * @error: (out) (optional): place to store error (if any)
799 *
800 * Returns: (transfer full): information about the file system on @device or
801 * %NULL in case of error
802 *
803 * Tech category: %BD_FS_TECH_EXT4-%BD_FS_TECH_MODE_QUERY
804 */
805 BDFSExt4Info* bd_fs_ext4_get_info (const gchar *device, GError **error) {
806 return (BDFSExt4Info*) ext_get_info (device, error);
807 }
808
809 static gboolean ext_resize (const gchar *device, guint64 new_size, const BDExtraArg **extra, GError **error) {
810 const gchar *args[4] = {"resize2fs", device, NULL, NULL};
811 gboolean ret = FALSE;
812
813 if (!check_deps (&avail_deps, DEPS_RESIZE2FS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
814 return FALSE;
815
816 if (new_size != 0)
817 /* resize2fs doesn't understand bytes, just 512B sectors */
818 args[2] = g_strdup_printf ("%"G_GUINT64_FORMAT"s", new_size / 512);
819 ret = bd_utils_exec_and_report_error (args, extra, error);
820
821 g_free ((gchar *) args[2]);
822 return ret;
823 }
824
825 /**
826 * bd_fs_ext2_resize:
827 * @device: the device the file system of which to resize
828 * @new_size: new requested size for the file system (if 0, the file system is
829 * adapted to the underlying block device)
830 * @extra: (nullable) (array zero-terminated=1): extra options for the resize (right now
831 * passed to the 'resize2fs' utility)
832 * @error: (out) (optional): place to store error (if any)
833 *
834 * Returns: whether the file system on @device was successfully resized or not
835 *
836 * Tech category: %BD_FS_TECH_EXT2-%BD_FS_TECH_MODE_RESIZE
837 */
838 gboolean bd_fs_ext2_resize (const gchar *device, guint64 new_size, const BDExtraArg **extra, GError **error) {
839 return ext_resize (device, new_size, extra, error);
840 }
841
842 /**
843 * bd_fs_ext3_resize:
844 * @device: the device the file system of which to resize
845 * @new_size: new requested size for the file system (if 0, the file system is
846 * adapted to the underlying block device)
847 * @extra: (nullable) (array zero-terminated=1): extra options for the resize (right now
848 * passed to the 'resize2fs' utility)
849 * @error: (out) (optional): place to store error (if any)
850 *
851 * Returns: whether the file system on @device was successfully resized or not
852 *
853 * Tech category: %BD_FS_TECH_EXT3-%BD_FS_TECH_MODE_RESIZE
854 */
855 gboolean bd_fs_ext3_resize (const gchar *device, guint64 new_size, const BDExtraArg **extra, GError **error) {
856 return ext_resize (device, new_size, extra, error);
857 }
858
859 /**
860 * bd_fs_ext4_resize:
861 * @device: the device the file system of which to resize
862 * @new_size: new requested size for the file system (if 0, the file system is
863 * adapted to the underlying block device)
864 * @extra: (nullable) (array zero-terminated=1): extra options for the resize (right now
865 * passed to the 'resize2fs' utility)
866 * @error: (out) (optional): place to store error (if any)
867 *
868 * Returns: whether the file system on @device was successfully resized or not
869 *
870 * Tech category: %BD_FS_TECH_EXT4-%BD_FS_TECH_MODE_RESIZE
871 */
872 gboolean bd_fs_ext4_resize (const gchar *device, guint64 new_size, const BDExtraArg **extra, GError **error) {
873 return ext_resize (device, new_size, extra, error);
874 }
875
876 static guint64 ext_get_min_size (const gchar *device, GError **error) {
877 const gchar *args[4] = {"resize2fs", "-P", device, NULL};
878 gboolean success = FALSE;
879 gchar *output = NULL;
880 gchar **lines = NULL;
881 gchar **line_p = NULL;
882 guint64 min_size = 0;
883 gchar **key_val = NULL;
884 BDFSExtInfo *info = NULL;
885
886 if (!check_deps (&avail_deps, DEPS_RESIZE2FS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
887 return FALSE;
888
889 info = ext_get_info (device, error);
890 if (!info)
891 return 0;
892
893 success = bd_utils_exec_and_capture_output (args, NULL, &output, error);
894 if (!success) {
895 /* error is already populated */
896 bd_fs_ext2_info_free (info);
897 return 0;
898 }
899
900 lines = g_strsplit (output, "\n", 0);
901 g_free (output);
902
903 for (line_p=lines; *line_p; line_p++) {
904 if (g_str_has_prefix (*line_p, "Estimated minimum size")) {
905 key_val = g_strsplit (*line_p, ":", 2);
906 if (g_strv_length (key_val) == 2) {
907 min_size = g_ascii_strtoull (key_val[1], NULL, 0) * info->block_size;
908 g_strfreev (lines);
909 g_strfreev (key_val);
910 bd_fs_ext2_info_free (info);
911 return min_size;
912 } else {
913 g_strfreev (key_val);
914 break;
915 }
916 }
917 }
918
919 g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL,
920 "Failed to get minimum size for '%s'", device);
921 g_strfreev (lines);
922 bd_fs_ext2_info_free (info);
923 return 0;
924 }
925
926 /**
927 * bd_fs_ext2_get_min_size:
928 * @device: the device containing the file system to get min size for
929 * @error: (out) (optional): place to store error (if any)
930 *
931 * Returns: smallest shrunken filesystem size as reported by resize2fs
932 * in case of error 0 is returned and @error is set
933 *
934 * Tech category: %BD_FS_TECH_EXT2-%BD_FS_TECH_MODE_RESIZE
935 */
936 guint64 bd_fs_ext2_get_min_size (const gchar *device, GError **error) {
937 return ext_get_min_size (device, error);
938 }
939
940 /**
941 * bd_fs_ext3_get_min_size:
942 * @device: the device containing the file system to get min size for
943 * @error: (out) (optional): place to store error (if any)
944 *
945 * Returns: smallest shrunken filesystem size as reported by resize2fs
946 * in case of error 0 is returned and @error is set
947 *
948 * Tech category: %BD_FS_TECH_EXT3-%BD_FS_TECH_MODE_RESIZE
949 */
950 guint64 bd_fs_ext3_get_min_size (const gchar *device, GError **error) {
951 return ext_get_min_size (device, error);
952 }
953
954 /**
955 * bd_fs_ext4_get_min_size:
956 * @device: the device containing the file system to get min size for
957 * @error: (out) (optional): place to store error (if any)
958 *
959 * Returns: smallest shrunken filesystem size as reported by resize2fs
960 * in case of error 0 is returned and @error is set
961 *
962 * Tech category: %BD_FS_TECH_EXT4-%BD_FS_TECH_MODE_RESIZE
963 */
964 guint64 bd_fs_ext4_get_min_size (const gchar *device, GError **error) {
965 return ext_get_min_size (device, error);
966 }