logo

bootstrap-initrd

Linux initrd to bootstrap from a small binary seed git clone https://anongit.hacktivis.me/git/bootstrap-initrd.git/

mount-stub.c (1775B)


  1. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  2. // SPDX-License-Identifier: MPL-2.0
  3. #define _GNU_SOURCE
  4. #include <errno.h>
  5. #include <getopt.h> // getopt_long
  6. #include <stdio.h> // fprintf
  7. #include <string.h> // strerror
  8. #include <sys/mount.h> // mount
  9. static void
  10. usage()
  11. {
  12. fprintf(stderr, "\
  13. Usage: mount [-t fstype] device mountpoint\n\
  14. mount [--bind|--rbind|--move] source destination\n\
  15. ");
  16. }
  17. int
  18. main(int argc, char *argv[])
  19. {
  20. char *fstype = NULL;
  21. int mnt_flags = 0;
  22. int opt_bind_move = 0;
  23. struct option longopts[] = {
  24. /* options exclusive to another, so mnt_flags can be set directly */
  25. {"bind", no_argument, &opt_bind_move, MS_BIND},
  26. {"rbind", no_argument, &opt_bind_move, MS_BIND | MS_REC},
  27. {"move", no_argument, &opt_bind_move, MS_MOVE},
  28. };
  29. int c = -1;
  30. while((c = getopt_long(argc, argv, ":t:", longopts, NULL)) != -1)
  31. {
  32. switch(c)
  33. {
  34. case 0:
  35. break;
  36. case 't':
  37. fstype = optarg;
  38. break;
  39. }
  40. }
  41. argc -= optind;
  42. argv += optind;
  43. if(opt_bind_move != 0)
  44. {
  45. if(argc != 2)
  46. {
  47. fprintf(stderr, "mount: Expected 2 arguments, got %d\n", argc);
  48. usage();
  49. return 1;
  50. }
  51. char *src = argv[0];
  52. char *dest = argv[1];
  53. int ret = mount(src, dest, NULL, opt_bind_move, NULL);
  54. if(ret != 0)
  55. {
  56. fprintf(stderr, "mount: Failed mounting %s to %s: %s\n", src, dest, strerror(errno));
  57. return 1;
  58. }
  59. return 0;
  60. }
  61. if(argc != 2)
  62. {
  63. fprintf(stderr, "mount: Expected 2 arguments, got %d\n", argc);
  64. usage();
  65. return 1;
  66. }
  67. char *src = argv[0];
  68. char *dest = argv[1];
  69. int ret = mount(src, dest, fstype, mnt_flags, NULL);
  70. if(ret != 0)
  71. {
  72. fprintf(stderr, "mount: Failed mounting %s to %s: %s\n", src, dest, strerror(errno));
  73. return 1;
  74. }
  75. return 0;
  76. }