From e031166e15184d861bcfc0dfe8d6378178b89e74 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 16 Jun 2020 11:43:11 +0200 Subject: [PATCH 1/2] repart: add simple specifier expansion in Label= Strictly speaking this is a compat breakage, but given the tool was added only in the last release, let's try to sail under the radar, and fix this early before anyone notices it wasn't supported always. --- man/repart.d.xml | 39 ++++++++++++++++++++++++++++++++++-- src/partition/repart.c | 32 +++++++++++++++++++++++------ src/partition/test-repart.sh | 9 ++++++--- 3 files changed, 69 insertions(+), 11 deletions(-) diff --git a/man/repart.d.xml b/man/repart.d.xml index f2c155e200..648d32625f 100644 --- a/man/repart.d.xml +++ b/man/repart.d.xml @@ -1,7 +1,8 @@ - + repart.d @@ -214,7 +215,7 @@ setting is not used for matching. It is also not used when a label is already set for an existing partition. It is thus only used when a partition is newly created or when an existing one had a no label set (that is: an empty label). If not specified a label derived from the partition type is - automatically used. + automatically used. Simple specifier expansion is supported, see below. @@ -315,6 +316,40 @@ + + Specifiers + + Specifiers may be used in the Label= setting. The following expansions are understood: + + Specifiers available + + + + + + + Specifier + Meaning + Details + + + + + + + + + + + + + + + + +
+
+ Examples diff --git a/src/partition/repart.c b/src/partition/repart.c index 4c6e459529..76fc8bd1c9 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -41,6 +41,7 @@ #include "pretty-print.h" #include "proc-cmdline.h" #include "sort-util.h" +#include "specifier.h" #include "stat-util.h" #include "stdio-util.h" #include "string-util.h" @@ -857,20 +858,42 @@ static int config_parse_label( void *data, void *userdata) { + static const Specifier specifier_table[] = { + { 'm', specifier_machine_id, NULL }, + { 'b', specifier_boot_id, NULL }, + { 'H', specifier_host_name, NULL }, + { 'l', specifier_short_host_name, NULL }, + { 'v', specifier_kernel_release, NULL }, + { 'a', specifier_architecture, NULL }, + { 'o', specifier_os_id, NULL }, + { 'w', specifier_os_version_id, NULL }, + { 'B', specifier_os_build_id, NULL }, + { 'W', specifier_os_variant_id, NULL }, + {} + }; + _cleanup_free_ char16_t *recoded = NULL; + _cleanup_free_ char *resolved = NULL; char **label = data; int r; assert(rvalue); assert(label); - if (!utf8_is_valid(rvalue)) { + r = specifier_printf(rvalue, specifier_table, NULL, &resolved); + if (r < 0) { + log_syntax(unit, LOG_ERR, filename, line, r, + "Failed to expand specifiers in Label=, ignoring: %s", rvalue); + return 0; + } + + if (!utf8_is_valid(resolved)) { log_syntax(unit, LOG_WARNING, filename, line, 0, "Partition label not valid UTF-8, ignoring: %s", rvalue); return 0; } - recoded = utf8_to_utf16(rvalue, strlen(rvalue)); + recoded = utf8_to_utf16(resolved, strlen(resolved)); if (!recoded) return log_oom(); @@ -880,10 +903,7 @@ static int config_parse_label( return 0; } - r = free_and_strdup(label, rvalue); - if (r < 0) - return log_oom(); - + free_and_replace(*label, resolved); return 0; } diff --git a/src/partition/test-repart.sh b/src/partition/test-repart.sh index e1ff357929..897f11411a 100755 --- a/src/partition/test-repart.sh +++ b/src/partition/test-repart.sh @@ -33,6 +33,7 @@ ln -s root.conf $D/definitions/root2.conf cat >$D/definitions/home.conf < $D/definitions/swap.conf <$D/populated +UNAME=`uname -r` + cmp $D/populated - < Date: Tue, 16 Jun 2020 14:38:44 +0200 Subject: [PATCH 2/2] repart: if now minimal size is specified, default to 10M Prompted by this discussion: https://lists.freedesktop.org/archives/systemd-devel/2020-June/044669.html --- man/repart.d.xml | 3 ++- src/partition/repart.c | 17 +++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/man/repart.d.xml b/man/repart.d.xml index 648d32625f..32df1e3d7f 100644 --- a/man/repart.d.xml +++ b/man/repart.d.xml @@ -292,7 +292,8 @@ SizeMaxBytes=) otherwise. If the backing device does not provide enough space to fulfill the constraints placing the partition will fail. For partitions that shall be created, depending on the setting of Priority= (see above) the partition might be dropped - and the placing algorithm restarted. By default no size constraints are set. + and the placing algorithm restarted. By default a minimum size constraint of 10M and no maximum size + constraint is set.
diff --git a/src/partition/repart.c b/src/partition/repart.c index 76fc8bd1c9..d3706fe180 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -49,6 +49,12 @@ #include "terminal-util.h" #include "utf8.h" +/* If not configured otherwise use a minimal partition size of 10M */ +#define DEFAULT_MIN_SIZE (10*1024*1024) + +/* Hard lower limit for new partition sizes */ +#define HARD_MIN_SIZE 4096 + /* Note: When growing and placing new partitions we always align to 4K sector size. It's how newer hard disks * are designed, and if everything is aligned to that performance is best. And for older hard disks with 512B * sector size devices were generally assumed to have an even number of sectors, hence at the worst we'll @@ -322,7 +328,9 @@ static uint64_t partition_min_size(const Partition *p) { /* Calculate the disk space we really need at minimum for this partition. If the partition already * exists the current size is what we really need. If it doesn't exist yet refuse to allocate less - * than 4K. */ + * than 4K. + * + * DEFAULT_MIN_SIZE is the default SizeMin= we configure if nothing else is specified. */ if (PARTITION_IS_FOREIGN(p)) { /* Don't allow changing size of partitions not managed by us */ @@ -330,11 +338,8 @@ static uint64_t partition_min_size(const Partition *p) { return p->current_size; } - sz = p->current_size != UINT64_MAX ? p->current_size : 4096; - if (p->size_min != UINT64_MAX) - return MAX(p->size_min, sz); - - return sz; + sz = p->current_size != UINT64_MAX ? p->current_size : HARD_MIN_SIZE; + return MAX(p->size_min == UINT64_MAX ? DEFAULT_MIN_SIZE : p->size_min, sz); } static uint64_t partition_max_size(const Partition *p) {