how to completely remove lvm from disks on linux

tested while building an automated deployment system, so the hosts were creating system partitions from scratch over and over and over… test subjects were 12 disk nodes with 4 raid 1 arrays and 2 physical disks exposed to the system from the bios raid controller.

a couple of these steps might be redundant and extraneous, but i just know it works. in fact, this method was used to clear the data disks, but left the system disks alone for the installer to contend with. The installer often failed on the system disks, but never on the data disks using this preparation script.

after all these years, i’ve finally given up on fdisk and here documents. i also had lotsa trouble from sfdisk at the beginning of this project. parted has proven to be pretty easy and reliable to script.

#! /bin/bash

# configure domU disks

# dom0 is on sd{a,b}. ignore 'em.
# disks not done by the installer:
dom1db=/dev/sdc
dom1data=/dev/sdd
dom2db=/dev/sde
dom2data=/dev/sdf
disks="$dom1db $dom1data $dom2db $dom2data"

# remove existings lvm configs
echo "clearing LVM on: $disks ..."
for _vol in `lvdisplay | awk '/LV Name.*domu/{print $3}'`
do
  umount -f $_vol >/dev/null 2>/dev/null
  lvchange -an $_vol
  lvremove -f $_vol
done

for _vol in `vgdisplay | awk '/VG Name.*domu/{print $3}'`
do
  vgchange -an $_vol
  vgremove -f $_vol
done

# last step didn't get 'em all...
for _vol in /dev/mapper/domu*
do
  dd if=/dev/zero of=$_vol bs=512 count=12
done

for _pv in `pvdisplay | awk '/PV Name.*dev.sd[c-f]/{print $3}'`
do
  pvremove -ff $_pv
done

# just in case some were missed... (some always are)
for _pv in $disks
do
  pvremove -ff $_pv
  dd if=/dev/zero of=$_pv bs=512 count=255
done

# see what else the system still knows about
# and get rid of them, too.
# note that we ignore 'sd*' partitions and that this
# assumes that we don't need dm-? for the system
# that we're running on. i know, big assumption.
#
for _part in `awk '/dm-/{print $4}' /proc/partitions`
do
  pvremove -ff $_part
  dd if=/dev/zero of=$_part bs=512 count=255
done

# okay. now we should have clean disk partitions to use.
# let's get started.
#
echo "setting up LVM on: $disks ..."
for disk in $disks
do
  parted -s $disk mklabel msdos
  pvcreate $disk
done

# =======================
#   create volume groups
# =======================
echo "Creating LVM volume groups..."
vgcreate domu1-vg00 $dom1db
vgcreate domu1-vg01 $dom1data
vgcreate domu2-vg00 $dom2db
vgcreate domu2-vg01 $dom2data

# =======================
#   activate LVM volumes
# =======================
echo "Activating all LVM volume groups..."
vgchange -a y

# =======================
#  domU1 logical volumes
# =======================
echo "Creating DomU1's LVM logical volumes..."
lvcreate -L 200G -n db domu1-vg00
lvcreate -L 4G -n root domu1-vg01
lvcreate -L 4G -n var domu1-vg01
lvcreate -L 4G -n swap domu1-vg01
lvcreate -L 4G -n tmp domu1-vg01
lvcreate -L 400G -n logs domu1-vg01

# =======================
#  domU2 logical volumes
# =======================
echo "Creating DomU2's LVM logical volumes..."
lvcreate -L 200G -n db domu2-vg00
lvcreate -L 4G -n root domu2-vg01
lvcreate -L 4G -n var domu2-vg01
lvcreate -L 4G -n swap domu2-vg01
lvcreate -L 4G -n tmp domu2-vg01
lvcreate -L 400G -n logs domu2-vg01

# =======================
#  domU1 make filesystems
# =======================
echo "creating filesystems on DomU1 LVM logical volumes..."
mkfs.ext3 /dev/domu1-vg01/logs
mkfs.ext3 /dev/domu1-vg01/root
mkfs.ext3 /dev/domu1-vg01/tmp
mkfs.ext3 /dev/domu1-vg01/var
mkswap /dev/domu1-vg01/swap

# =======================
#  domU2 make filesystems
# =======================
echo "creating filesystems on DomU2 LVM logical volumes..."
mkfs.ext3 /dev/domu2-vg01/logs
mkfs.ext3 /dev/domu2-vg01/root
mkfs.ext3 /dev/domu2-vg01/tmp
mkfs.ext3 /dev/domu2-vg01/var
mkswap /dev/domu2-vg01/swap

echo "DONE: Disks partitioned and filesystems created."