]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: add sample to test fs and context relation
authorKarel Zak <kzak@redhat.com>
Thu, 22 Jun 2023 12:00:46 +0000 (14:00 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 22 Jun 2023 12:00:46 +0000 (14:00 +0200)
Reerences: https://github.com/util-linux/util-linux/issues/2326
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/Makemodule.am
libmount/samples/Makemodule.am [new file with mode: 0644]
libmount/samples/overwrite.c [new file with mode: 0644]

index 8546c2f9b14f5dd9e02418bbb4ae00442c35ebbd..02fb521cf6ca705fcc1dcf9d207aff1f4a840dd5 100644 (file)
@@ -2,6 +2,7 @@ if BUILD_LIBMOUNT
 
 include libmount/src/Makemodule.am
 include libmount/python/Makemodule.am
+include libmount/samples/Makemodule.am
 
 if ENABLE_GTK_DOC
 # Docs uses separate Makefiles
diff --git a/libmount/samples/Makemodule.am b/libmount/samples/Makemodule.am
new file mode 100644 (file)
index 0000000..9081ed7
--- /dev/null
@@ -0,0 +1,12 @@
+
+check_PROGRAMS += \
+       sample-mount-overwrite
+
+sample_mount_cflags = $(AM_CFLAGS) $(NO_UNUSED_WARN_CFLAGS) \
+                      -I$(ul_libmount_incdir)
+sample_mount_ldadd = libmount.la $(LDADD)
+
+
+sample_mount_overwrite_SOURCES = libmount/samples/overwrite.c
+sample_mount_overwrite_LDADD =  $(sample_mount_ldadd)
+sample_mount_overwrite_CFLAGS = $(sample_mount_cflags)
diff --git a/libmount/samples/overwrite.c b/libmount/samples/overwrite.c
new file mode 100644 (file)
index 0000000..ff11b71
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2023 Karel Zak <kzak@redhat.com>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ *
+ *
+ * This sample reads the mountpoint entry from /etc/fstab and mounts it to the
+ * different (on the command line specified) mountpoint. The mount options
+ * settings are read from fstab.
+ */
+#include <stdlib.h>
+
+#include "c.h"
+
+#include "libmount.h"
+
+
+int main(int argc, char *argv[])
+{
+       char *target, *fstab_target;
+        struct libmnt_table *tab;
+        struct libmnt_fs *fs;
+        struct libmnt_context *cxt;
+       int rc;
+
+       if (argc != 3)
+               errx(EXIT_FAILURE, "usage: %s <mnt-from-fstab> <target>",
+                               program_invocation_short_name);
+
+       fstab_target = argv[1];
+       target = argv[2];
+
+       printf("Mounting %s from fstab to %s\n", fstab_target, target);
+
+       tab = mnt_new_table_from_file("/etc/fstab");
+       if (!tab)
+               err(EXIT_FAILURE, "failed to parse fstab");
+
+       fs = mnt_table_find_target(tab, fstab_target, MNT_ITER_FORWARD);
+       if (!fs)
+               err(EXIT_FAILURE, "cannot found %s in fstab", argv[1]);
+
+        cxt = mnt_new_context();
+       if (!cxt)
+               err(EXIT_FAILURE, "cannot allocate context");
+
+       mnt_context_set_fs(cxt, fs);
+       mnt_context_set_target(cxt, target);
+
+       rc = mnt_context_mount(cxt);
+
+       printf("Done: rc=%d status=%d\n", rc, mnt_context_get_status(cxt));
+
+       mnt_free_context(cxt);
+       mnt_unref_table(tab);
+       return rc == 0 && mnt_context_get_status(cxt) == 1 ? EXIT_SUCCESS : EXIT_FAILURE;
+}