]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
repart: add support for list of definitions directories
authorRichard Phibel <rphibel@googlemail.com>
Thu, 11 Aug 2022 18:20:40 +0000 (20:20 +0200)
committerRichard Phibel <rphibel@googlemail.com>
Fri, 12 Aug 2022 16:05:18 +0000 (18:05 +0200)
src/partition/repart.c
src/partition/test-repart.sh

index b250be71c83342eacd1686e52dc4bb0595d89af7..0c0c0de794ca7d9c96dc0f936422f0f5e4e5d6c5 100644 (file)
@@ -95,7 +95,7 @@ static bool arg_dry_run = true;
 static const char *arg_node = NULL;
 static char *arg_root = NULL;
 static char *arg_image = NULL;
-static char *arg_definitions = NULL;
+static char **arg_definitions = NULL;
 static bool arg_discard = true;
 static bool arg_can_factory_reset = false;
 static int arg_factory_reset = -1;
@@ -114,7 +114,7 @@ static uint32_t arg_tpm2_pcr_mask = UINT32_MAX;
 
 STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
 STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
-STATIC_DESTRUCTOR_REGISTER(arg_definitions, freep);
+STATIC_DESTRUCTOR_REGISTER(arg_definitions, strv_freep);
 STATIC_DESTRUCTOR_REGISTER(arg_key, erase_and_freep);
 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep);
 
@@ -1404,7 +1404,7 @@ static int partition_read_definition(Partition *p, const char *path, const char
 
 static int context_read_definitions(
                 Context *context,
-                const char *directory,
+                char **directories,
                 const char *root) {
 
         _cleanup_strv_free_ char **files = NULL;
@@ -1414,11 +1414,9 @@ static int context_read_definitions(
 
         assert(context);
 
-        dirs = directory ?
-                STRV_MAKE_CONST(directory) :
-                (const char* const*)CONF_PATHS_STRV("repart.d");
+        dirs = (const char* const*) (directories ?: CONF_PATHS_STRV("repart.d"));
 
-        r = conf_files_list_strv(&files, ".conf", directory ? NULL : root, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, dirs);
+        r = conf_files_list_strv(&files, ".conf", directories ? NULL : root, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, dirs);
         if (r < 0)
                 return log_error_errno(r, "Failed to enumerate *.conf files: %m");
 
@@ -4281,11 +4279,15 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_pretty = r;
                         break;
 
-                case ARG_DEFINITIONS:
-                        r = parse_path_argument(optarg, false, &arg_definitions);
+                case ARG_DEFINITIONS: {
+                        _cleanup_free_ char *path = NULL;
+                        r = parse_path_argument(optarg, false, &path);
                         if (r < 0)
                                 return r;
+                        if (strv_consume(&arg_definitions, TAKE_PTR(path)) < 0)
+                                return log_oom();
                         break;
+                }
 
                 case ARG_SIZE: {
                         uint64_t parsed, rounded;
@@ -4904,6 +4906,8 @@ static int run(int argc, char *argv[]) {
         if (!context)
                 return log_oom();
 
+        strv_uniq(arg_definitions);
+
         r = context_read_definitions(context, arg_definitions, arg_root);
         if (r < 0)
                 return r;
index 13df482e00d96799c6987e5cb31ec1be71fd6fc8..1aac7ac10810e09010717254452e73a35ab06f9d 100755 (executable)
@@ -272,3 +272,60 @@ diff <(echo "$JSON_OUTPUT") - <<EOF
        }
 ]
 EOF
+
+echo "### Testing list of definitions directories ###"
+
+mkdir -p "$D/definitions1"
+
+cat >"$D/definitions1/root1.conf" <<EOF
+[Partition]
+Type=swap
+SizeMaxBytes=32M
+UUID=7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0
+Label=label1
+EOF
+
+mkdir -p "$D/definitions2"
+
+cat >"$D/definitions2/root2.conf" <<EOF
+[Partition]
+Type=swap
+SizeMaxBytes=32M
+UUID=837c3d67-21b3-478e-be82-7e7f83bf96d3
+Label=label2
+EOF
+
+rm -f test-definitions
+
+JSON_OUTPUT=$("$repart" --definitions="$D/definitions1" --definitions="$D/definitions2" --dry-run=yes --empty=create --size=100M --json=pretty test-definitions)
+
+diff <(echo "$JSON_OUTPUT") - <<EOF
+[
+       {
+               "type" : "swap",
+               "label" : "label1",
+               "uuid" : "7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0",
+               "file" : "root1.conf",
+               "node" : "test-definitions1",
+               "offset" : 1048576,
+               "old_size" : 0,
+               "raw_size" : 33554432,
+               "old_padding" : 0,
+               "raw_padding" : 0,
+               "activity" : "create"
+       },
+       {
+               "type" : "swap",
+               "label" : "label2",
+               "uuid" : "837c3d67-21b3-478e-be82-7e7f83bf96d3",
+               "file" : "root2.conf",
+               "node" : "test-definitions2",
+               "offset" : 34603008,
+               "old_size" : 0,
+               "raw_size" : 33554432,
+               "old_padding" : 0,
+               "raw_padding" : 0,
+               "activity" : "create"
+       }
+]
+EOF