summaryrefslogtreecommitdiffstats
path: root/misc-utils/findmnt-verify.c
blob: 7281c8df51cb2ec80a0f2f248b2d712f7d3c124d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libmount.h>
#include <blkid.h>
#include <sys/utsname.h>

#include "nls.h"
#include "c.h"
#include "strutils.h"
#include "xalloc.h"
#include "pathnames.h"
#include "match.h"

#include "findmnt.h"

struct verify_context {
	struct libmnt_fs	*fs;
	struct libmnt_table	*tb;

	char	**fs_ary;
	size_t	fs_num;
	size_t  fs_alloc;

	int	nwarnings;
	int	nerrors;

	unsigned int	target_printed : 1,
			no_fsck : 1;
};

static void __attribute__ ((__format__ (__printf__, 3, 0)))
	verify_mesg(struct verify_context *vfy, char type, const char *fmt, va_list ap)
{
	if (!vfy->target_printed && vfy->fs) {
		fprintf(stdout, "%s\n", mnt_fs_get_target(vfy->fs));
		vfy->target_printed = 1;
	}

	fprintf(stdout, "   [%c] ", type);
	vfprintf(stdout, fmt, ap);
	fputc('\n', stdout);
}

static int __attribute__ ((__format__ (__printf__, 2, 3)))
	verify_warn(struct verify_context *vfy, const char *fmt, ...)
{
	va_list ap;
	vfy->nwarnings++;
	va_start(ap, fmt);
	verify_mesg(vfy, 'W', fmt, ap);
	va_end(ap);
	return 0;
}

static int __attribute__ ((__format__ (__printf__, 2, 3)))
	verify_err(struct verify_context *vfy, const char *fmt, ...)
{
	va_list ap;
	vfy->nerrors++;
	va_start(ap, fmt);
	verify_mesg(vfy, 'E', fmt, ap);
	va_end(ap);
	return 0;
}

static int __attribute__ ((__format__ (__printf__, 2, 3)))
	verify_ok(struct verify_context *vfy __attribute__((unused)),
		      const char *fmt, ...)
{
	va_list ap;

	if (!(flags & FL_VERBOSE))
		return 0;

	va_start(ap, fmt);
	verify_mesg(vfy, ' ', fmt, ap);
	va_end(ap);
	return 0;
}

static int verify_order(struct verify_context *vfy)
{
	struct libmnt_iter *itr = NULL;
	struct libmnt_fs *next;
	const char *tgt;

	tgt = mnt_fs_get_target(vfy->fs);
	if (tgt && !(flags & FL_NOCACHE))
		tgt  = mnt_resolve_target(tgt, cache);
	else if (!tgt)
		return 0;

	itr = mnt_new_iter(MNT_ITER_FORWARD);
	if (!itr) {
		warn(_("failed to initialize libmount iterator"));
		goto done;
	}

	/* set iterator position to 'fs' */
	mnt_table_set_iter(vfy->tb, itr, vfy->fs);

	if (mnt_table_next_fs(vfy->tb, itr, &next) != 0)
		goto done;

	/* scan all next filesystems */
	while (mnt_table_next_fs(vfy->tb, itr, &next) == 0) {
		const char *n_tgt;
		size_t len;

		n_tgt = mnt_fs_get_target(next);
		if (n_tgt && !(flags & FL_NOCACHE))
			n_tgt  = mnt_resolve_target(n_tgt, cache);
		else if (!n_tgt)
			continue;
		len = strlen(n_tgt);

		if (strncmp(n_tgt, tgt, len) == 0) {
			if (*(tgt + len) == '\0')
				verify_warn(vfy, _("target specified more than once"));
			else if (*(tgt + len) == '/')
				verify_err(vfy, _("wrong order: %s specified before %s"), tgt, n_tgt);
		}
	}
done:
	mnt_free_iter(itr);
	return 0;
}

static int verify_target(struct verify_context *vfy)
{
	const char *tgt = mnt_fs_get_target(vfy->fs);
	struct stat sb;

	if (!tgt)
		return verify_err(vfy, _("undefined target (fs_file)"));

	if (!(flags & FL_NOCACHE)) {
		const char *cn = mnt_resolve_target(tgt, cache);
		if (!cn)
			return -ENOMEM;
		if (strcmp(cn, tgt) != 0)
			verify_warn(vfy, _("non-canonical target path (real: %s)"), cn);
		tgt = cn;
	}
	if (stat(tgt, &sb) != 0) {
		if (mnt_fs_get_option(vfy->fs, "noauto", NULL, NULL) == 1)
			verify_err(vfy, _("unreachable on boot required target: %m"));
		else
			verify_warn(vfy, _("unreachable target: %m"));

	} else if (!S_ISDIR(sb.st_mode)
		 && mnt_fs_get_option(vfy->fs, "bind", NULL, NULL) == 1) {
		verify_err(vfy, _("target is not a directory"));
	} else
		verify_ok(vfy, _("target exists"));

	return 0;
}

static char *verify_tag(struct verify_context *vfy, const char *name,
		      const char *value)
{
	char *src = NULL;

	if (!(flags & FL_NOCACHE))
		src = mnt_resolve_tag(name, value, cache);

	if (!src) {
		if (mnt_fs_get_option(vfy->fs, "noauto", NULL, NULL) == 1)
			verify_err(vfy, _("unreachable on boot required source: %s=%s"), name, value);
		else
			verify_warn(vfy, _("unreachable: %s=%s"), name, value);
	} else
		verify_ok(vfy, _("%s=%s translated to %s"), name, value, src);

	return src;
}

/* Note that mount source is very FS specific and we should not
 * interpret unreachable source as error. The exception is only
 * NAME=value, this has to be convertible to device name.
 */
static int verify_source(struct verify_context *vfy)
{
	const char *src = mnt_fs_get_srcpath(vfy->fs);
	char *t = NULL, *v = NULL;
	struct stat sb;
	int isbind, rc = 0;

	/* source is NAME=value tag */
	if (!src) {
		const char *tag = NULL, *val = NULL;

		if (mnt_fs_get_tag(vfy->fs, &tag, &val) != 0)
			return verify_err(vfy, _("undefined source (fs_spec)"));

		src = verify_tag(vfy, tag, val);
		if (!src)
			goto done;

	/* blkid is able to parse it, but libmount does not see it as a tag --
	 * it means unsupported tag */
	} else if (blkid_parse_tag_string(src, &t, &v) == 0 && stat(src, &sb) != 0) {
		rc = verify_err(vfy, _("unsupported source tag: %s"), src);
		goto done;
	}
	isbind = mnt_fs_get_option(vfy->fs, "bind", NULL, NULL) == 0;

	/* source is path */
	if (mnt_fs_is_pseudofs(vfy->fs) || mnt_fs_is_netfs(vfy->fs))
		verify_ok(vfy, _("do not check %s source (pseudo/net)"), src);

	else if (stat(src, &sb) != 0)
		verify_warn(vfy, _("unreachable source: %s: %m"), src);

	else if ((S_ISDIR(sb.st_mode) || S_ISREG(sb.st_mode)) && !isbind)
		verify_warn(vfy, _("non-bind mount source %s is a directory or regular file"), src);

	else if (!S_ISBLK(sb.st_mode) && !isbind)
		verify_warn(vfy, _("source %s is not a block device"), src);
	else
		verify_ok(vfy, _("source %s exists"), src);
done:
	free(t);
	free(v);
	return rc;
}

static int verify_options(struct verify_context *vfy)
{
	const char *opts;

	opts = mnt_fs_get_vfs_options(vfy->fs);
	if (opts)
		verify_ok(vfy, _("VFS options: %s"), opts);

	opts = mnt_fs_get_fs_options(vfy->fs);
	if (opts)
		verify_ok(vfy, _("FS options: %s"), opts);

	opts = mnt_fs_get_user_options(vfy->fs);
	if (opts)
		verify_ok(vfy, _("userspace options: %s"), opts);

	return 0;
}

static int verify_swaparea(struct verify_context *vfy)
{
	char *arg;
	size_t argsz = 0;

	if (mnt_fs_get_option(vfy->fs, "discard", &arg, &argsz) == 0
	    && arg
	    && strncmp(arg, "once", argsz) != 0
	    && strncmp(arg, "pages", argsz) != 0)
		verify_err(vfy, _("unsupported swaparea discard policy: %s"), arg);

	if (mnt_fs_get_option(vfy->fs, "pri", &arg, &argsz) == 0 && arg) {
		char *p = arg;
		if (*p == '-')
			p++;
		for (; p < arg + argsz; p++) {
			if (!isdigit((unsigned char) *p)) {
				verify_err(vfy, _("failed to parse swaparea priority option"));
				break;
			}
		}
	}

	return 0;
}

static int is_supported_filesystem(struct verify_context *vfy, const char *name)
{
	size_t n;

	if (!vfy->fs_num)
		return 0;

	for (n = 0; n < vfy->fs_num; n++ ) {
		if (match_fstype(vfy->fs_ary[n], name))
			return 1;
	}

	return 0;
}

static int add_filesystem(struct verify_context *vfy, const char *name)
{
	#define MYCHUNK	16

	if (is_supported_filesystem(vfy, name))
		return 0;

	if (vfy->fs_alloc == 0 || vfy->fs_num + 1 <= vfy->fs_alloc) {
		vfy->fs_alloc = ((vfy->fs_alloc + 1 + MYCHUNK) / MYCHUNK) * MYCHUNK;
		vfy->fs_ary = xreallocarray(vfy->fs_ary, vfy->fs_alloc, sizeof(char *));
	}

	vfy->fs_ary[vfy->fs_num] = xstrdup(name);
	vfy->fs_num++;

	return 0;
}

static int read_proc_filesystems(struct verify_context *vfy)
{
	int rc = 0;
	FILE *f;
	char buf[80], *cp, *t;

	f = fopen("/proc/filesystems", "r");
	if (!f)
		return -errno;

	while (!feof(f)) {
		if (!fgets(buf, sizeof(buf), f))
			break;
		cp = buf;
		if (!isspace(*cp)) {
			while (*cp && !isspace(*cp))
				cp++;
		}
		while (*cp && isspace(*cp))
			cp++;
		if ((t = strchr(cp, '\n')) != NULL)
			*t = 0;
		if ((t = strchr(cp, '\t')) != NULL)
			*t = 0;
		if ((t = strchr(cp, ' ')) != NULL)
			*t = 0;

		rc = add_filesystem(vfy, cp);
		if (rc)
			break;
	}
	fclose(f);
	return rc;
}

static void free_proc_filesystems(struct verify_context *vfy)
{
	size_t n;

	if (!vfy->fs_ary)
		return;

	for (n = 0; n < vfy->fs_num; n++ )
		free(vfy->fs_ary[n]);
	free(vfy->fs_ary);
}

static int read_kernel_filesystems(struct verify_context *vfy)
{
	int rc = 0;
#ifdef __linux__
	struct utsname uts;
	FILE *f;
	char buf[1024];

	if (uname(&uts))
		return 0;
	snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);

	f = fopen(buf, "r");
	if (!f)
		return 0;

	while (!feof(f)) {
		char *p, *name;

		if (!fgets(buf, sizeof(buf), f))
			break;

		if (strncmp("kernel/fs/", buf, 10) != 0 ||
		    strncmp("kernel/fs/nls/", buf, 14) == 0)
			continue;

		p = strchr(buf, ':');
		if (!p)
			continue;
		*p = '\0';

		name = strrchr(buf, '/');
		if (!name)
			continue;
		name++;

		p = strstr(name, ".ko");
		if (!p)
			continue;
		*p = '\0';

		rc = add_filesystem(vfy, name);
		if (rc)
			break;
	}
	fclose(f);
#endif /* __linux__ */
	return rc;
}

static int verify_fstype(struct verify_context *vfy)
{
	char *src = mnt_resolve_spec(mnt_fs_get_source(vfy->fs), cache);
	char *realtype = NULL;
	const char *type;
	int ambi = 0, isauto = 0, isswap = 0;

	if (!src)
		return 0;

	if (mnt_fs_is_pseudofs(vfy->fs) || mnt_fs_is_netfs(vfy->fs)) {
		verify_ok(vfy, _("do not check %s FS type (pseudo/net)"), src);
		goto done;
	}

	type = mnt_fs_get_fstype(vfy->fs);

	if (type) {
		int none = strcmp(type, "none") == 0;

		if (none
		    && mnt_fs_get_option(vfy->fs, "bind", NULL, NULL) == 1
		    && mnt_fs_get_option(vfy->fs, "move", NULL, NULL) == 1) {
			verify_warn(vfy, _("\"none\" FS type is recommended for bind or move oprations only"));
			goto done;
		}

		if (strcmp(type, "auto") == 0)
			isauto = 1;
		else if (strcmp(type, "swap") == 0)
			isswap = 1;
		else if (strcmp(type, "xfs") == 0 || strcmp(type, "btrfs") == 0)
			vfy->no_fsck = 1;

		if (!isswap && !isauto && !none && !is_supported_filesystem(vfy, type))
			verify_warn(vfy, _("%s seems unsupported by the current kernel"), type);
	}

	errno = 0;
	realtype = mnt_get_fstype(src, &ambi, cache);

	if (!realtype) {
		const char *reson = errno ? strerror(errno) : _("reason unknown");

		if (isauto)
			verify_err(vfy, _("cannot detect on-disk filesystem type (%s)"), reson);
		else
			verify_warn(vfy, _("cannot detect on-disk filesystem type (%s)"), reson);
		goto done;
	}

	if (realtype) {
		isswap = strcmp(realtype, "swap") == 0;
		vfy->no_fsck = strcmp(realtype, "xfs") == 0
				|| strcmp(realtype, "btrfs") == 0;

		if (type && !isauto && strcmp(type, realtype) != 0) {
			verify_warn(vfy, _("%s does not match with on-disk %s"), type, realtype);
			goto done;
		}
		if (!isswap && !is_supported_filesystem(vfy, realtype)) {
			verify_warn(vfy, _("on-disk %s seems unsupported by the current kernel"), realtype);
			goto done;
		}

		verify_ok(vfy, _("FS type is %s"), realtype);
	}

done:
	if (!cache) {
		free(src);
		free(realtype);
	}
	return 0;
}

static int verify_passno(struct verify_context *vfy)
{
	int passno = mnt_fs_get_passno(vfy->fs);
	const char *tgt = mnt_fs_get_target(vfy->fs);

	if (tgt && strcmp("/", tgt) == 0 && passno != 1 && !vfy->no_fsck)
		return verify_warn(vfy, _("recommended root FS passno is 1 (current is %d)"), passno);

	return 0;
}

static int verify_filesystem(struct verify_context *vfy)
{
	int rc = 0;

	if (mnt_fs_is_swaparea(vfy->fs))
		rc = verify_swaparea(vfy);
	else {
		rc = verify_target(vfy);
		if (!rc)
			rc = verify_options(vfy);
	}

	if (!rc)
		rc = verify_source(vfy);
	if (!rc)
		rc = verify_fstype(vfy);
	if (!rc)
		rc = verify_passno(vfy);	/* depends on verify_fstype() */

	return rc;
}

int verify_table(struct libmnt_table *tb)
{
	struct verify_context vfy = { .nerrors = 0 };
	struct libmnt_iter *itr;
	int rc = 0;		/* overall return code (alloc errors, etc.) */
	int check_order = is_listall_mode();
	static int has_read_fs = 0;

	itr = mnt_new_iter(MNT_ITER_FORWARD);
	if (!itr) {
		warn(_("failed to initialize libmount iterator"));
		goto done;
	}

	vfy.tb = tb;

	if (has_read_fs == 0) {
		read_proc_filesystems(&vfy);
		read_kernel_filesystems(&vfy);
		has_read_fs = 1;
	}

	while (rc == 0 && (vfy.fs = get_next_fs(tb, itr))) {
		vfy.target_printed = 0;
		vfy.no_fsck = 0;

		if (check_order)
			rc = verify_order(&vfy);
		if (!rc)
			rc = verify_filesystem(&vfy);

		if (flags & FL_FIRSTONLY)
			break;
		flags |= FL_NOSWAPMATCH;
	}

#ifdef USE_SYSTEMD
	{
		struct stat a, b;

		if (stat(_PATH_SD_UNITSLOAD, &a) == 0 &&
		    stat(_PATH_MNTTAB, &b) == 0 &&
		    cmp_stat_mtime(&a, &b, <))
			verify_warn(&vfy, _(
	"your fstab has been modified, but systemd still uses the old version;\n"
	"       use 'systemctl daemon-reload' to reload"));
	}
#endif

done:
	mnt_free_iter(itr);

	/* summary */
	if (vfy.nerrors || parse_nerrors || vfy.nwarnings) {
		fputc('\n', stderr);
		fprintf(stderr, P_("%d parse error", "%d parse errors", parse_nerrors), parse_nerrors);
		fprintf(stderr, P_(", %d error",     ", %d errors", vfy.nerrors), vfy.nerrors);
		fprintf(stderr, P_(", %d warning",   ", %d warnings", vfy.nwarnings), vfy.nwarnings);
		fputc('\n', stderr);
	} else
		fprintf(stdout, _("Success, no errors or warnings detected\n"));


	free_proc_filesystems(&vfy);

	return rc != 0 ? rc : vfy.nerrors + parse_nerrors;
}