Categories
SysOps

How to wipe partition-table signatures with one exception

Today I will extend the how to fix device excluded by a filter blog post to include information on how to wipe filesystem or partition-table signatures except for LVM2 members, which is a great idea for automation.

Initial setup

In this example I will play with the /dev/sd{b,c,d,e} disks.

$ lsblk 
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda      8:0    0  20G  0 disk 
`-sda1   8:1    0  20G  0 part /
sdb      8:16   0   2G  0 disk 
sdc      8:32   0   2G  0 disk 
sdd      8:48   0   2G  0 disk 
sde      8:64   0   2G  0 disk 
`-sde1   8:65   0   2G  0 part 

Existing data volume group using /dev/sd{b,c} physical volumes.

$ sudo vgs
  VG   #PV #LV #SN Attr   VSize VFree
  data   2   0   0 wz--n- 3.99g 3.99g
$ sudo pvs
  PV         VG   Fmt  Attr PSize  PFree 
  /dev/sdb   data lvm2 a--  <2.00g <2.00g
  /dev/sdc   data lvm2 a--  <2.00g <2.00g

Quering physical volumes

Use pvdisplay to query physical volumes.

$ sudo pvdisplay --noheadings --columns  --options pv_name | tr -d [:blank:]
/dev/sdb
/dev/sdc

Query physical volumes that belong to the specific volume group.

$ sudo pvdisplay --noheadings --select vg_name=data --columns  --options pv_name | tr -d [:blank:]
/dev/sdb
/dev/sdc

Query the specific physical volume.

$ sudo pvdisplay --noheadings --select pv_name=/dev/sdc --columns  --options pv_name | tr -d [:blank:]
/dev/sdc

Query the specific physical volume in the volume group.

$ sudo pvdisplay --noheadings --select vg_name=data,pv_name=/dev/sdb --columns  --options pv_name | tr -d [:blank:]
/dev/sdb

Query the non-existent physical volume.

$ sudo pvdisplay --noheadings --select vg_name=anydata,pv_name=/dev/any --columns  --options pv_name | tr -d [:blank:]

Notice the empty result, do not rely on the exit code.

Wipe the partition-table signatures with one exception

Inspect wipefs manual. page.

-t, --types list
Limit the set of printed or erased signatures. More than one type may be specified in a comma-separated list.
The list or individual types can be prefixed with 'no' to specify the types on which no action should be taken.

Dry-run wipefs on existing LVM2 member or empty disk/partition.

$ sudo wipefs --no-act --types noLVM2_member --all /dev/sdb

Dry-run wipefs on a disk/partition that was used earlier and have some leftover data.

$ sudo wipefs --no-act --types noLVM2_member --all /dev/sdd
/dev/sdd: 8 bytes were erased at offset 0x00000200 (gpt): 45 46 49 20 50 41 52 54
/dev/sdd: 8 bytes were erased at offset 0x7ffffe00 (gpt): 45 46 49 20 50 41 52 54
/dev/sdd: 2 bytes were erased at offset 0x000001fe (PMBR): 55 aa
/dev/sdd: calling ioctl to re-read partition table: Success

Notice, it is an LVM2 member, a simple mistake can be devastating.

Wipe the partition-table signatures when needed

Combine querying for specific physical volume with wipefs exception for LVM2 members to wipe the partition-table signatures only when it is needed, so you can later create physical volume and add it to the specific volume group.

Check on existing LVM2 member.

$ sudo pvdisplay --noheadings --select pv_name=/dev/sdc --columns  --options pv_name | tr -d [:blank:] | grep -q ^/dev/sdc$ || (sudo wipefs --no-act --types noLVM2_member --all /dev/sdc && exit 100)
/dev/sdc
$ echo $?
0

Check on earlier used disk.

$ sudo pvdisplay --noheadings --select pv_name=/dev/sde --columns  --options pv_name | tr -d [:blank:] | grep -q ^/dev/sde$ || (sudo wipefs --types noLVM2_member --all /dev/sde && exit 100)
/dev/sde: 8 bytes were erased at offset 0x00000200 (gpt): 45 46 49 20 50 41 52 54
/dev/sde: 8 bytes were erased at offset 0x7ffffe00 (gpt): 45 46 49 20 50 41 52 54
/dev/sde: 2 bytes were erased at offset 0x000001fe (PMBR): 55 aa
/dev/sde: calling ioctl to re-read partition table: Success
$ echo $?
100

Check on non-existing disk.

$ sudo pvdisplay --noheadings --select pv_name=/dev/sdz --columns  --options pv_name | tr -d [:blank:] | grep -q ^/dev/sdz$ || (sudo wipefs --no-act --types noLVM2_member --all /dev/sdz && exit 100)
wipefs: error: /dev/sdz: probing initialization failed: No such file or directory
$ echo $?
1

Exit code 0 indicates that wipefs operation was not performed.
Exit code 100 indicates that wipefs finished successfully.
Exit code 1 indicates that wipefs operation finished with an error.