From: Roland Gaudig <
roland...@weidmueller.com>
Signed-off-by: Roland Gaudig <
roland...@weidmueller.com>
---
This patch introduces a handler for creating file systems on spare
partitions. It checks if the desired filesystem exists. In case it
doesn't the file system will be created. In case there is already
a filesystem of the desired type on that partition, nothing will be
changed.
doc/source/handlers.rst | 20 ++++++
doc/source/sw-description.rst | 5 +-
handlers/Config.in | 7 ++
handlers/Makefile | 1 +
handlers/filesystem_handler.c | 120 ++++++++++++++++++++++++++++++++++
5 files changed, 152 insertions(+), 1 deletion(-)
create mode 100644 handlers/filesystem_handler.c
diff --git a/doc/source/handlers.rst b/doc/source/handlers.rst
index 07fb6c2..f9b4fbc 100644
--- a/doc/source/handlers.rst
+++ b/doc/source/handlers.rst
@@ -857,3 +857,23 @@ found on the device. It is a partition handler and it runs before any image is i
"18e12df1-d8e1-4283-8727-37727eb4261d"];
}
});
+
+Filesystem Handler
+------------------
+
+This handler checks if the device already has a filesystem of the specified type.
+If the filesystem does not yet exist, it will be created.
+
+::
+
+ scripts: (
+ {
+ device = "/dev/mmcblk0p1";
+ type = "filesystem";
+ filesystem = "vfat";
+ },
+ {
+ device = "/dev/mmcblk0p2";
+ type = "filesystem";
+ filesystem = "ext2";
+ })
diff --git a/doc/source/sw-description.rst b/doc/source/sw-description.rst
index fd8bd11..98001d9 100644
--- a/doc/source/sw-description.rst
+++ b/doc/source/sw-description.rst
@@ -1242,8 +1242,11 @@ There are 4 main sections inside sw-description:
| | | | rootfs will be used. |
+-------------+----------+------------+---------------------------------------+
| filesystem | string | files | indicates the filesystem type where |
- | | | | the file must be installed. Only |
+ | | | scripts | the file must be installed. Only |
| | | | used if "device" attribute is set. |
+ | | | | In combination witht the filesystem |
+ | | | | handler it indicates the filesystem |
+ | | | | which shall be created. |
+-------------+----------+------------+---------------------------------------+
| path | string | files | For files: indicates the path |
| | | | (absolute) where the file must be |
diff --git a/handlers/Config.in b/handlers/Config.in
index 8571c0c..a646610 100644
--- a/handlers/Config.in
+++ b/handlers/Config.in
@@ -99,6 +99,13 @@ config DISKPART
help
Handler to partition a disk, eMMC or SD
+config FILESYSTEM
+ bool "filesystem handler for creating file systems on empty partitions"
+ default n
+ help
+ The filesystem handler checks if a partition already contains the
+ specified filesystem. If not, the file system will be created.
+
comment "diskpart support needs libfdisk"
depends on !HAVE_LIBFDISK
diff --git a/handlers/Makefile b/handlers/Makefile
index 1449139..c0d78b6 100644
--- a/handlers/Makefile
+++ b/handlers/Makefile
@@ -12,6 +12,7 @@ obj-$(CONFIG_ARCHIVE) += archive_handler.o
obj-$(CONFIG_BOOTLOADERHANDLER) += boot_handler.o
obj-$(CONFIG_CFI) += flash_handler.o
obj-$(CONFIG_DISKPART) += diskpart_handler.o
+obj-$(CONFIG_FILESYSTEM) += filesystem_handler.o
obj-$(CONFIG_UNIQUEUUID) += uniqueuuid_handler.o
obj-$(CONFIG_CFIHAMMING1)+= flash_hamming1_handler.o
obj-$(CONFIG_LUASCRIPTHANDLER) += lua_scripthandler.o
diff --git a/handlers/filesystem_handler.c b/handlers/filesystem_handler.c
new file mode 100644
index 0000000..f7ba3ee
--- /dev/null
+++ b/handlers/filesystem_handler.c
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2021 Weidmueller Interface GmbH & Co. KG
+ * Roland Gaudig <
roland...@weidmueller.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "swupdate.h"
+#include "handler.h"
+#include "pctl.h"
+#include "util.h"
+
+void filesystem_handler_init(void);
+
+
+static int filesystem(struct img_type *img,
+ void __attribute__ ((__unused__)) *data)
+{
+ char *lbltype = img->filesystem;
+ int ret = 0;
+ char command[SWUPDATE_GENERAL_STRING_SIZE];
+ int len;
+ int exit_code;
+
+ if (!data)
+ return -EINVAL;
+
+ script_fn scriptfn = *(script_fn *)data;
+
+ /*
+ * Run only in case of pre-install
+ */
+ if (scriptfn != PREINSTALL)
+ return 0;
+
+ if (!strlen(img->device)) {
+ ERROR("File system handler requires setting \"device\" attribute");
+ return -EINVAL;
+ }
+
+ if (!lbltype) {
+ ERROR("File system handler requires setting \"filesystem\" attribute");
+ return -EINVAL;
+ }
+
+ /* Check if filesystem exists */
+ len = snprintf(command, SWUPDATE_GENERAL_STRING_SIZE,
+ "fsck.%s -n %s", lbltype, img->device);
+ if (len < 0) {
+ ERROR("File snprintf");
+ return -1;
+ }
+
+ if (len > (SWUPDATE_GENERAL_STRING_SIZE - 1)) {
+ ERROR("Path name to device is to long");
+ return -1;
+ }
+
+ TRACE("Check if %s file system exists on %s ...", lbltype, img->device);
+ exit_code = run_system_cmd(command);
+
+ if (exit_code == -1) {
+ ERROR("Could not run %s", command);
+ return -1;
+ }
+
+ if (exit_code == 127) {
+ ERROR("fsck.%s not found, %s filesystem not supported", lbltype, lbltype);
+ return -1;
+ }
+
+ if (exit_code == 0) {
+ TRACE("Found %s file system on %s", lbltype, img->device);
+ return 0;
+ }
+
+ if (exit_code > 0) {
+ /* File system does not exist, create new file system */
+ TRACE("Creating new %s file system on %s", lbltype, img->device);
+ len = snprintf(command, SWUPDATE_GENERAL_STRING_SIZE,
+ "mkfs.%s %s", lbltype, img->device);
+
+ if (len < 0) {
+ ERROR("File snprintf");
+ return -1;
+ }
+
+ if (len > (SWUPDATE_GENERAL_STRING_SIZE - 1)) {
+ ERROR("Path name to device is to long");
+ return -1;
+ }
+
+ exit_code = run_system_cmd(command);
+
+ if (exit_code) {
+ if (exit_code == -1) {
+ ERROR("File could not run %s", command);
+ return -1;
+ } else {
+ ERROR("Couldn't create %s filesystem on %s",
+ lbltype, img->device);
+ return -1;
+ }
+ }
+ }
+
+ return ret;
+}
+
+
+__attribute__((constructor))
+void filesystem_handler_init(void)
+{
+ register_handler("filesystem", filesystem,
+ SCRIPT_HANDLER | NO_DATA_HANDLER, NULL);
+}
--
2.25.1