]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mkfs-util: add support for making vfat partitions
authorLennart Poettering <lennart@poettering.net>
Thu, 30 Jul 2020 20:29:48 +0000 (22:29 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 24 Aug 2020 20:00:17 +0000 (22:00 +0200)
fat is a bit more limited in volume name length and UUID support. Let's
add some special support for it.

This is particularly useful to generate EFI system partitions.

src/shared/mkfs-util.c

index a78f68e398a6ee10813ef90261ad47845cc4c659..583ef90e65042b775ac34080e8da6ee290145fc5 100644 (file)
@@ -4,6 +4,7 @@
 #include "mkfs-util.h"
 #include "path-util.h"
 #include "process-util.h"
+#include "stdio-util.h"
 #include "string-util.h"
 
 int mkfs_exists(const char *fstype) {
@@ -94,6 +95,26 @@ int make_filesystem(
                         else
                                 (void) execlp(mkfs, mkfs, "-L", label, "-m", j, "-m", "reflink=1", "-K", node, NULL);
 
+                } else if (streq(fstype, "vfat")) {
+                        char mangled_label[8 + 3 + 1], vol_id[8 + 1];
+
+                        /* Classic FAT only allows 11 character uppercase labels */
+                        strncpy(mangled_label, label, sizeof(mangled_label)-1);
+                        mangled_label[sizeof(mangled_label)-1] = 0;
+                        ascii_strupper(mangled_label);
+
+                        xsprintf(vol_id, "%08" PRIx32,
+                                 ((uint32_t) uuid.bytes[0] << 24) |
+                                 ((uint32_t) uuid.bytes[1] << 16) |
+                                 ((uint32_t) uuid.bytes[2] << 8) |
+                                 ((uint32_t) uuid.bytes[3])); /* Take first 32 byte of UUID */
+
+                        (void) execlp(mkfs, mkfs,
+                                      "-i", vol_id,
+                                      "-n", mangled_label,
+                                      "-F", "32",  /* yes, we force FAT32 here */
+                                      node, NULL);
+
                 } else if (streq(fstype, "swap")) {
 
                         (void) execlp(mkfs, mkfs,