commit 8f3f1ee9fa5f7720f39ba31e94f091c68be2df71
parent 4211f7cccd00b13ed1ea9fddec13b31bd97eb094
Author: cowmonk <cowmonk@based.pt>
Date: Fri, 27 Nov 2026 22:44:50 -0700
mkfs.ext4: added a formatter based on lwext4
partplan checks sizes and calls it, blkid detects ext4, and Linux loop
checks live in diskutil_linux.c.
Use the regular build, install and clean rules, keep TLS off the standalone
formatter, & credited lwext4 in README and LICENSE.
Diffstat:
11 files changed, 570 insertions(+), 27 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -43,6 +43,7 @@ Copyright (c) 2014 Roberto E. Vargas Caballero [k0ga@shike2.com](mailto:k0ga@shi
Copyright (c) 2014 Silvan Jegen [s.jegen@gmail.com](mailto:s.jegen@gmail.com)
Copyright (c) 2014 Tuukka Kataja [stuge@xor.fi](mailto:stuge@xor.fi)
Copyright (c) 2015 Dionysis Grigoropoulos [info@erethon.com](mailto:info@erethon.com)
+Copyright (c) 2015 Grzegorz Kostka [kostka.grzegorz@gmail.com](mailto:kostka.grzegorz@gmail.com)
Copyright (c) 2015 Quentin Rameau [quinq@fifth.space](mailto:quinq@fifth.space)
Copyright (c) 2015 Risto Salminen [ripejcp@gmail.com](mailto:ripejcp@gmail.com)
Copyright (c) 2015 Tai Chi Minh Ralph Eastwood [tcmreastwood@gmail.com](mailto:tcmreastwood@gmail.com)
@@ -66,4 +67,6 @@ SOFTWARE.
Portions of this software (specifically utils under net/) are partially
based on code written by Rob Landley
+mkfs.fat is a port of OpenBSD's newfs_msdos (BSD 2-Clause).
+mkfs.ext4 is based on lwext4's formatter (BSD 3-Clause).
The rest are based on SBASE/UBASE source code
diff --git a/README b/README
@@ -152,8 +152,8 @@ xsi:
standalone and installed with setuid
extra:
- b3sum, blkid, dc, fdisk, kmap, lsblk, mkfs.fat, partplan, pgrep,
- signify, sync, yap
+ b3sum, blkid, dc, fdisk, kmap, lsblk, mkfs.ext4, mkfs.fat, partplan,
+ pgrep, signify, sync, yap
TODOs / PENDING RELEASE
=======================
@@ -211,6 +211,7 @@ Copyright (c) 2014 Roberto E. Vargas Caballero [k0ga@shike2.com](mailto:k0ga@shi
Copyright (c) 2014 Silvan Jegen [s.jegen@gmail.com](mailto:s.jegen@gmail.com)
Copyright (c) 2014 Tuukka Kataja [stuge@xor.fi](mailto:stuge@xor.fi)
Copyright (c) 2015 Dionysis Grigoropoulos [info@erethon.com](mailto:info@erethon.com)
+Copyright (c) 2015 Grzegorz Kostka [kostka.grzegorz@gmail.com](mailto:kostka.grzegorz@gmail.com)
Copyright (c) 2015 Quentin Rameau [quinq@fifth.space](mailto:quinq@fifth.space)
Copyright (c) 2015 Risto Salminen [ripejcp@gmail.com](mailto:ripejcp@gmail.com)
Copyright (c) 2015 Tai Chi Minh Ralph Eastwood [tcmreastwood@gmail.com](mailto:tcmreastwood@gmail.com)
@@ -221,6 +222,7 @@ Copyright (c) 2026 xplshn [anto@xplshn.com.ar](mailto:anto@xplshn.com.ar) (https
Portions of this software (specifically utils under net/) are partially based on code written by Rob Landley
Our line editing library is partially based on work by Salvatore Sanfilippo (linenoise) and JC Wang (crossline)
+mkfs.ext4 is based on lwext4's formatter (BSD 3-Clause).
mkfs.fat is a port of OpenBSD's newfs_msdos (BSD 2-Clause).
The rest are based on SBASE/UBASE source code
diff --git a/cmd/extra/mkfs.ext4.c b/cmd/extra/mkfs.ext4.c
@@ -0,0 +1,464 @@
+/*
+ * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/* Adapted from lwext4 src/ext4_mkfs.c, b2d8fe025940cf4091fc40334aedd64d56034cdc.
+ * On-disk fields below use byte offsets from the ext4 and JBD2 specifications. */
+#include "diskutil.h"
+#include "util.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
+
+enum {
+ BLOCK = MKFS_EXT4_BLOCK_SIZE, INODE = 256, DESC = 32, JOURNAL = 1024,
+ GROUP_BLOCKS = BLOCK * 8, ROOT = 2, LOG_INO = 8, LOST = 11
+};
+
+struct Volume {
+ const char *path;
+ int fd;
+ uint32_t blocks, groups, gdt_blocks, inodes_per_group;
+ uint32_t table_blocks, now;
+};
+
+static void
+usage(void)
+{
+ eprintf("usage: %s [-L label] [-U uuid] device|image\n", argv0);
+}
+
+static void
+le16(unsigned char *p, uint16_t v)
+{
+ p[0] = v;
+ p[1] = v >> 8;
+}
+
+static void
+le32(unsigned char *p, uint32_t v)
+{
+ le16(p, v);
+ le16(p + 2, v >> 16);
+}
+
+static void
+be32(unsigned char *p, uint32_t v)
+{
+ p[0] = v >> 24;
+ p[1] = v >> 16;
+ p[2] = v >> 8;
+ p[3] = v;
+}
+
+static void
+volume_uuid(unsigned char uuid[16], const char *s)
+{
+ int fd, hi, lo;
+ size_t i = 0;
+ ssize_t n;
+
+ if (s) {
+ if (strlen(s) != 36)
+ usage();
+ for (; i < 16; i++) {
+ if ((i == 4 || i == 6 || i == 8 || i == 10) && *s++ != '-')
+ usage();
+ hi = hexval(*s++);
+ lo = hexval(*s++);
+ if (hi < 0 || lo < 0)
+ usage();
+ uuid[i] = (hi << 4) | lo;
+ }
+ if (!memcmp(uuid, (unsigned char[16]){0}, 16))
+ eprintf("UUID must be nonzero\n");
+ return;
+ }
+ if ((fd = open(ARUU_PATH_DEVURANDOM, O_RDONLY)) < 0)
+ eprintf("%s:", ARUU_PATH_DEVURANDOM);
+ while (i < 16) {
+ n = read(fd, uuid + i, 16 - i);
+ if (n < 0 && errno == EINTR)
+ continue;
+ if (n <= 0) {
+ if (!n)
+ errno = EIO;
+ eprintf("%s:", ARUU_PATH_DEVURANDOM);
+ }
+ i += n;
+ }
+ if (close(fd) < 0)
+ eprintf("%s:", ARUU_PATH_DEVURANDOM);
+ uuid[6] = (uuid[6] & 15) | 0x40;
+ uuid[8] = (uuid[8] & 63) | 0x80;
+}
+
+/* With sparse_super, groups 0, 1 and powers of 3, 5 and 7 have backups. */
+static int
+has_super(uint32_t group)
+{
+ const unsigned bases[] = {3, 5, 7};
+ size_t i;
+
+ if (group <= 1)
+ return 1;
+ for (i = 0; i < LEN(bases); i++) {
+ uint32_t n = group;
+
+ while (n % bases[i] == 0)
+ n /= bases[i];
+ if (n == 1)
+ return 1;
+ }
+ return 0;
+}
+
+static void
+open_volume(struct Volume *v)
+{
+ struct stat st;
+ struct flock lock = {.l_type = F_WRLCK, .l_whence = SEEK_SET};
+ int flags = O_RDWR | O_NONBLOCK | O_NOCTTY;
+ uint64_t bytes;
+ time_t now;
+
+#if defined(__linux__) && !defined(ARUU_FORCE_POSIX)
+ flags |= O_EXCL; /* kernel claim on a block device */
+#endif
+ if ((v->fd = open(v->path, flags)) < 0 || fstat(v->fd, &st) < 0)
+ eprintf("%s:", v->path);
+ if (S_ISREG(st.st_mode)) {
+ if (fcntl(v->fd, F_SETLK, &lock) < 0 || fstat(v->fd, &st) < 0)
+ eprintf("%s:", v->path);
+#if defined(__linux__) && !defined(ARUU_FORCE_POSIX)
+ blockdev_check_loop(&st, v->path);
+#endif
+ bytes = st.st_size;
+#if defined(__linux__) && !defined(ARUU_FORCE_POSIX)
+ } else if (S_ISBLK(st.st_mode)) {
+ struct BlockDev dev = {.fd = v->fd};
+
+ blockdev_sys_size(&dev);
+ if (!dev.sec_size || BLOCK % dev.sec_size)
+ eprintf("%s: incompatible sector size\n", v->path);
+ if (dev.size % dev.sec_size)
+ eprintf("%s: incomplete device sector\n", v->path);
+ /* A partition can end between filesystem blocks. Leave its tail alone. */
+ bytes = dev.size - dev.size % BLOCK;
+#endif
+ } else {
+ eprintf("%s: unsupported target type\n", v->path);
+ }
+ if (bytes % BLOCK || !mkfs_ext4_size_ok(bytes) ||
+ (off_t)bytes <= 0 || (uint64_t)(off_t)bytes != bytes)
+ eprintf("%s: expected a 4 KiB aligned size from 8 MiB to below 16 TiB\n", v->path);
+
+ v->blocks = bytes / BLOCK;
+ v->groups = 1 + (v->blocks - 1) / GROUP_BLOCKS;
+ v->gdt_blocks = 1 + (v->groups - 1) / (BLOCK / DESC);
+ v->inodes_per_group = (v->blocks / 4 + v->groups - 1) / v->groups;
+ v->table_blocks = (v->inodes_per_group + BLOCK / INODE - 1) / (BLOCK / INODE);
+ v->inodes_per_group = v->table_blocks * (BLOCK / INODE);
+ /* As in lwext4, omit a partial last group too small for its own metadata. */
+ if (v->groups > 1 && v->blocks % GROUP_BLOCKS &&
+ v->blocks % GROUP_BLOCKS < 2 + v->table_blocks +
+ (has_super(v->groups - 1) ? 1 + v->gdt_blocks : 0)) {
+ v->blocks -= v->blocks % GROUP_BLOCKS;
+ v->groups--;
+ v->gdt_blocks = 1 + (v->groups - 1) / (BLOCK / DESC);
+ }
+ now = time(NULL);
+ if (now == (time_t)-1 || (uintmax_t)now > UINT32_MAX)
+ eprintf("filesystem timestamp out of range\n");
+ v->now = (uint32_t)now;
+}
+
+static void
+write_blocks(const struct Volume *v, uint32_t block, const unsigned char *buf, unsigned count)
+{
+ off_t off = (off_t)((uint64_t)block * BLOCK);
+ size_t done = 0, len = (size_t)count * BLOCK;
+ ssize_t n;
+
+ if (block >= v->blocks || count > v->blocks - block)
+ eprintf("%s: block outside volume\n", v->path);
+ while (done < len) {
+ n = pwrite(v->fd, buf + done, len - done, off + (off_t)done);
+ if (n < 0 && errno == EINTR)
+ continue;
+ if (n <= 0) {
+ if (!n)
+ errno = EIO;
+ eprintf("%s: writing block %u:", v->path, (unsigned)block);
+ }
+ done += n;
+ }
+}
+
+static void
+write_block(const struct Volume *v, uint32_t block, const unsigned char *buf)
+{
+ write_blocks(v, block, buf, 1);
+}
+
+static void
+sync_volume(const struct Volume *v)
+{
+ while (fsync(v->fd) < 0)
+ if (errno != EINTR)
+ eprintf("%s: fsync:", v->path);
+}
+
+/* A used prefix, a free interval, then padding through the end of the bitmap. */
+static void
+bitmap(const struct Volume *v, uint32_t block, uint32_t used, uint32_t count)
+{
+ unsigned char map[BLOCK];
+
+ memset(map, 0xff, sizeof map);
+ for (; used < count; used++)
+ map[used / 8] &= ~(1U << (used % 8));
+ write_block(v, block, map);
+}
+
+static void
+inode(unsigned char *p, uint32_t now, unsigned mode, unsigned links,
+ uint32_t bytes, uint32_t blocks)
+{
+ le16(p, mode);
+ le32(p + 4, bytes);
+ le32(p + 8, now); /* atime, ctime, mtime */
+ le32(p + 12, now);
+ le32(p + 16, now);
+ le16(p + 26, links);
+ le32(p + 28, blocks * (BLOCK / 512));
+ le16(p + 128, 32); /* i_extra_isize */
+ le32(p + 132, now >> 31);
+ le32(p + 136, now >> 31);
+ le32(p + 140, now >> 31);
+ le32(p + 144, now); /* crtime */
+ le32(p + 148, now >> 31);
+}
+
+static void
+dirent(unsigned char *p, uint32_t ino, unsigned len, const char *name)
+{
+ le32(p, ino);
+ le16(p + 4, len);
+ p[6] = strlen(name);
+ p[7] = 2; /* EXT4_FT_DIR */
+ memcpy(p + 8, name, p[6]);
+}
+
+/* The first table block holds all reserved inodes and lost+found. The two
+ * directory blocks, journal indirect block and journal data follow the table. */
+static void
+write_files(const struct Volume *v, uint32_t root, unsigned char *sb)
+{
+ unsigned char table[BLOCK] = {0}, buf[BLOCK] = {0};
+ unsigned char *log = table + (LOG_INO - 1) * INODE;
+ uint32_t first = root + 3, i;
+
+ inode(table + (ROOT - 1) * INODE, v->now, 0040755, 3, BLOCK, 1);
+ le32(table + (ROOT - 1) * INODE + 40, root);
+ inode(table + (LOST - 1) * INODE, v->now, 0040700, 2, BLOCK, 1);
+ le32(table + (LOST - 1) * INODE + 40, root + 1);
+ inode(log, v->now, 0100600, 1, JOURNAL * BLOCK, JOURNAL + 1);
+ for (i = 0; i < 12; i++)
+ le32(log + 40 + 4 * i, first + i);
+ le32(log + 40 + 12 * 4, root + 2);
+ memcpy(sb + 268, log + 40, 60); /* journal i_block backup */
+ le32(sb + 332, JOURNAL * BLOCK);
+ write_block(v, root - v->table_blocks, table);
+
+ dirent(buf, ROOT, 12, ".");
+ dirent(buf + 12, ROOT, 12, "..");
+ dirent(buf + 24, LOST, BLOCK - 24, "lost+found");
+ write_block(v, root, buf);
+ memset(buf, 0, BLOCK);
+ dirent(buf, LOST, 12, ".");
+ dirent(buf + 12, ROOT, BLOCK - 12, "..");
+ write_block(v, root + 1, buf);
+
+ memset(buf, 0, BLOCK);
+ for (i = 12; i < JOURNAL; i++)
+ le32(buf + (i - 12) * 4, first + i);
+ write_block(v, root + 2, buf);
+
+ /* lwext4 create_journal_inode: JBD2 superblock v2, empty log. */
+ memset(buf, 0, BLOCK);
+ be32(buf, 0xc03b3998);
+ be32(buf + 4, 4);
+ be32(buf + 12, BLOCK);
+ be32(buf + 16, JOURNAL);
+ be32(buf + 20, 1); /* first log block */
+ be32(buf + 24, 1); /* next sequence */
+ be32(buf + 40, 1); /* revoke */
+ memcpy(buf + 48, sb + 104, 16);
+ be32(buf + 64, 1); /* internal journal, one user */
+ write_block(v, first, buf);
+ memset(buf, 0, BLOCK);
+ for (i = 1; i < JOURNAL; i++)
+ write_block(v, first + i, buf);
+}
+
+static void
+format(const struct Volume *v, const char *label, const unsigned char uuid[16])
+{
+ static const unsigned char zero[64 * 1024];
+ unsigned char sb[1024] = {0}, first[BLOCK], buf[BLOCK] = {0};
+ unsigned char *gdt = ecalloc(v->gdt_blocks, BLOCK);
+ uint32_t g, i, free_blocks = 0;
+ size_t done = 0;
+ ssize_t n;
+
+ /* Read the whole block for devices with 4 KiB logical sectors. Only the
+ * superblock at offset 1024 is invalidated and later published. */
+ while (done < sizeof first) {
+ n = pread(v->fd, first + done, sizeof first - done, done);
+ if (n < 0 && errno == EINTR)
+ continue;
+ if (n < 0)
+ eprintf("%s: reading boot area:", v->path);
+ if (!n)
+ eprintf("%s: short read of boot area\n", v->path);
+ done += n;
+ }
+ memset(first + 1024, 0, sizeof sb);
+ write_block(v, 0, first);
+ sync_volume(v);
+ for (g = 0; g < v->groups; g++) {
+ uint32_t start = g * GROUP_BLOCKS;
+ uint32_t count = MIN(v->blocks - start, GROUP_BLOCKS);
+ uint32_t bitmap_block = start + (has_super(g) ? 1 + v->gdt_blocks : 0);
+ uint32_t table = bitmap_block + 2;
+ uint32_t used = table - start + v->table_blocks + (g == 0 ? JOURNAL + 3 : 0);
+ uint32_t inodes = g == 0 ? LOST : 0;
+ unsigned char *d = gdt + g * DESC;
+
+ le32(d, bitmap_block);
+ le32(d + 4, bitmap_block + 1);
+ le32(d + 8, table);
+ le16(d + 12, count - used);
+ le16(d + 14, v->inodes_per_group - inodes);
+ le16(d + 16, g == 0 ? 2 : 0);
+ free_blocks += count - used;
+ bitmap(v, bitmap_block, used, count);
+ bitmap(v, bitmap_block + 1, inodes, v->inodes_per_group);
+ for (i = (g == 0); i < v->table_blocks;) {
+ unsigned chunk = MIN(v->table_blocks - i, sizeof zero / BLOCK);
+ write_blocks(v, table + i, zero, chunk);
+ i += chunk;
+ }
+ }
+
+ /* lwext4 fill_sb: dynamic revision, Linux inode layout, remount-ro on error. */
+ le32(sb, v->inodes_per_group * v->groups);
+ le32(sb + 4, v->blocks);
+ le32(sb + 12, free_blocks);
+ le32(sb + 16, v->inodes_per_group * v->groups - LOST);
+ le32(sb + 24, 2); /* block and cluster size, log2(bytes / 1024) */
+ le32(sb + 28, 2);
+ le32(sb + 32, GROUP_BLOCKS);
+ le32(sb + 36, GROUP_BLOCKS);
+ le32(sb + 40, v->inodes_per_group);
+ le32(sb + 48, v->now); /* last write */
+ le16(sb + 54, 0xffff); /* no mount-count limit */
+ le16(sb + 56, 0xef53);
+ le16(sb + 58, 1); /* clean */
+ le16(sb + 60, 2);
+ le32(sb + 64, v->now); /* last check */
+ le32(sb + 76, 1);
+ le32(sb + 84, LOST); /* first non-reserved inode */
+ le16(sb + 88, INODE);
+ le32(sb + 92, 4); /* has_journal */
+ le32(sb + 96, 2); /* filetype */
+ le32(sb + 100, 0x43); /* sparse_super | large_file | extra_isize */
+ memcpy(sb + 104, uuid, 16);
+ memcpy(sb + 120, label, strlen(label));
+ le32(sb + 224, LOG_INO);
+ sb[253] = 1; /* journal backup type */
+ le32(sb + 264, v->now); /* creation time */
+ le16(sb + 348, 32); /* required and desired extra inode fields */
+ le16(sb + 350, 32);
+ write_files(v, 3 + v->gdt_blocks + v->table_blocks, sb);
+
+ for (g = 0; g < v->groups; g++) {
+ if (!has_super(g))
+ continue;
+ write_blocks(v, g * GROUP_BLOCKS + 1, gdt, v->gdt_blocks);
+ if (g) {
+ le16(sb + 90, g);
+ memcpy(buf, sb, sizeof sb);
+ write_block(v, g * GROUP_BLOCKS, buf);
+ }
+ }
+ sync_volume(v);
+ le16(sb + 90, 0);
+ memcpy(first + 1024, sb, sizeof sb);
+ write_block(v, 0, first);
+ sync_volume(v);
+ free(gdt);
+}
+
+// ?man mkfs.ext4: create a MICE journaled filesystem for the Linux ext4 driver
+// ?man arguments: [-L label] [-U uuid] device|image
+// ?man Formats an existing image or Linux block device, 8 MiB to below 16 TiB with 4 KiB blocks.
+int
+main(int argc, char **argv)
+{
+ struct Volume v = {0};
+ const char *label = "", *id = NULL;
+ unsigned char uuid[16];
+
+ ARGBEGIN {
+ case 'L':
+ label = EARGF(usage());
+ break;
+ case 'U':
+ id = EARGF(usage());
+ break;
+ default:
+ usage();
+ } ARGEND
+ if (argc != 1)
+ usage();
+ if (strlen(label) > 16)
+ eprintf("volume label exceeds 16 bytes\n");
+ volume_uuid(uuid, id);
+ v.path = argv[0];
+ open_volume(&v);
+ format(&v, label, uuid);
+ if (close(v.fd) < 0)
+ eprintf("%s:", v.path);
+ return 0;
+}
diff --git a/cmd/extra/partplan.c b/cmd/extra/partplan.c
@@ -594,6 +594,11 @@ main(int argc, char *argv[])
usage();
p = ecalloc(1, sizeof *p);
load(p, argv[2]);
+ if (!strcmp(argv[1], "write") || !strcmp(argv[1], "mkfs")) {
+ char why[200];
+ if (plan_check(p, 0, why, sizeof why) < 0)
+ eprintf("%s\n", why);
+ }
if (strcmp(argv[1], "show") == 0)
rc = do_show(p);
else if (strcmp(argv[1], "gaps") == 0)
diff --git a/ninqu.rules b/ninqu.rules
@@ -518,7 +518,7 @@
find shared -name '*.a' -exec rm -f {} +
printf ' CLEAN compiled binaries\n'
- find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' ')' -perm -100 -exec rm -f {} +
+ find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' ')' -perm -100 -exec rm -f {} +
sh scripts/genconfig.sh clean
rm -f scripts/mk/config.mk scripts/mk/config.kv scripts/mk/rules.mk shared/libutil/nofork_list.h
@@ -556,7 +556,7 @@
m=\"$DESTDIR$(MANPREFIX)\"
mkdir -p \"$d/bin\" \"$m/man1\" \"$m/man8\" \"$d/lib\" \"$d/include/aruu\"
- find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' ')' -perm -100 | while read -r bin; do
+ find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' ')' -perm -100 | while read -r bin; do
name=${bin##*/}
printf ' INSTALL %s/bin/%s\n' '$(PREFIX)' \"$name\"
cp \"$bin\" \"$d/bin/$name\"
@@ -607,7 +607,7 @@
m=\"$DESTDIR$(MANPREFIX)\"
mkdir -p \"$d/bin\" \"$m/man1\" \"$m/man8\" \"$d/lib\" \"$d/include/aruu\"
- find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' ')' -perm -100 ! -path 'cmd/dev/*' | while read -r bin; do
+ find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' ')' -perm -100 ! -path 'cmd/dev/*' | while read -r bin; do
name=${bin##*/}
printf ' INSTALL %s/bin/%s\n' '$(PREFIX)' \"$name\"
cp \"$bin\" \"$d/bin/$name\"
@@ -651,7 +651,7 @@
case \"$DESTDIR\" in /usr|/usr/*) printf 'ARUU cannot remove from usr\n' >&2; exit 1 ;; esac
d=\"$DESTDIR$(PREFIX)\"
m=\"$DESTDIR$(MANPREFIX)\"
- find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' ')' -perm -100 | while read -r bin; do
+ find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' ')' -perm -100 | while read -r bin; do
name=${bin##*/}
printf ' REMOVE %s/bin/%s\n' '$(PREFIX)' \"$name\"
rm -f \"$d/bin/$name\"
@@ -883,7 +883,7 @@
(dep BOX_DISPATCH BOX_OBJ POSIX_BC_BOX POSIX_AWK_BOX POSIX_SH_BOX POSIX_MAKE_BOX EXTRA_YAP_BOX NINQU_BOX LIBDISK LIBUTIL LIBUTF LIBREDLINE LIBBLAKE3 LIBMAGIC)
(out "aruu-box")
(description "single multi-call binary, one dispatch table for every enabled tool")
- (shell "$(CC) $(CPPFLAGS) $(CFLAGS) -o $(OUT) $(IN) $(LDFLAGS) $(LDLIBS) $(BOX_EXTRA_LIBS)")
+ (shell "$(CC) $(CPPFLAGS) $(CFLAGS) -o $(OUT) $(IN) $(LDFLAGS) $(LDLIBS) $(LDLIBS_TLS) $(BOX_EXTRA_LIBS)")
(group box))
(group box BOX SETUID_BIN)
diff --git a/scripts/genconfig.sh b/scripts/genconfig.sh
@@ -203,7 +203,7 @@ config_resolve() {
CPPFLAGS_TLS=${CPPFLAGS_TLS:-}
NINJA_TRY=${NINJA_TRY:-samu,ninja}
LDFLAGS="${LDFLAGS:-} ${LDFLAGS_TLS}"
- LDLIBS="${LDLIBS:-} ${LDLIBS_TLS}"
+ LDLIBS="${LDLIBS:-}"
for g in BUILD_POSIX BUILD_XSI BUILD_NET BUILD_PSEUDO BUILD_EXTRA BUILD_LINUX BUILD_DEV; do
eval "${g}=\${${g}:-0}"
diff --git a/shared/diskutil.h b/shared/diskutil.h
@@ -6,6 +6,15 @@
#include <stddef.h>
#include <stdint.h>
+#include <sys/stat.h>
+
+/* The formatter uses 4 KiB blocks and 32-bit on-disk block counts. */
+#define MKFS_EXT4_BLOCK_SIZE 4096
+static inline int
+mkfs_ext4_size_ok(uint64_t bytes)
+{
+ return bytes >= (8U << 20) && bytes / MKFS_EXT4_BLOCK_SIZE <= UINT32_MAX;
+}
struct BlockDev {
int fd;
@@ -50,6 +59,9 @@ void blockdev_free_list(struct BlockDevInfo *list);
/* a backend per kernel: the size of a device, rereading a table written
* to it, and the list of disks. blockdev_probe_list opens the names most
* systems give disks, for a kernel that has no way to list them */
+#if defined(__linux__) && !defined(ARUU_FORCE_POSIX)
+void blockdev_check_loop(const struct stat *, const char *);
+#endif
void blockdev_sys_size(struct BlockDev *dev);
void blockdev_sys_reread(struct BlockDev *dev);
struct BlockDevInfo *blockdev_probe_list(void);
diff --git a/shared/libdisk/plan.c b/shared/libdisk/plan.c
@@ -167,6 +167,13 @@ plan_check(const struct plan *p, unsigned need, char *why, size_t n)
if (!l->used)
continue;
+ if (a->format && strcmp(a->fs, "ext4") == 0) {
+ uint64_t sectors = l->end - l->start + 1;
+ if (!p->label.sec_size || MKFS_EXT4_BLOCK_SIZE % p->label.sec_size ||
+ sectors > UINT64_MAX / p->label.sec_size ||
+ !mkfs_ext4_size_ok(sectors * p->label.sec_size))
+ return fail(why, n, "%s", "ext4 needs 8 MiB to below 16 TiB and sectors dividing 4 KiB");
+ }
if (a->mount[0] && a->mount[0] != '/')
return fail(why, n, "%s is not a place to mount: it starts with a /", a->mount);
if (a->mount[0] && !a->fs[0])
@@ -294,18 +301,8 @@ plan_mkfs_argv(const struct plan *p, int slot, char **argv, int max, char *store
return -1;
argv[0] = NULL;
if (strcmp(a->fs, "ext4") == 0) {
- char size[32];
-
- /* MICE's ext4 maker, which is told the size of what it makes */
- snprintf(size, sizeof size, "%llu",
- (unsigned long long)((p->label.part[slot].end - p->label.part[slot].start + 1) * p->label.sec_size));
- rc |= put(argv, &n, max, store, store_n, &used, "make_ext4fs");
- rc |= put(argv, &n, max, store, store_n, &used, "-l");
- rc |= put(argv, &n, max, store, store_n, &used, size);
- /* its own UUID: the one make_ext4fs derives from the label is the same
- * for two filesystems of one label, and fstab names them by it */
- rc |= put(argv, &n, max, store, store_n, &used, "-U");
- rc |= put(argv, &n, max, store, store_n, &used, "random");
+ /* mkfs.ext4 generates a fresh UUID, even for filesystems with the same label. */
+ rc |= put(argv, &n, max, store, store_n, &used, "mkfs.ext4");
if (a->label[0]) {
rc |= put(argv, &n, max, store, store_n, &used, "-L");
rc |= put(argv, &n, max, store, store_n, &used, a->label);
@@ -451,6 +448,7 @@ plan_guided(struct plan *p, const char *disk, uint64_t swap_mib, const char *roo
{
struct BlockDev dev;
uint64_t mib;
+ char why[160];
int rc = 0;
memset(p, 0, sizeof *p);
@@ -469,7 +467,9 @@ plan_guided(struct plan *p, const char *disk, uint64_t swap_mib, const char *roo
if (swap_mib)
rc |= plan_add(p, 0, swap_mib * mib, "8200", "SWAP", "swap", "", "") < 0;
rc |= plan_add(p, 0, 0, "8300", "ROOT", root_fs, "/", "root") < 0;
- return rc ? -1 : 0;
+ if (rc)
+ return -1;
+ return plan_check(p, 0, why, sizeof why);
}
int
diff --git a/shared/libdisk/plan_test.c b/shared/libdisk/plan_test.c
@@ -12,7 +12,7 @@
#include <unistd.h>
static int nlines; static void line(const char *l, void *a){ (void)a; nlines++; puts(l); }
int main(void){
- char path[]="/tmp/plan_test.XXXXXX"; int fd=mkstemp(path); assert(fd>=0 && ftruncate(fd,(off_t)1024<<20)==0); close(fd);
+ char path[]="/tmp/plan_test.XXXXXX"; int fd=mkstemp(path); assert(fd>=0 && ftruncate(fd,(off_t)1<<40)==0); close(fd);
struct plan before, p; char why[200]; char *argv[16]; char store[256];
assert(plan_read_disk(&before,path)==0 && before.label.kind==LABEL_NONE);
p=before;
@@ -22,9 +22,14 @@ int main(void){
assert(plan_add(&p,0,64*2048,"ef00","ESP","vfat","/boot/efi","ESP")==1);
assert(plan_add(&p,0,0,"8300","ROOT","ext4","/","")==2);
assert(plan_check(&p,PLAN_NEED_ROOT|PLAN_NEED_BOOT,why,sizeof why)==0);
+ { uint64_t end = p.label.part[2].end;
+ p.label.part[2].end = p.label.part[2].start + 2047;
+ assert(plan_check(&p,0,why,sizeof why)<0);
+ p.label.part[2].end = end; }
assert(plan_changes(&before,&p,line,NULL)>=3);
- assert(plan_mkfs_argv(&p,2,argv,16,store,sizeof store)==0 && !strcmp(argv[0],"make_ext4fs") && !strcmp(argv[1],"-l"));
- assert(argv[2] && argv[3] && argv[4] == NULL);
+ char target[PLAN_PATH + 8];
+ plan_part_path(p.disk, 2, target, sizeof target);
+ assert(plan_mkfs_argv(&p,2,argv,16,store,sizeof store)==0 && !strcmp(argv[0],"mkfs.ext4") && !strcmp(argv[1],target) && !argv[2]);
assert(plan_write_table(&p)==0);
FILE *f=tmpfile(); assert(plan_save(&p,f)==0); rewind(f);
struct plan q; assert(plan_load(&q,f,why,sizeof why)==0);
diff --git a/shared/libutil/diskutil.c b/shared/libutil/diskutil.c
@@ -486,9 +486,11 @@ blockdev_detect_fs(
if (val == fs->magic1 || (fs->magic2 && val == fs->magic2)) {
const char *type_name = fs->name;
if (strcmp(fs->name, "ext") == 0) {
- /* ext4 has a journal too: its own features (extents, 64bit,
- * flex_bg) are looked for before the journal that makes ext3 */
- if ((buf[1120] & 0xC0) || (buf[1121] & 0x02))
+ /* Extents, 64-bit descriptors, flex_bg and extra inode fields
+ * identify ext4 even when the filesystem also has a journal. */
+ if (read_size < 1128)
+ continue;
+ if ((buf[1120] & 0xc0) || (buf[1121] & 0x02) || (buf[1124] & 0x40))
type_name = "ext4";
else if (buf[1116] & 4)
type_name = "ext3";
diff --git a/shared/libutil/diskutil_linux.c b/shared/libutil/diskutil_linux.c
@@ -6,6 +6,7 @@
#include "../util.h"
#if defined(__linux__) && !defined(ARUU_FORCE_POSIX)
+#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
@@ -35,6 +36,55 @@ blockdev_sys_size(struct BlockDev *dev)
#pragma GCC diagnostic pop
}
+/* sysfs exposes the backing path of each attached loop, including unmounted ones. */
+void
+blockdev_check_loop(const struct stat *target, const char *name)
+{
+ struct dirent *de;
+ struct stat st;
+ char entry[PATH_MAX], *path = NULL;
+ size_t cap = 0;
+ ssize_t n;
+ DIR *dir;
+ FILE *f;
+
+ dir = opendir(ARUU_LINUX_PATH_SYS_BLOCK);
+ if (!dir) {
+ if (errno == ENOENT)
+ return;
+ eprintf("%s:", ARUU_LINUX_PATH_SYS_BLOCK);
+ }
+ for (;;) {
+ errno = 0;
+ if (!(de = readdir(dir))) {
+ if (errno)
+ eprintf("%s:", ARUU_LINUX_PATH_SYS_BLOCK);
+ break;
+ }
+ if (strncmp(de->d_name, "loop", 4))
+ continue;
+ if (snprintf(entry, sizeof entry, "%s/%s/loop/backing_file",
+ ARUU_LINUX_PATH_SYS_BLOCK, de->d_name) >= (int)sizeof entry)
+ eprintf("%s: loop path too long\n", name);
+ if (!(f = fopen(entry, "r"))) {
+ if (errno == ENOENT)
+ continue;
+ eprintf("%s:", entry);
+ }
+ n = getdelim(&path, &cap, '\0', f);
+ if (n < 0 && !feof(f))
+ eprintf("%s:", entry);
+ fclose(f);
+ if (n > 0 && path[n - 1] == '\n')
+ path[n - 1] = '\0';
+ if (n > 0 && stat(path, &st) == 0 && st.st_dev == target->st_dev &&
+ st.st_ino == target->st_ino)
+ eprintf("%s: attached to a loop device\n", name);
+ }
+ free(path);
+ closedir(dir);
+}
+
void
blockdev_sys_reread(struct BlockDev *dev)
{