Created
December 29, 2024 17:32
-
-
Save gdamjan/cc761915b16c3c7a82118ee2dade8343 to your computer and use it in GitHub Desktop.
Revisions
-
gdamjan created this gist
Dec 29, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,80 @@ #define _GNU_SOURCE #include <libmount/libmount.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <errno.h> /* * gcc -o remount-rw remount.c `pkgconf --libs mount` */ int remount_rw(const char *target) { struct libmnt_context *cxt; struct libmnt_table *tb = NULL; struct libmnt_fs *fs; const char *options; char *new_opts = NULL; int rc = -1; // Create new mounting context cxt = mnt_new_context(); if (!cxt) { fprintf(stderr, "Failed to initialize libmount context\n"); return -1; } // Read current mount table if (mnt_context_get_mtab(cxt, &tb)) { fprintf(stderr, "Failed to read mtab\n"); goto cleanup; } // Find the filesystem entry for our target fs = mnt_table_find_target(tb, target, MNT_ITER_BACKWARD); if (!fs) { fprintf(stderr, "Mount point %s not found\n", target); goto cleanup; } // Get current mount options options = mnt_fs_get_options(fs); if (!options) { fprintf(stderr, "Failed to get current mount options\n"); goto cleanup; } // Create new options string with "remount,rw" if (asprintf(&new_opts, "%s%sremount,rw", options, strlen(options) > 0 ? "," : "") == -1) { fprintf(stderr, "Failed to create new options string\n"); goto cleanup; } // Set up remount operation mnt_context_set_target(cxt, target); mnt_context_set_options(cxt, new_opts); mnt_context_set_mflags(cxt, MS_REMOUNT); // Perform the remount rc = mnt_context_mount(cxt); if (rc) { fprintf(stderr, "Remount failed: %s\n", strerror(errno)); rc = -1; } cleanup: free(new_opts); mnt_free_context(cxt); return rc; } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s <mount_point>\n", argv[0]); return 1; } return remount_rw(argv[1]) == 0 ? 0 : 1; }