[PATCH] Support mkswap -U

Like Ma likemartinma at gmail.com
Sat Oct 26 20:38:48 UTC 2019


---
 include/libbb.h     |  3 +++
 libbb/xfuncs.c      | 33 +++++++++++++++++++++++++++++++++
 util-linux/mkswap.c | 38 ++++++++++++++++++++++++--------------
 3 files changed, 60 insertions(+), 14 deletions(-)

diff --git a/include/libbb.h b/include/libbb.h
index 05a560977..73254d3ec 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1013,6 +1013,9 @@ char* hex2bin(char *dst, const char *src, int count) FAST_FUNC;
 /* Generate a UUID */
 void generate_uuid(uint8_t *buf) FAST_FUNC;
 
+/* Convert a string to UUID */
+int parse_uuid (const char* in, uint8_t* buf) FAST_FUNC;
+
 /* Last element is marked by mult == 0 */
 struct suffix_mult {
 	char suffix[4];
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index ee2dbdef1..584c81353 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -429,3 +429,36 @@ int FAST_FUNC wait_for_exitstatus(pid_t pid)
 		bb_simple_perror_msg_and_die("waitpid");
 	return exit_status;
 }
+
+int FAST_FUNC parse_uuid (const char* in, uint8_t* buf)
+{
+	const char* p = in;
+	uint8_t* q = buf;
+	int seg[] = { 8, 4, 4, 4, 12 };
+	int i = 0;
+
+	memset (buf, 0, 16);
+	for (; *p; ++p) {
+		int odd = seg[i] & 0x01;
+		if (odd) *q <<= 4;
+
+		if ('0' <= *p && *p <= '9') {
+			*q += *p - '0';
+		} else if ('a' <= *p && *p <= 'f') {
+			*q += *p - 'a' + 10;
+		} else if ('A' <= *p && *p <= 'F') {
+			*q += *p - 'A' + 10;
+		} else {
+			return -1;
+		}
+
+		if (odd) ++q;
+
+		if (--seg[i] <= 0) {
+			if (++i >= (sizeof (seg) / sizeof (int))) break;
+			if (*++p != '-') return -1;
+		}
+	}
+
+	return i < (sizeof (seg) /sizeof (int)) || *++p ? -1 : 0;
+}
diff --git a/util-linux/mkswap.c b/util-linux/mkswap.c
index 9e51a1dcc..ec89b82b3 100644
--- a/util-linux/mkswap.c
+++ b/util-linux/mkswap.c
@@ -116,12 +116,16 @@ int mkswap_main(int argc UNUSED_PARAM, char **argv)
 	int fd;
 	unsigned pagesize;
 	off_t len;
-	const char *label = "";
+	const char *label = "", *uuid = "";
 
 	INIT_G();
 
-	/* TODO: -p PAGESZ, -U UUID */
-	getopt32(argv, "^" "L:" "\0" "-1"/*at least one arg*/, &label);
+	/* TODO: -p PAGESZ */
+	if (ENABLE_FEATURE_MKSWAP_UUID) {
+		getopt32(argv, "^" "L:U:" "\0" "-1"/*at least one arg*/, &label, &uuid);
+	} else {
+		getopt32(argv, "^" "L:" "\0" "-1"/*at least one arg*/, &label);
+	}
 	argv += optind;
 
 	fd = xopen(argv[0], O_WRONLY);
@@ -148,17 +152,23 @@ int mkswap_main(int argc UNUSED_PARAM, char **argv)
 	hdr->last_page = (uoff_t)len / pagesize;
 
 	if (ENABLE_FEATURE_MKSWAP_UUID) {
-		char uuid_string[32];
-		generate_uuid((void*)hdr->sws_uuid);
-		bin2hex(uuid_string, hdr->sws_uuid, 16);
-		/* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */
-		printf("UUID=%.8s"  "-%.4s-%.4s-%.4s-%.12s\n",
-			uuid_string,
-			uuid_string+8,
-			uuid_string+8+4,
-			uuid_string+8+4+4,
-			uuid_string+8+4+4+4
-		);
+		if (*uuid) {
+			if (parse_uuid(uuid, (void*)hdr->sws_uuid) < 0)
+				bb_error_msg_and_die("Invalid option -U %s\n", uuid);
+			printf("UUID=%s\n", uuid);
+		} else {
+			char uuid_string[32];
+			generate_uuid((void*)hdr->sws_uuid);
+			bin2hex(uuid_string, hdr->sws_uuid, 16);
+			/* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */
+			printf("UUID=%.8s"  "-%.4s-%.4s-%.4s-%.12s\n",
+				uuid_string,
+				uuid_string+8,
+				uuid_string+8+4,
+				uuid_string+8+4+4,
+				uuid_string+8+4+4+4
+			);
+		}
 	}
 	safe_strncpy(hdr->sws_volume, label, 16);
 
-- 
2.23.0



More information about the busybox mailing list