diff mbox series

[1/1] diskpart: ensure expected filesystems are present/created

Message ID 20210923105102.88933-1-james.hilliard1@gmail.com
State Accepted
Headers show
Series [1/1] diskpart: ensure expected filesystems are present/created | expand

Commit Message

James Hilliard Sept. 23, 2021, 10:51 a.m. UTC
Right now we only create filesystems when the partition table
changes, however this can be result in missing filesystems if
the disk already has the expected partition table layout but
does not have the expected filesystems.

Ensure that the specified filesystems are present and create
them if they are missing.

We retain the existing behavior of unconditionally creating
new filesystems in cases where the partition table is changed.

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 fs/diskformat.c               | 37 +++++++++++++++++++++++++++++++++
 handlers/diskformat_handler.c | 39 +----------------------------------
 handlers/diskpart_handler.c   | 21 ++++++++++++++++++-
 include/fs_interface.h        |  2 ++
 4 files changed, 60 insertions(+), 39 deletions(-)

Comments

Stefano Babic Oct. 5, 2021, 12:32 p.m. UTC | #1
On 23.09.21 12:51, James Hilliard wrote:
> Right now we only create filesystems when the partition table
> changes, however this can be result in missing filesystems if
> the disk already has the expected partition table layout but
> does not have the expected filesystems.
> 
> Ensure that the specified filesystems are present and create
> them if they are missing.
> 
> We retain the existing behavior of unconditionally creating
> new filesystems in cases where the partition table is changed.
> 
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> ---
>   fs/diskformat.c               | 37 +++++++++++++++++++++++++++++++++
>   handlers/diskformat_handler.c | 39 +----------------------------------
>   handlers/diskpart_handler.c   | 21 ++++++++++++++++++-
>   include/fs_interface.h        |  2 ++
>   4 files changed, 60 insertions(+), 39 deletions(-)
> 
> diff --git a/fs/diskformat.c b/fs/diskformat.c
> index 450dd17..0841f0b 100644
> --- a/fs/diskformat.c
> +++ b/fs/diskformat.c
> @@ -35,6 +35,43 @@ static struct supported_filesystems fs[] = {
>   #endif
>   };
>   
> +/*
> + * Checks if file system fstype already exists on device.
> + * return 0 if not exists, 1 if exists, negative values on failure
> + */
> +int diskformat_fs_exists(char *device, char *fstype)
> +{
> +	char buf[10];
> +	const char *value = buf;
> +	size_t len;
> +	blkid_probe pr;
> +	int ret = 0;
> +
> +	pr = blkid_new_probe_from_filename(device);
> +
> +	if (!pr) {
> +		ERROR("%s: failed to create libblkid probe",
> +			  device);
> +		return -EFAULT;
> +	}
> +
> +	while (blkid_do_probe(pr) == 0) {
> +		if (blkid_probe_lookup_value(pr, "TYPE", &value, &len)) {
> +			ERROR("blkid_probe_lookup_value failed");
> +			ret = -EFAULT;
> +			break;
> +		}
> +
> +		if (!strncmp(value, fstype, sizeof(buf))) {
> +			ret = 1;
> +			break;
> +		}
> +	}
> +	blkid_free_probe(pr);
> +
> +	return ret;
> +}
> +
>   int diskformat_mkfs(char *device, char *fstype)
>   {
>   	int index;
> diff --git a/handlers/diskformat_handler.c b/handlers/diskformat_handler.c
> index 409a35e..bf236b2 100644
> --- a/handlers/diskformat_handler.c
> +++ b/handlers/diskformat_handler.c
> @@ -14,43 +14,6 @@
>   
>   void diskformat_handler(void);
>   
> -/*
> - * Checks if file system fstype already exists on device.
> - * return 0 if not exists, 1 if exists, negative values on failure
> - */
> -static int fs_exists(char *device, char *fstype)
> -{
> -	char buf[10];
> -	const char *value = buf;
> -	size_t len;
> -	blkid_probe pr;
> -	int ret = 0;
> -
> -	pr = blkid_new_probe_from_filename(device);
> -
> -	if (!pr) {
> -		ERROR("%s: failed to create libblkid probe",
> -		      device);
> -		return -EFAULT;
> -	}
> -
> -	while (blkid_do_probe(pr) == 0) {
> -		if (blkid_probe_lookup_value(pr, "TYPE", &value, &len)) {
> -			ERROR("blkid_probe_lookup_value failed");
> -			ret = -EFAULT;
> -			break;
> -		}
> -
> -		if (!strncmp(value, fstype, sizeof(buf))) {
> -			ret = 1;
> -			break;
> -		}
> -	}
> -	blkid_free_probe(pr);
> -
> -	return ret;
> -}
> -
>   static int diskformat(struct img_type *img,
>   		      void __attribute__ ((__unused__)) *data)
>   {
> @@ -74,7 +37,7 @@ static int diskformat(struct img_type *img,
>   		; /* Skip file system exists check */
>   	} else {
>   		/* Check if file system exists */
> -		ret = fs_exists(img->device, fstype);
> +		ret = diskformat_fs_exists(img->device, fstype);
>   
>   		if (ret < 0)
>   			return ret;
> diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c
> index d079c8f..227387f 100644
> --- a/handlers/diskpart_handler.c
> +++ b/handlers/diskpart_handler.c
> @@ -951,7 +951,7 @@ handler_release:
>   
>   #ifdef CONFIG_DISKPART_FORMAT
>   	/* Create filesystems */
> -	if (!ret && createtable->parent) {
> +	if (!ret) {
>   		LIST_FOREACH(part, &priv.listparts, next) {
>   			/*
>   			 * priv.listparts counts partitions starting with 0,
> @@ -971,6 +971,25 @@ handler_release:
>   				path = strdup(img->device);
>   			device = fdisk_partname(path, partno);
>   			free(path);
> +
> +			if (!createtable->parent) {
> +				/* Check if file system exists */
> +				ret = diskformat_fs_exists(device, part->fstype);
> +
> +				if (ret < 0) {
> +					free(device);
> +					break;
> +				}
> +
> +				if (ret) {
> +					TRACE("Found %s file system on %s, skip mkfs",
> +						  part->fstype, device);
> +					ret = 0;
> +					free(device);
> +					continue;
> +				}
> +			}
> +
>   			ret = diskformat_mkfs(device, part->fstype);
>   			free(device);
>   			if (ret)
> diff --git a/include/fs_interface.h b/include/fs_interface.h
> index c0120e9..25c22e5 100644
> --- a/include/fs_interface.h
> +++ b/include/fs_interface.h
> @@ -7,6 +7,8 @@
>   #ifndef _FS_INTERFACE_H
>   #define _FS_INTERFACE_H
>   
> +int diskformat_fs_exists(char *device, char *fstype);
> +
>   int diskformat_mkfs(char *device, char *fstype);
>   
>   #if defined(CONFIG_FAT_FILESYSTEM)
> 

Applied to -master, thanks !

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/fs/diskformat.c b/fs/diskformat.c
index 450dd17..0841f0b 100644
--- a/fs/diskformat.c
+++ b/fs/diskformat.c
@@ -35,6 +35,43 @@  static struct supported_filesystems fs[] = {
 #endif
 };
 
+/*
+ * Checks if file system fstype already exists on device.
+ * return 0 if not exists, 1 if exists, negative values on failure
+ */
+int diskformat_fs_exists(char *device, char *fstype)
+{
+	char buf[10];
+	const char *value = buf;
+	size_t len;
+	blkid_probe pr;
+	int ret = 0;
+
+	pr = blkid_new_probe_from_filename(device);
+
+	if (!pr) {
+		ERROR("%s: failed to create libblkid probe",
+			  device);
+		return -EFAULT;
+	}
+
+	while (blkid_do_probe(pr) == 0) {
+		if (blkid_probe_lookup_value(pr, "TYPE", &value, &len)) {
+			ERROR("blkid_probe_lookup_value failed");
+			ret = -EFAULT;
+			break;
+		}
+
+		if (!strncmp(value, fstype, sizeof(buf))) {
+			ret = 1;
+			break;
+		}
+	}
+	blkid_free_probe(pr);
+
+	return ret;
+}
+
 int diskformat_mkfs(char *device, char *fstype)
 {
 	int index;
diff --git a/handlers/diskformat_handler.c b/handlers/diskformat_handler.c
index 409a35e..bf236b2 100644
--- a/handlers/diskformat_handler.c
+++ b/handlers/diskformat_handler.c
@@ -14,43 +14,6 @@ 
 
 void diskformat_handler(void);
 
-/*
- * Checks if file system fstype already exists on device.
- * return 0 if not exists, 1 if exists, negative values on failure
- */
-static int fs_exists(char *device, char *fstype)
-{
-	char buf[10];
-	const char *value = buf;
-	size_t len;
-	blkid_probe pr;
-	int ret = 0;
-
-	pr = blkid_new_probe_from_filename(device);
-
-	if (!pr) {
-		ERROR("%s: failed to create libblkid probe",
-		      device);
-		return -EFAULT;
-	}
-
-	while (blkid_do_probe(pr) == 0) {
-		if (blkid_probe_lookup_value(pr, "TYPE", &value, &len)) {
-			ERROR("blkid_probe_lookup_value failed");
-			ret = -EFAULT;
-			break;
-		}
-
-		if (!strncmp(value, fstype, sizeof(buf))) {
-			ret = 1;
-			break;
-		}
-	}
-	blkid_free_probe(pr);
-
-	return ret;
-}
-
 static int diskformat(struct img_type *img,
 		      void __attribute__ ((__unused__)) *data)
 {
@@ -74,7 +37,7 @@  static int diskformat(struct img_type *img,
 		; /* Skip file system exists check */
 	} else {
 		/* Check if file system exists */
-		ret = fs_exists(img->device, fstype);
+		ret = diskformat_fs_exists(img->device, fstype);
 
 		if (ret < 0)
 			return ret;
diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c
index d079c8f..227387f 100644
--- a/handlers/diskpart_handler.c
+++ b/handlers/diskpart_handler.c
@@ -951,7 +951,7 @@  handler_release:
 
 #ifdef CONFIG_DISKPART_FORMAT
 	/* Create filesystems */
-	if (!ret && createtable->parent) {
+	if (!ret) {
 		LIST_FOREACH(part, &priv.listparts, next) {
 			/*
 			 * priv.listparts counts partitions starting with 0,
@@ -971,6 +971,25 @@  handler_release:
 				path = strdup(img->device);
 			device = fdisk_partname(path, partno);
 			free(path);
+
+			if (!createtable->parent) {
+				/* Check if file system exists */
+				ret = diskformat_fs_exists(device, part->fstype);
+
+				if (ret < 0) {
+					free(device);
+					break;
+				}
+
+				if (ret) {
+					TRACE("Found %s file system on %s, skip mkfs",
+						  part->fstype, device);
+					ret = 0;
+					free(device);
+					continue;
+				}
+			}
+
 			ret = diskformat_mkfs(device, part->fstype);
 			free(device);
 			if (ret)
diff --git a/include/fs_interface.h b/include/fs_interface.h
index c0120e9..25c22e5 100644
--- a/include/fs_interface.h
+++ b/include/fs_interface.h
@@ -7,6 +7,8 @@ 
 #ifndef _FS_INTERFACE_H
 #define _FS_INTERFACE_H
 
+int diskformat_fs_exists(char *device, char *fstype);
+
 int diskformat_mkfs(char *device, char *fstype);
 
 #if defined(CONFIG_FAT_FILESYSTEM)