Debian Codegraph @

libmount/samples/overwrite.c

raw
1 /*
2 * Copyright (C) 2023 Karel Zak <[email protected]>
3 *
4 * This file may be redistributed under the terms of the
5 * GNU Lesser General Public License.
6 *
7 *
8 * This sample reads the mountpoint entry from /etc/fstab and mounts it to the
9 * different (on the command line specified) mountpoint. The mount options
10 * settings are read from fstab.
11 */
12 #include <stdlib.h>
13
14 #include "c.h"
15
16 #include "libmount.h"
17
18
19 int main(int argc, char *argv[])
20 {
21 char *target, *fstab_target;
22 struct libmnt_table *tab;
23 struct libmnt_fs *fs;
24 struct libmnt_context *cxt;
25 int rc;
26
27 if (argc != 3)
28 errx(EXIT_FAILURE, "usage: %s <mnt-from-fstab> <target>",
29 program_invocation_short_name);
30
31 fstab_target = argv[1];
32 target = argv[2];
33
34 printf("Mounting %s from fstab to %s\n", fstab_target, target);
35
36 tab = mnt_new_table_from_file("/etc/fstab");
37 if (!tab)
38 err(EXIT_FAILURE, "failed to parse fstab");
39
40 fs = mnt_table_find_target(tab, fstab_target, MNT_ITER_FORWARD);
41 if (!fs)
42 err(EXIT_FAILURE, "cannot found %s in fstab", argv[1]);
43
44 cxt = mnt_new_context();
45 if (!cxt)
46 err(EXIT_FAILURE, "cannot allocate context");
47
48 mnt_context_set_fs(cxt, fs);
49 mnt_context_set_target(cxt, target);
50
51 rc = mnt_context_mount(cxt);
52
53 printf("Done: rc=%d status=%d\n", rc, mnt_context_get_status(cxt));
54
55 mnt_free_context(cxt);
56 mnt_unref_table(tab);
57 return rc == 0 && mnt_context_get_status(cxt) == 1 ? EXIT_SUCCESS : EXIT_FAILURE;
58 }