Detail: ensure --export names are acceptable as shell variables.

If an array contains a device which has a name that
contains something other than alphnumerics and underscores,
then some values reported by "mdadm --detail --export" will
not be valid as variable assignment of the shell.
This particularly affects dm devices.
e.g.
   MD_DEVICE_dm-4_ROLE=1
   MD_DEVICE_dm-4_DEV=/dev/dm-4

As it is particularly useful to be able to work with these
in a shell script, and as the precise name is not important,
change all non-alphanumerics to '_'.

   MD_DEVICE_dm_4_ROLE=1
   MD_DEVICE_dm_4_DEV=/dev/dm-4

Signed-off-by: NeilBrown <neilb@suse.com>
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
This commit is contained in:
NeilBrown 2017-04-20 12:40:06 +10:00 committed by Jes Sorensen
parent 5c4b3b9aa9
commit b9c9bd9bac
1 changed files with 9 additions and 3 deletions

View File

@ -25,6 +25,7 @@
#include "mdadm.h"
#include "md_p.h"
#include "md_u.h"
#include <ctype.h>
#include <dirent.h>
static int cmpstringp(const void *p1, const void *p2)
@ -276,17 +277,22 @@ int Detail(char *dev, struct context *c)
char *path =
map_dev(mdi->disk.major,
mdi->disk.minor, 0);
char *sysdev = xstrdup(mdi->sys_name + 1);
char *cp;
for (cp = sysdev; *cp; cp++)
if (!isalnum(*cp))
*cp = '_';
if (mdi->disk.raid_disk >= 0)
printf("MD_DEVICE_%s_ROLE=%d\n",
mdi->sys_name+4,
sysdev,
mdi->disk.raid_disk);
else
printf("MD_DEVICE_%s_ROLE=spare\n",
mdi->sys_name+4);
sysdev);
if (path)
printf("MD_DEVICE_%s_DEV=%s\n",
mdi->sys_name+4, path);
sysdev, path);
}
}
goto out;