Skip to contentSkip to conversation

filter-partitiond: ignore invalid partitions of loop devices

PATCH New Lidong Zhong 1 participant 1 message Nov 25, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 filter-partitiond: ignore invalid partitions of loop devices

After detaching the loop device, partitions of the device become invalid but sometimes they are still visible. And running lvm2 commands will report error messages like:

linux-cno1:~ # pvs
Error reading device /dev/loop0p1 at 0 length 4096. Error reading device /dev/loop0p2 at 0 length 4096.
PV VG Fmt Attr PSize PFree
/dev/sda1 lvm2 --- 1.00g 1.00g

This patch is written for ignoring the invalid partitions of loop devices.

---
 lib/filters/filter-partitioned.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
lib/filters/filter-partitioned.c +33 -0
index 1a700543c..1f04601bd 100644
@@ -19,10 +19,35 @@
 
 #define MSG_SKIPPING "%s: Skipping: Partition table signature found"
 
+static int _loop_is_with_partscan(dev_t dev)
+{
+	FILE *fp;
+	int partscan = 1;
+	char path[PATH_MAX];
+	char buffer[64];
+
+	if (dm_snprintf(path, sizeof(path), "%sdev/block/%d:%d/loop/partscan",
+			dm_sysfs_dir(),
+			(int) MAJOR(dev),
+			(int) MINOR(dev)) < 0) {
+		log_warn("Sysfs path for partscan is too long.");
+		return 0;
+	}
+
+	if (!(fp = fopen(path, "r")))
+		return 0; /* not there -> no partscan */
+
+	if (fclose(fp))
+		log_sys_debug("fclose", path);
+
+	return partscan;
+}
+
 static int _passes_partitioned_filter(struct cmd_context *cmd, struct dev_filter *f, struct device *dev, const char *use_filter_name)
 {
 	struct dev_types *dt = (struct dev_types *) f->private;
 	int ret;
+	dev_t devt;
 
 	ret = dev_is_partitioned(dt, dev);
 
@@ -42,6 +67,14 @@ static int _passes_partitioned_filter(struct cmd_context *cmd, struct dev_filter
 		return 0;
 	}
 
+	/*check if it's a valid loop partition*/
+	if (MAJOR(dev->dev) == dt->blkext_major && (2 == dev_get_primary_dev(dt, dev, &devt))) {
+		if(MAJOR(devt) == dt->loop_major && !_loop_is_with_partscan(devt)) {
+			log_debug_devs("Partitioned filter skip invalide loop partition: %s.", dev_name(dev));
+			return 0;
+		}   
+	}
+
 	return 1;
 }
 
Signature · 1 line
2.16.4