ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

commit 423e0020982122a7d7d4f5e2e2e62f0ba795de19
parent 92895baad07775a97cf69091d570836afa33a833
Author: sin <sin@2f30.org>
Date:   Fri,  9 Aug 2013 13:45:20 +0100

Add mkswap(8)

No manpage yet.

Diffstat:
MMakefile | 1+
Amkswap.c | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 84 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -20,6 +20,7 @@ ifeq ($(OS),linux) SRC += \ insmod.c \ lsmod.c \ + mkswap.c \ rmmod.c endif diff --git a/mkswap.c b/mkswap.c @@ -0,0 +1,83 @@ +/* See LICENSE file for copyright and license details. */ +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "util.h" + +enum { SWAP_UUID_LENGTH = 16, SWAP_LABEL_LENGTH = 16 }; +enum { SWAP_MIN_PAGES = 10 }; + +struct swap_hdr { + char bootbits[1024]; + unsigned int version; + unsigned int last_page; + unsigned int nr_badpages; + unsigned char uuid[SWAP_UUID_LENGTH]; + char volume_name[SWAP_LABEL_LENGTH]; + unsigned int padding[117]; + unsigned int badpages[1]; +}; + +static void +usage(void) +{ + eprintf("usage: %s <filename|device>\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + int fd; + unsigned int pages; + long pagesize; + struct stat sb; + char *buf; + struct swap_hdr *hdr; + + ARGBEGIN { + default: + usage(); + } ARGEND; + + if (argc < 1) + usage(); + + pagesize = sysconf(_SC_PAGE_SIZE); + + fd = open(argv[0], O_RDWR); + if (fd < 0) + eprintf("open %s:", argv[0]); + if (fstat(fd, &sb) < 0) + eprintf("stat %s:", argv[0]); + + buf = calloc(1, pagesize); + if (!buf) + eprintf("malloc:"); + + pages = sb.st_size / pagesize; + if (pages < SWAP_MIN_PAGES) + enprintf(1, "swap space needs to be at least %ldKiB\n", + SWAP_MIN_PAGES * pagesize / 1024); + + /* Fill up the swap header */ + hdr = (struct swap_hdr *)buf; + hdr->version = 1; + hdr->last_page = pages - 1; + strncpy(buf + pagesize - 10, "SWAPSPACE2", 10); + + printf("Setting up swapspace version 1, size = %luKiB\n", + (pages - 1) * pagesize / 1024); + + /* Write out the signature page */ + if (write(fd, buf, pagesize) != pagesize) + enprintf(1, "unable to write signature page\n"); + + fsync(fd); + close(fd); + free(buf); + + return 0; +}