mkswap.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 "busybox.h"
  9. int mkswap_main(int argc, char *argv[])
  10. {
  11. int fd, pagesize;
  12. off_t len;
  13. unsigned int hdr[129];
  14. // No options supported.
  15. if (argc != 2) bb_show_usage();
  16. // Figure out how big the device is and announce our intentions.
  17. fd = xopen(argv[1], O_RDWR);
  18. len = fdlength(fd);
  19. pagesize = getpagesize();
  20. printf("Setting up swapspace version 1, size = %"OFF_FMT"d bytes\n",
  21. len - pagesize);
  22. // Make a header.
  23. memset(hdr, 0, sizeof(hdr));
  24. hdr[0] = 1;
  25. hdr[1] = (len / pagesize) - 1;
  26. // Write the header. Sync to disk because some kernel versions check
  27. // signature on disk (not in cache) during swapon.
  28. xlseek(fd, 1024, SEEK_SET);
  29. xwrite(fd, hdr, sizeof(hdr));
  30. xlseek(fd, pagesize-10, SEEK_SET);
  31. xwrite(fd, "SWAPSPACE2", 10);
  32. fsync(fd);
  33. if (ENABLE_FEATURE_CLEAN_UP) close(fd);
  34. return 0;
  35. }