aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

commit 55079f07f91782d0c9687a81c8c3308414ceb4cb
parent b97b7916ff74bb1a757e4d07f9170bc38e945354
Author: xplshn <anto@xplshn.com.ar>
Date:   Fri, 20 Nov 2026 06:32:56 -0300

proper uuid generation in mkswap

Signed-off-by: xplshn <anto@xplshn.com.ar>

Diffstat:
Mcmd/linux/mkswap.c | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 80 insertions(+), 8 deletions(-)

diff --git a/cmd/linux/mkswap.c b/cmd/linux/mkswap.c @@ -1,13 +1,19 @@ /* see LICENSE file for copyright and license details */ +#include <sys/ioctl.h> #include <sys/stat.h> #include <fcntl.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> +#if defined(__linux__) +#include <linux/fs.h> +#endif + #include "util.h" #define SWAP_UUID_LENGTH 16 @@ -28,11 +34,11 @@ struct swap_hdr { static void usage(void) { - eprintf("usage: %s device\n", argv0); + eprintf("usage: %s [-L label] [-U uuid] device\n", argv0); } // ?man mkswap: set up a swap area -// ?man arguments: device +// ?man arguments: [-L label] [-U uuid] device // ?man initialize a linux swap area on a device or file int main(int argc, char *argv[]) @@ -43,9 +49,19 @@ main(int argc, char *argv[]) struct stat sb; char *buf; struct swap_hdr *hdr; + uint64_t bytes = 0; + char *label = NULL; + char *uuid_str = NULL; + char uuid_out[37] = {0}; ARGBEGIN { + case 'L': + label = EARGF(usage()); + break; + case 'U': + uuid_str = EARGF(usage()); + break; default: usage(); } @@ -67,22 +83,78 @@ main(int argc, char *argv[]) if (fstat(fd, &sb) < 0) eprintf("stat %s:", argv[0]); - buf = ecalloc(1, pagesize); + if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) { +#if defined(BLKGETSIZE64) + if (ioctl(fd, BLKGETSIZE64, &bytes) < 0) + bytes = 0; +#endif + if (bytes == 0) { + off_t end = lseek(fd, 0, SEEK_END); + if (end != (off_t)-1) + bytes = (uint64_t)end; + lseek(fd, 0, SEEK_SET); + } + } else if (S_ISREG(sb.st_mode)) { + bytes = (uint64_t)sb.st_size; + } else { + off_t end = lseek(fd, 0, SEEK_END); + if (end != (off_t)-1) + bytes = (uint64_t)end; + lseek(fd, 0, SEEK_SET); + } - pages = sb.st_size / pagesize; + if (bytes == 0) + eprintf("unable to determine size of %s\n", argv[0]); + + pages = bytes / pagesize; if (pages < SWAP_MIN_PAGES) eprintf("swap space needs to be at least %ldKiB\n", SWAP_MIN_PAGES * pagesize / 1024); - /* fill up the swap header */ + buf = ecalloc(1, pagesize); + hdr = (struct swap_hdr *)buf; hdr->version = 1; hdr->last_page = pages - 1; + + if (uuid_str) { + unsigned int u[16]; + if (sscanf(uuid_str, + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", + &u[0], &u[1], &u[2], &u[3], &u[4], &u[5], &u[6], &u[7], + &u[8], &u[9], &u[10], &u[11], &u[12], &u[13], &u[14], &u[15]) == 16) { + int i; + for (i = 0; i < 16; i++) + hdr->uuid[i] = (unsigned char)u[i]; + } + } else { + int rfd = open("/dev/urandom", O_RDONLY); + if (rfd >= 0) { + if (read(rfd, hdr->uuid, SWAP_UUID_LENGTH) == SWAP_UUID_LENGTH) { + hdr->uuid[6] = (hdr->uuid[6] & 0x0f) | 0x40; + hdr->uuid[8] = (hdr->uuid[8] & 0x3f) | 0x80; + } + close(rfd); + } + } + + if (label) + estrlcpy(hdr->volume_name, label, sizeof(hdr->volume_name)); + memcpy(buf + pagesize - 10, "SWAPSPACE2", 10); - printf("Setting up swapspace version 1, size = %luKiB\n", (pages - 1) * pagesize / 1024); + snprintf(uuid_out, sizeof(uuid_out), + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", + hdr->uuid[0], hdr->uuid[1], hdr->uuid[2], hdr->uuid[3], + hdr->uuid[4], hdr->uuid[5], hdr->uuid[6], hdr->uuid[7], + hdr->uuid[8], hdr->uuid[9], hdr->uuid[10], hdr->uuid[11], + hdr->uuid[12], hdr->uuid[13], hdr->uuid[14], hdr->uuid[15]); + + printf("Setting up swapspace version 1, size = %luKiB\n", (unsigned long)(pages - 1) * pagesize / 1024); + if (hdr->volume_name[0]) + printf("LABEL=%s, ", hdr->volume_name); + printf("UUID=%s\n", uuid_out); - /* write out the signature page */ - if (write(fd, buf, pagesize) != pagesize) + if (lseek(fd, 0, SEEK_SET) != 0 || write(fd, buf, pagesize) != pagesize) eprintf("unable to write signature page\n"); fsync(fd);