mkswap.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* vi: set sw=4 ts=4: */
  2. /* mkswap.c - format swap device (Linux v1 only)
  3. *
  4. * Copyright 2006 Rob Landley <rob@landley.net>
  5. *
  6. * Licensed under GPL version 2, see file LICENSE in this tarball for details.
  7. */
  8. #include "libbb.h"
  9. #if ENABLE_SELINUX
  10. static void mkswap_selinux_setcontext(int fd, const char *path)
  11. {
  12. struct stat stbuf;
  13. if (!is_selinux_enabled())
  14. return;
  15. if (fstat(fd, &stbuf) < 0)
  16. bb_perror_msg_and_die("fstat failed");
  17. if (S_ISREG(stbuf.st_mode)) {
  18. security_context_t newcon;
  19. security_context_t oldcon = NULL;
  20. context_t context;
  21. if (fgetfilecon(fd, &oldcon) < 0) {
  22. if (errno != ENODATA)
  23. goto error;
  24. if (matchpathcon(path, stbuf.st_mode, &oldcon) < 0)
  25. goto error;
  26. }
  27. context = context_new(oldcon);
  28. if (!context || context_type_set(context, "swapfile_t"))
  29. goto error;
  30. newcon = context_str(context);
  31. if (!newcon)
  32. goto error;
  33. /* fsetfilecon_raw is hidden */
  34. if (strcmp(oldcon, newcon) != 0 && fsetfilecon(fd, newcon) < 0)
  35. goto error;
  36. if (ENABLE_FEATURE_CLEAN_UP) {
  37. context_free(context);
  38. freecon(oldcon);
  39. }
  40. }
  41. return;
  42. error:
  43. bb_perror_msg_and_die("SELinux relabeling failed");
  44. }
  45. #else
  46. #define mkswap_selinux_setcontext(fd, path) ((void)0)
  47. #endif
  48. #if 0 /* from Linux 2.6.23 */
  49. /*
  50. * Magic header for a swap area. The first part of the union is
  51. * what the swap magic looks like for the old (limited to 128MB)
  52. * swap area format, the second part of the union adds - in the
  53. * old reserved area - some extra information. Note that the first
  54. * kilobyte is reserved for boot loader or disk label stuff...
  55. */
  56. union swap_header {
  57. struct {
  58. char reserved[PAGE_SIZE - 10];
  59. char magic[10]; /* SWAP-SPACE or SWAPSPACE2 */
  60. } magic;
  61. struct {
  62. char bootbits[1024]; /* Space for disklabel etc. */
  63. __u32 version; /* second kbyte, word 0 */
  64. __u32 last_page; /* 1 */
  65. __u32 nr_badpages; /* 2 */
  66. unsigned char sws_uuid[16]; /* 3,4,5,6 */
  67. unsigned char sws_volume[16]; /* 7,8,9,10 */
  68. __u32 padding[117]; /* 11..127 */
  69. __u32 badpages[1]; /* 128, total 129 32-bit words */
  70. } info;
  71. };
  72. #endif
  73. #define NWORDS 129
  74. #define hdr ((uint32_t*)(&bb_common_bufsiz1))
  75. struct BUG_bufsiz1_is_too_small {
  76. char BUG_bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1];
  77. };
  78. /* Stored without terminating NUL */
  79. static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2";
  80. int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  81. int mkswap_main(int argc, char **argv)
  82. {
  83. int fd, pagesize;
  84. off_t len;
  85. // No options supported.
  86. if (argc != 2) bb_show_usage();
  87. // Figure out how big the device is and announce our intentions.
  88. fd = xopen(argv[1], O_RDWR);
  89. /* fdlength was reported to be unreliable - use seek */
  90. len = xlseek(fd, 0, SEEK_END);
  91. #if ENABLE_SELINUX
  92. xlseek(fd, 0, SEEK_SET);
  93. #endif
  94. pagesize = getpagesize();
  95. printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n",
  96. len - pagesize);
  97. mkswap_selinux_setcontext(fd, argv[1]);
  98. // Make a header. hdr is zero-filled so far...
  99. hdr[0] = 1;
  100. hdr[1] = (len / pagesize) - 1;
  101. // Write the header. Sync to disk because some kernel versions check
  102. // signature on disk (not in cache) during swapon.
  103. xlseek(fd, 1024, SEEK_SET);
  104. xwrite(fd, hdr, NWORDS * 4);
  105. xlseek(fd, pagesize - 10, SEEK_SET);
  106. xwrite(fd, SWAPSPACE2, 10);
  107. fsync(fd);
  108. if (ENABLE_FEATURE_CLEAN_UP) close(fd);
  109. return 0;
  110. }