Skip to contentSkip to conversation

Some fixes for mdadm

PATCH Applied Xiao Ni 3 participants 7 messages Sep 15, 2020 – Oct 15, 2020 MD RAID

lore
Share & tools
lore

Xiao Ni

author
#0 lore raw

v2 cover Some fixes for mdadm

Hi all

These two patches fix some mdadm problems. The first is to avoid one raid being not active after boot. The second patch is to check journal device when creating bitmap.

v2:Change patch1's comments to make it more clear

Xiao Ni (2):
Check hostname file empty or not when creating raid device Don't create bitmap for raid5 with journal disk

 Create.c |  1 +
 mdadm.c  |  3 +++
 mdadm.h  |  1 +
 util.c   | 19 +++++++++++++++++++
 4 files changed, 24 insertions(+)
Signature · 1 line
2.7.5
author
#1 lore raw

PATCH v2 1/2 Check hostname file empty or not when creating raid device

If /etc/hostname is empty and the hostname is decided by network(dhcp, e.g.), there is a risk that raid device will not be in active state after boot. It will be auto-read-only state. It depends on the boot sequence. If the storage starts before network. The system detects disks first, udev rules are triggered and raid device is assemble automatically. But the network hasn't started successfully. So mdadm can't get the right hostname. The raid device will be treated as a foreign raid.
Add a note message if /etc/hostname is empty when creating a raid device.

---
 mdadm.c |  3 +++
 mdadm.h |  1 +
 util.c  | 19 +++++++++++++++++++
 3 files changed, 23 insertions(+)
mdadm.c +3 -0
index 1b3467f..e551958 100644
@@ -1408,6 +1408,9 @@ int main(int argc, char *argv[])
 	if (c.homehost == NULL && c.require_homehost)
 		c.homehost = conf_get_homehost(&c.require_homehost);
 	if (c.homehost == NULL || strcasecmp(c.homehost, "<system>") == 0) {
+		if (check_hostname())
+			pr_err("Note: The file /etc/hostname is empty. There is a risk the raid\n"
+				"      can't be active after boot\n");
 		if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
 			sys_hostname[sizeof(sys_hostname)-1] = 0;
 			c.homehost = sys_hostname;
mdadm.h +1 -0
index 399478b..3ef1209 100644
@@ -1480,6 +1480,7 @@ extern int parse_cluster_confirm_arg(char *inp, char **devname, int *slot);
 extern int check_ext2(int fd, char *name);
 extern int check_reiser(int fd, char *name);
 extern int check_raid(int fd, char *name);
+extern int check_hostname(void);
 extern int check_partitions(int fd, char *dname,
 			    unsigned long long freesize,
 			    unsigned long long size);
util.c +19 -0
index 579dd42..de5bad0 100644
@@ -694,6 +694,25 @@ int check_raid(int fd, char *name)
 	return 1;
 }
 
+/* It checks /etc/hostname has value or not */
+int check_hostname()
+{
+	int fd, ret = 0;
+	char buf[256];
+
+	fd = open("/etc/hostname", O_RDONLY);
+	if (fd < 0) {
+		ret = 1;
+		goto out;
+	}
+
+	if (read(fd, buf, sizeof(buf)) == 0)
+		ret = 1;
+out:
+	close(fd);
+	return ret;
+}
+
 int fstat_is_blkdev(int fd, char *devname, dev_t *rdev)
 {
 	struct stat stb;
Signature · 1 line
2.7.5
#2 lore raw

Re: [PATCH V2 1/2] Check hostname file empty or not when creating raid device

Quoted 53 lines · Xiao Ni

If /etc/hostname is empty and the hostname is decided by network(dhcp, e.g.), there is a risk that raid device will not be in active state after boot. It will be auto-read-only state. It depends on the boot sequence. If the storage starts before network. The system detects disks first, udev rules are triggered and raid device is assemble automatically. But the network hasn't started successfully. So mdadm can't get the right hostname. The raid device will be treated as a foreign raid.
Add a note message if /etc/hostname is empty when creating a raid device.

---
 mdadm.c |  3 +++
 mdadm.h |  1 +
 util.c  | 19 +++++++++++++++++++
 3 files changed, 23 insertions(+)
mdadm.c +3 -0
index 1b3467f..e551958 100644
@@ -1408,6 +1408,9 @@ int main(int argc, char *argv[])
        if (c.homehost == NULL && c.require_homehost)
                c.homehost = conf_get_homehost(&c.require_homehost);
        if (c.homehost == NULL || strcasecmp(c.homehost, "<system>") == 0) {
+               if (check_hostname())
+                       pr_err("Note: The file /etc/hostname is empty. There is a risk the raid\n"
+                               "      can't be active after boot\n");
                if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
                        sys_hostname[sizeof(sys_hostname)-1] = 0;
                        c.homehost = sys_hostname;
mdadm.h +1 -0
index 399478b..3ef1209 100644
@@ -1480,6 +1480,7 @@ extern int parse_cluster_confirm_arg(char *inp, char **devname, int *slot);
 extern int check_ext2(int fd, char *name);
 extern int check_reiser(int fd, char *name);
 extern int check_raid(int fd, char *name);
+extern int check_hostname(void);
 extern int check_partitions(int fd, char *dname,
                            unsigned long long freesize,
                            unsigned long long size);
util.c +4 -0
index 579dd42..de5bad0 100644
@@ -694,6 +694,25 @@ int check_raid(int fd, char *name)
        return 1;
 }
 
+/* It checks /etc/hostname has value or not */
+int check_hostname()
+{
+       int fd, ret = 0;

util.c int check_raid(int fd, char *name) · line 710

Quoted 10 lines
util.c +10 -0
+       char buf[256];
+
+       fd = open("/etc/hostname", O_RDONLY);
+       if (fd < 0) {
+               ret = 1;
+               goto out;
+       }
+
+       if (read(fd, buf, sizeof(buf)) == 0)
+               ret = 1;

Why not use stat() here, since you don't use file content? Also, any error from read() - mean file "not empty". This is right?

Quoted 8 lines
util.c +5 -0
+out:
+       close(fd);
+       return ret;
+}
+
 int fstat_is_blkdev(int fd, char *devname, dev_t *rdev)
 {
        struct stat stb;
#3 lore raw

Re: [PATCH V2 1/2] Check hostname file empty or not when creating raid device

In reply to Xiao Ni
Quoted 50 lines · Xiao Ni

If /etc/hostname is empty and the hostname is decided by network(dhcp, e.g.), there is a risk that raid device will not be in active state after boot. It will be auto-read-only state. It depends on the boot sequence. If the storage starts before network. The system detects disks first, udev rules are triggered and raid device is assemble automatically. But the network hasn't started successfully. So mdadm can't get the right hostname. The raid device will be treated as a foreign raid.
Add a note message if /etc/hostname is empty when creating a raid device.

---
 mdadm.c |  3 +++
 mdadm.h |  1 +
 util.c  | 19 +++++++++++++++++++
 3 files changed, 23 insertions(+)
mdadm.c +3 -0
index 1b3467f..e551958 100644
@@ -1408,6 +1408,9 @@ int main(int argc, char *argv[])
 	if (c.homehost == NULL && c.require_homehost)
 		c.homehost = conf_get_homehost(&c.require_homehost);
 	if (c.homehost == NULL || strcasecmp(c.homehost, "<system>") == 0) {
+		if (check_hostname())
+			pr_err("Note: The file /etc/hostname is empty. There is a risk the raid\n"
+				"      can't be active after boot\n");
 		if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
 			sys_hostname[sizeof(sys_hostname)-1] = 0;
 			c.homehost = sys_hostname;
mdadm.h +1 -0
index 399478b..3ef1209 100644
@@ -1480,6 +1480,7 @@ extern int parse_cluster_confirm_arg(char *inp, char **devname, int *slot);
 extern int check_ext2(int fd, char *name);
 extern int check_reiser(int fd, char *name);
 extern int check_raid(int fd, char *name);
+extern int check_hostname(void);
 extern int check_partitions(int fd, char *dname,
 			    unsigned long long freesize,
 			    unsigned long long size);
util.c +1 -0
index 579dd42..de5bad0 100644
@@ -694,6 +694,25 @@ int check_raid(int fd, char *name)
 	return 1;
 }
 
+/* It checks /etc/hostname has value or not */

util.c int check_raid(int fd, char *name) · line 707

Quoted 10 lines
util.c +10 -0
+int check_hostname()
+{
+	int fd, ret = 0;
+	char buf[256];
+
+	fd = open("/etc/hostname", O_RDONLY);
+	if (fd < 0) {
+		ret = 1;
+		goto out;
+	}

I don't think this is the right approach. If someone uses dhcp to obtain the hostname and explicitly configured the raid to start after the network, they shouldn't get nagged by this message.

Any reason you cannot use gethostname(2) for this?

Thanks,
Jes

author
#4 lore raw

Re: [PATCH V2 1/2] Check hostname file empty or not when creating raid device

Quoted 65 lines · Jes Sorensen
Quoted 50 lines · Xiao Ni

If /etc/hostname is empty and the hostname is decided by network(dhcp, e.g.), there is a risk that raid device will not be in active state after boot. It will be auto-read-only state. It depends on the boot sequence. If the storage starts before network. The system detects disks first, udev rules are triggered and raid device is assemble automatically. But the network hasn't started successfully. So mdadm can't get the right hostname. The raid device will be treated as a foreign raid.
Add a note message if /etc/hostname is empty when creating a raid device.

---
  mdadm.c |  3 +++
  mdadm.h |  1 +
  util.c  | 19 +++++++++++++++++++
  3 files changed, 23 insertions(+)
mdadm.c +3 -0
index 1b3467f..e551958 100644
@@ -1408,6 +1408,9 @@ int main(int argc, char *argv[])
  	if (c.homehost == NULL && c.require_homehost)
  		c.homehost = conf_get_homehost(&c.require_homehost);
  	if (c.homehost == NULL || strcasecmp(c.homehost, "<system>") == 0) {
+		if (check_hostname())
+			pr_err("Note: The file /etc/hostname is empty. There is a risk the raid\n"
+				"      can't be active after boot\n");
  		if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
  			sys_hostname[sizeof(sys_hostname)-1] = 0;
  			c.homehost = sys_hostname;
mdadm.h +1 -0
index 399478b..3ef1209 100644
@@ -1480,6 +1480,7 @@ extern int parse_cluster_confirm_arg(char *inp, char **devname, int *slot);
  extern int check_ext2(int fd, char *name);
  extern int check_reiser(int fd, char *name);
  extern int check_raid(int fd, char *name);
+extern int check_hostname(void);
  extern int check_partitions(int fd, char *dname,
  			    unsigned long long freesize,
  			    unsigned long long size);
util.c +1 -0
index 579dd42..de5bad0 100644
@@ -694,6 +694,25 @@ int check_raid(int fd, char *name)
  	return 1;
  }
 
+/* It checks /etc/hostname has value or not */

util.c int check_raid(int fd, char *name) · line 707

Quoted 10 lines
util.c +10 -0
+int check_hostname()
+{
+	int fd, ret = 0;
+	char buf[256];
+
+	fd = open("/etc/hostname", O_RDONLY);
+	if (fd < 0) {
+		ret = 1;
+		goto out;
+	}

I don't think this is the right approach. If someone uses dhcp to obtain the hostname and explicitly configured the raid to start after the network, they shouldn't get nagged by this message.

Quoted 1 line

Any reason you cannot use gethostname(2) for this?

Hi Jes

If the raid device is used as root device and the network service needs some files that on the raid device, so the raid device need to be assemble first. The system call gethostname can't return the right hostname in this case.

But the patch has some problem. If the /etc/mdadm.conf has the raid information already, it can assemble the raid successfully without considering hostname. I'll try to send another patch.

Regards
Xiao

author
#5 lore raw

PATCH v2 2/2 Don't create bitmap for raid5 with journal disk

In reply to Xiao Ni

Journal disk and bitmap can't exist at the same time. It needs to check if the raid has a journal disk when creating bitmap.

---
 Create.c | 1 +
 1 file changed, 1 insertion(+)
Create.c +1 -0
index 6f84e5b..0efa19c 100644
@@ -542,6 +542,7 @@ int Create(struct supertype *st, char *mddev,
 	if (!s->bitmap_file &&
 	    s->level >= 1 &&
 	    st->ss->add_internal_bitmap &&
+	    s->journaldisks == 0 &&
 	    (s->consistency_policy != CONSISTENCY_POLICY_RESYNC &&
 	     s->consistency_policy != CONSISTENCY_POLICY_PPL) &&
 	    (s->write_behind || s->size > 100*1024*1024ULL)) {
Signature · 1 line
2.7.5
Applied
#6 lore raw

Re: [PATCH V2 2/2] Don't create bitmap for raid5 with journal disk

Quoted 14 lines · Xiao Ni

Journal disk and bitmap can't exist at the same time. It needs to check if the raid has a journal disk when creating bitmap.

---
 Create.c | 1 +
 1 file changed, 1 insertion(+)
Create.c +0 -0
index 6f84e5b..0efa19c 100644

Create.c int Create(struct supertype *st, char *mddev, · line 548

Quoted 7 lines
Create.c +1 -0
@@ -542,6 +542,7 @@ int Create(struct supertype *st, char *mddev,
 	if (!s->bitmap_file &&
 	    s->level >= 1 &&
 	    st->ss->add_internal_bitmap &&
+	    s->journaldisks == 0 &&
 	    (s->consistency_policy != CONSISTENCY_POLICY_RESYNC &&
 	     s->consistency_policy != CONSISTENCY_POLICY_PPL) &&
 	    (s->write_behind || s->size > 100*1024*1024ULL)) {

Applied!

That said, I'd love if we could do something to get rid of some of these very excessive if statements. It is extremely difficult to verify all cases are correct.

Thanks,
Jes