10f66f451Sopenharmony_ci/* mkswap.c - Format swap device.
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2009 Rob Landley <rob@landley.net>
40f66f451Sopenharmony_ci
50f66f451Sopenharmony_ciUSE_MKSWAP(NEWTOY(mkswap, "<1>1L:", TOYFLAG_SBIN))
60f66f451Sopenharmony_ci
70f66f451Sopenharmony_ciconfig MKSWAP
80f66f451Sopenharmony_ci  bool "mkswap"
90f66f451Sopenharmony_ci  default y
100f66f451Sopenharmony_ci  help
110f66f451Sopenharmony_ci    usage: mkswap [-L LABEL] DEVICE
120f66f451Sopenharmony_ci
130f66f451Sopenharmony_ci    Set up a Linux swap area on a device or file.
140f66f451Sopenharmony_ci*/
150f66f451Sopenharmony_ci
160f66f451Sopenharmony_ci#define FOR_mkswap
170f66f451Sopenharmony_ci#include "toys.h"
180f66f451Sopenharmony_ci
190f66f451Sopenharmony_ciGLOBALS(
200f66f451Sopenharmony_ci  char *L;
210f66f451Sopenharmony_ci)
220f66f451Sopenharmony_ci
230f66f451Sopenharmony_civoid mkswap_main(void)
240f66f451Sopenharmony_ci{
250f66f451Sopenharmony_ci  int fd = xopen(*toys.optargs, O_RDWR), pagesize = sysconf(_SC_PAGE_SIZE);
260f66f451Sopenharmony_ci  off_t len = fdlength(fd);
270f66f451Sopenharmony_ci  unsigned int pages = (len/pagesize)-1, *swap = (unsigned int *)toybuf;
280f66f451Sopenharmony_ci  char *label = (char *)(swap+7), *uuid = (char *)(swap+3);
290f66f451Sopenharmony_ci
300f66f451Sopenharmony_ci  // Write header. Note that older kernel versions checked signature
310f66f451Sopenharmony_ci  // on disk (not in cache) during swapon, so sync after writing.
320f66f451Sopenharmony_ci
330f66f451Sopenharmony_ci  swap[0] = 1;
340f66f451Sopenharmony_ci  swap[1] = pages;
350f66f451Sopenharmony_ci  xlseek(fd, 1024, SEEK_SET);
360f66f451Sopenharmony_ci  create_uuid(uuid);
370f66f451Sopenharmony_ci  if (TT.L) strncpy(label, TT.L, 15);
380f66f451Sopenharmony_ci  xwrite(fd, swap, 129*sizeof(unsigned int));
390f66f451Sopenharmony_ci  xlseek(fd, pagesize-10, SEEK_SET);
400f66f451Sopenharmony_ci  xwrite(fd, "SWAPSPACE2", 10);
410f66f451Sopenharmony_ci  fsync(fd);
420f66f451Sopenharmony_ci
430f66f451Sopenharmony_ci  if (CFG_TOYBOX_FREE) close(fd);
440f66f451Sopenharmony_ci
450f66f451Sopenharmony_ci  if (TT.L) sprintf(toybuf, ", LABEL=%s", label);
460f66f451Sopenharmony_ci  else *toybuf = 0;
470f66f451Sopenharmony_ci  printf("Swapspace size: %luk%s, UUID=%s\n",
480f66f451Sopenharmony_ci    pages*(unsigned long)(pagesize/1024),
490f66f451Sopenharmony_ci    toybuf, show_uuid(uuid));
500f66f451Sopenharmony_ci}
51