pvcreate & detection of foreign signatures
cover pvcreate & detection of foreign signatures
After the third report of broken LUKS after pvcreate (just this week...) I added LUKS detection to lvm. But the current code is messy and in fact it wipes superblocks without asking by default.
This is attempt to change that to something more user friendly.
Milan Broz (4):
Change the pvcreate swap/md logic
Remove assumption that --ye must be used only in --force mode Fix file open leak in swap signature detection Detect LUKS signature in pvcreate
lib/Makefile.in | 1 + lib/device/dev-luks.c | 43 ++++++++++++++++++++++++++++ lib/device/dev-swap.c | 12 +++---- lib/device/device.h | 1 + lib/metadata/metadata.c | 71 +++++++++++++++++++++++++---------------------- tools/pvremove.c | 5 --- tools/toollib.c | 5 --- 7 files changed, 88 insertions(+), 50 deletions(-) create mode 100644 lib/device/dev-luks.c
PATCH 1/4 Change the pvcreate swap/md logic
pvcreate detects MD and swap signature.
The logic hidden there is not only documented but it is also user unfriendly. Who invented this logic should run pvcreate on its own critical MD device to see why;-)
This patch
- create one function instead of duplication code
- asks if user want to overwrite signature
- allow aborting (!)
(Please note that writing LVM signarute without wiping old is wrong, it confuses blkid, MD will not work anyway and swap and LUKS is broken too.)
Question is ignore if --yes is given or --force switch is used. (See later patch also.)
--- lib/metadata/metadata.c | 70 ++++++++++++++++++++++++----------------------- 1 files changed, 36 insertions(+), 34 deletions(-)
lib/metadata/metadata.c +36 -34
@@ -1526,6 +1526,40 @@ int vg_split_mdas(struct cmd_context *cmd __attribute__((unused)),return 1 ; } +static int _wipe_sb (struct device *dev,const char *type,const char *name, +int wipe_len,struct pvcreate_params *pp, +int (*func)(struct device *dev,uint64_t *signature)) +{ +int wipe; +uint64_t superblock; + + wipe =func (dev, &superblock); +if (wipe ==-1 ) { +log_error ("Fatal error while trying to detect %s on %s." , + type, name); +return 0 ; + } + +if (wipe ==0 ) +return 1 ; + +/* Specifying --yes => do not ask. */ +if (!pp->yes && (pp->force ==PROMPT ) && +yes_no_prompt ("WARNING: %s detected on %s. Wipe it? [y/n] " , + type, name) !='y' ) { +log_error ("Aborting pvcreate on %s." , name); +return 0 ; + } + +log_print ("Wiping %s on %s." , type, name); +if (!dev_set (dev, superblock, wipe_len,0 )) { +log_error ("Failed to wipe %s on %s." , type, name); +return 0 ; + } + +return 1 ; +} +/* * See if we may pvcreate on this device. * 0 indicates we may not.
@@ -1535,8 +1569,6 @@ static int pvcreate_check(struct cmd_context *cmd, const char *name, {struct physical_volume *pv;struct device *dev; -uint64_t md_superblock, swap_signature; -int wipe_md, wipe_swap;struct dm_list mdas;dm_list_init (&mdas);
@@ -1602,41 +1634,11 @@ static int pvcreate_check(struct cmd_context *cmd, const char *name,return 0 ; } -/* Wipe superblock? */ -if ((wipe_md =dev_is_md (dev, &md_superblock)) ==1 && - ((!pp->idp && !pp->restorefile) || pp->yes || - (yes_no_prompt ("Software RAID md superblock " -"detected on %s. Wipe it? [y/n] " , name) =='y' ))) { -log_print ("Wiping software RAID md superblock on %s" , name); -if (!dev_set (dev, md_superblock,4 ,0 )) { -log_error ("Failed to wipe RAID md superblock on %s" , - name); -return 0 ; - } - } - -if (wipe_md ==-1 ) { -log_error ("Fatal error while trying to detect software " -"RAID md superblock on %s" , name); +if (!_wipe_sb (dev,"software RAID md superblock" , name,4 , pp, dev_is_md))return 0 ; - } - -if ((wipe_swap =dev_is_swap (dev, &swap_signature)) ==1 && - ((!pp->idp && !pp->restorefile) || pp->yes || - (yes_no_prompt ("Swap signature detected on %s. Wipe it? [y/n] " , - name) =='y' ))) { -log_print ("Wiping swap signature on %s" , name); -if (!dev_set (dev, swap_signature,10 ,0 )) { -log_error ("Failed to wipe swap signature on %s" , name); -return 0 ; - } - } -if (wipe_swap ==-1 ) { -log_error ("Fatal error while trying to detect swap " -"signature on %s" , name); +if (!_wipe_sb (dev,"swap signature" , name,10 , pp, dev_is_swap))return 0 ; - }if (sigint_caught ())return 0 ;
Signature · 1 line
1.7.1
PATCH 2/4 Remove assumption that --ye must be used only in --force mode
In reply to Milan BrozThis is not only undocumented but is is also in violation with --help documentation.
Using --yes without --force is useful in pvcreate when it detects old signature.
--- tools/pvremove.c | 5 ----- tools/toollib.c | 5 ----- 2 files changed, 0 insertions(+), 10 deletions(-)
tools/pvremove.c +0 -5
@@ -143,11 +143,6 @@ int pvremove(struct cmd_context *cmd, int argc, char **argv)return EINVALID_CMD_LINE ; } -if (arg_count (cmd, yes_ARG) && !arg_count (cmd, force_ARG)) { -log_error ("Option y can only be given with option f" ); -return EINVALID_CMD_LINE ; - } -for (i =0 ; i < argc; i++) { r =pvremove_single (cmd, argv[i],NULL );if (r > ret)
tools/toollib.c +0 -5
@@ -1344,11 +1344,6 @@ int pvcreate_params_validate(struct cmd_context *cmd,return 0 ; } -if (arg_count (cmd, yes_ARG) && !arg_count (cmd, force_ARG)) { -log_error ("Option y can only be given with option f" ); -return 0 ; - } - pp->yes =arg_count (cmd, yes_ARG); pp->force =arg_count (cmd, force_ARG);
Signature · 1 line
1.7.1
PATCH 3/4 Fix file open leak in swap signature detection
In reply to Milan Broz--- lib/device/dev-swap.c | 12 +++++------- 1 files changed, 5 insertions(+), 7 deletions(-)
lib/device/dev-swap.c +5 -7
@@ -42,7 +42,7 @@ int dev_is_swap(struct device *dev, uint64_t *signature) {char buf[10 ];uint64_t size; -int page; +int page, ret =0 ;if (!dev_get_size (dev, &size)) { stack;
@@ -66,11 +66,12 @@ int dev_is_swap(struct device *dev, uint64_t *signature)break ;if (!dev_read (dev, page -SIGNATURE_SIZE ,SIGNATURE_SIZE , buf)) { - stack; -return -1 ; + ret =-1 ; +break ; }if (_swap_detect_signature (buf)) { *signature = page -SIGNATURE_SIZE ; + ret =1 ;break ; } }
@@ -78,10 +79,7 @@ int dev_is_swap(struct device *dev, uint64_t *signature)if (!dev_close (dev)) stack; -if (*signature) -return 1 ; - - return 0; + return ret; }#endif
Signature · 1 line
1.7.1
PATCH 4/4 Detect LUKS signature in pvcreate
In reply to Milan BrozOne shiny day we should use libblkid here. But now using LUKS is very common together with LVM and pvcreate destroys LUKS completely.
So for user's convenience, try to detect LUKS signature and allow abort.
--- lib/Makefile.in | 1 + lib/device/dev-luks.c | 43 +++++++++++++++++++++++++++++++++++++++++++ lib/device/device.h | 1 + lib/metadata/metadata.c | 3 +++ 4 files changed, 48 insertions(+), 0 deletions(-) create mode 100644 lib/device/dev-luks.c
lib/Makefile.in +1 -0
@@ -47,6 +47,7 @@ SOURCES =\ device/dev-io.c \ device/dev-md.c \ device/dev-swap.c \ + device/dev-luks.c \ device/device.c \ display/display.c \ error/errseg.c \
lib/device/dev-luks.c new file +43 -0
43 lines not on this page · raw message
lib/device/device.h +1 -0
@@ -96,6 +96,7 @@ const char *dev_name_confirmed(struct device *dev, int quiet);/* Does device contain md superblock? If so, where? */ int dev_is_md (struct device *dev,uint64_t *sb);int dev_is_swap (struct device *dev,uint64_t *signature); +int dev_is_luks (struct device *dev,uint64_t *signature);unsigned long dev_md_stripe_width (const char *sysfs_dir,struct device *dev);int is_partitioned_dev (struct device *dev);
lib/metadata/metadata.c +3 -0
@@ -1640,6 +1640,9 @@ static int pvcreate_check(struct cmd_context *cmd, const char *name,if (!_wipe_sb (dev,"swap signature" , name,10 , pp, dev_is_swap))return 0 ; +if (!_wipe_sb (dev,"LUKS signature" , name,8 , pp, dev_is_luks)) +return 0 ; +if (sigint_caught ())return 0 ;
Signature · 1 line
1.7.1
cover pvcreate & detection of foreign signatures
In reply to Milan BrozHi,
Quoted 5 lines · Milan Broz
After the third report of broken LUKS after pvcreate (just this week...) I added LUKS detection to lvm. But the current code is messy and in fact it wipes superblocks without asking by default.
This is attempt to change that to something more user friendly.
Looks OK to me. I am trusting you on the LUKS detection bit, the rest
Quoted 15 lines
Milan Broz (4):
Change the pvcreate swap/md logic
Remove assumption that --ye must be used only in --force mode Fix file open leak in swap signature detection Detect LUKS signature in pvcreatelib/Makefile.in | 1 + lib/device/dev-luks.c | 43 ++++++++++++++++++++++++++++ lib/device/dev-swap.c | 12 +++---- lib/device/device.h | 1 + lib/metadata/metadata.c | 71 +++++++++++++++++++++++++---------------------- tools/pvremove.c | 5 --- tools/toollib.c | 5 --- 7 files changed, 88 insertions(+), 50 deletions(-) create mode 100644 lib/device/dev-luks.c