From 857615d6d258d5bb3badf4caebd4bc8e69f1a33a Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 10 May 2024 13:45:59 +0100 Subject: [PATCH] daemon/parted: Assume sfdisk --part-type exists This "new" parameter was added in 2014: commit 8eab3194ce1737a167812d5e84d83b0dfc253fac Author: Karel Zak Date: Mon Sep 15 12:37:52 2014 +0200 sfdisk: add --parttype The patch also makes --{id,change-id,print-id} deprecated in favour of --parttype. The original --id is too generic option name and the --print-id and --change-id are unnecessary and inconsistent with another sfdisk options (e.g. we don't have --change-bootable) Also remove an extraneous / incorrect comment about parted. As history has played out, sfdisk proves to be the better tool and parted is a PITA. --- daemon/parted.c | 32 +++----------------------------- daemon/parted.ml | 22 +--------------------- 2 files changed, 4 insertions(+), 50 deletions(-) diff --git a/daemon/parted.c b/daemon/parted.c index 6d33e5a60..d0f2ce03b 100644 --- a/daemon/parted.c +++ b/daemon/parted.c @@ -424,30 +424,6 @@ do_part_get_bootable (const char *device, int partnum) return strstr (boot, "boot") != NULL; } -/* Test if sfdisk is recent enough to have --part-type, to be used instead - * of --print-id and --change-id. - */ -static int -test_sfdisk_has_part_type (void) -{ - static int tested = -1; - - if (tested != -1) - return tested; - - int r; - CLEANUP_FREE char *out = NULL, *err = NULL; - - r = command (&out, &err, "sfdisk", "--help", NULL); - if (r == -1) { - reply_with_error ("%s: %s", "sfdisk --help", err); - return -1; - } - - tested = strstr (out, "--part-type") != NULL; - return tested; -} - int do_part_set_mbr_id (const char *device, int partnum, int idbyte) { @@ -456,8 +432,6 @@ do_part_set_mbr_id (const char *device, int partnum, int idbyte) return -1; } - const char *param = test_sfdisk_has_part_type () ? "--part-type" : "--change-id"; - char partnum_str[16]; snprintf (partnum_str, sizeof partnum_str, "%d", partnum); @@ -470,10 +444,10 @@ do_part_set_mbr_id (const char *device, int partnum, int idbyte) udev_settle (); - r = command (NULL, &err, "sfdisk", - param, device, partnum_str, idbyte_str, NULL); + r = command (NULL, &err, "sfdisk", "--part-type", + device, partnum_str, idbyte_str, NULL); if (r == -1) { - reply_with_error ("sfdisk %s: %s", param, err); + reply_with_error ("sfdisk --part-type: %s", err); return -1; } diff --git a/daemon/parted.ml b/daemon/parted.ml index e0ca7a605..c9e55890b 100644 --- a/daemon/parted.ml +++ b/daemon/parted.ml @@ -25,33 +25,13 @@ open Utils include Structs -(* Test if [sfdisk] is recent enough to have [--part-type], to be used - * instead of [--print-id] and [--change-id]. - *) -let test_sfdisk_has_part_type = lazy ( - let out = command "sfdisk" ["--help"] in - String.find out "--part-type" >= 0 -) - -(* Currently we use sfdisk for getting and setting the ID byte. In - * future, extend parted to provide this functionality. As a result - * of using sfdisk, this won't work for non-MBR-style partitions, but - * that limitation is noted in the documentation and we can extend it - * later without breaking the ABI. - *) let part_get_mbr_id device partnum = if partnum <= 0 then failwith "partition number must be >= 1"; - let param = - if Lazy.force test_sfdisk_has_part_type then - "--part-type" - else - "--print-id" in - udev_settle (); let out = - command "sfdisk" [param; device; string_of_int partnum] in + command "sfdisk" ["--part-type"; device; string_of_int partnum] in udev_settle (); (* It's printed in hex, possibly with a leading space. *)