Removing a LVM Volume Group in Linux 

No Comments

What if you created a volume group in LVM and no longer need it? Well, to properly clean everything up follow the steps below.

First, let’s identify the target.  In this case, I made a temporary /u2 partition, that I now no longer need:

# df -h /u2
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup02-LogVol00
148G 88G 53G 63% /u2

Of course, we need to unmount the drive:

# umount /u2

Let’s take a look at the volume we will be removing.  Notice there is a logical volume and a corresponding physical volume:

# vgdisplay /dev/mapper/VolGroup02
— Volume group —
VG Name VolGroup02
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 150.00 GB
PE Size 4.00 MB
Total PE 38399
Alloc PE / Size 38399 / 150.00 GB
Free PE / Size 0 / 0
VG UUID 7tVffs-U3id-Nksb-Yoqk-37Qg-JptR-bJYCy7

Next, we’ll deactivate the logical volume, then remove it:

# lvchange -an /dev/mapper/VolGroup02-LogVol00
# lvremove /dev/mapper/VolGroup02-LogVol00
Logical volume “LogVol00″ successfully removed

If you look at the volume group now, you can see the logical volume has been removed.

# vgdisplay /dev/mapper/VolGroup02
— Volume group —
VG Name VolGroup02
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 150.00 GB
PE Size 4.00 MB
Total PE 38399
Alloc PE / Size 0 / 0
Free PE / Size 38399 / 150.00 GB
VG UUID 7tVffs-U3id-Nksb-Yoqk-37Qg-JptR-bJYCy7

Let’s take a look at the physical volume before changing it:

# pvdisplay /dev/sdc1
— Physical volume —
PV Name /dev/sdc1
VG Name VolGroup02
PV Size 150.00 GB / not usable 2.00 MB
Allocatable yes
PE Size (KByte) 4096
Total PE 38399
Free PE 38399
Allocated PE 0
PV UUID B9zc6s-BvKl-TkWn-YTX9-lsBY-tstd-wQZFY3

Now we will go ahead and remove the volume group from the system, which also clears the physical volume for removal:

# vgremove /dev/mapper/VolGroup02
Volume group “VolGroup02″ successfully removed

You can see that the physical volume is no longer tied to a volume group:

# pvdisplay /dev/sdc1
“/dev/sdc1″ is a new physical volume of “150.00 GB”
— NEW Physical volume —
PV Name /dev/sdc1
VG Name
PV Size 150.00 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID B9zc6s-BvKl-TkWn-YTX9-lsBY-tstd-wQZFY3

We no longer need this hard drive, so we can now remove it:

# pvremove /dev/sdc1
Labels on physical volume “/dev/sdc1″ successfully wiped

One last item of cleanup, remove the corresponding line in /etc/fstab:

# vi /etc/fstab

Sending mail from command line using Mutt 

1 Comment

I use this to attach reports (pdfs, etc) to emails in my scripts. Small Perl snippet:

system("/usr/bin/mutt -i $file_with_body_content -a $file_to_attach -s "Some Random Report $report_date" $mailto < /dev/null");

And since this has now bitten me twice, make sure that there are no variables with carriage returns at the end. In my case, both times I forgot to chomp($report_date) and could not figure out why the attachments were displaying inline instead of as proper attachments when I received the email.

Useful Networker Commands 

No Comments

For use with EMC (Legato) Networker…

Move everything off of a specific volume onto another pool (Careful, does not take into account possible clones – add /cloneid somehow to the end. I haven’t been able to automate that yet):
# for i in `/usr/sbin/mminfo -q “volume=OLDVOLUME” -r “ssid”`; do /usr/sbin/nsrstage -v -b NEWPOOL -s NETWORKERSERVERNAME -m -S $i; done

Remove everything off of a specific volume (again does not take into account clones):
# for i in `/usr/sbin/mminfo -q “volume=OLDVOLUME” -r “ssid”`; do /usr/sbin/nsrmm -e now -d -v -y -S $i; done

Clone all savesets from the last 24 hours to the “Tape” pool:
# /usr/sbin/nsrclone -S -e now -C 1 -b Tape -y month

Clone all savesets from the last 24 hours from a specific pool to the “Tape” pool and set retention to a month:
# /usr/sbin/nsrclone -S -e now -C 2 -B SOMEPOOL -b Tape -y month

Random Useful Scripts 

No Comments

Convert plain text to passwd crypt:

#!/usr/bin/perl

print "Enter the password you would like to use:  ";
$pass1 = <STDIN>;

if ($pass1) {
	my $salt =
	   join('', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
	my $cryptpw = crypt($pass1, $salt);
	print "The encrypted password suitable for passwd is $cryptpwn";
}

Convert base64 encoded to plain text:

#!/usr/bin/perl

use MIME::Base64;

print "Enter the encoded data:  ";
$code = <STDIN>;

if ($code) {
	my $decrypted = decode_base64($code);
	print "The decrypted data is:n"$decrypted"n";
}

Create 10 random passwords:

#!/usr/bin/perl

sub randomPassword {
 my $password;
 my $_rand;

 my $password_length = $_[0];
 if (!$password_length) {
  $password_length = 10;
 }

 my @chars = split(" ",
 "a b c d e f g h i j k l m n o
  A B C D E F G H I J K L M N O
  p q r s t u v w x y z 
  P Q R S T U V W X Y Z 
  0 1 2 3 4 5 6 7 8 9");

 srand;

 for (my $i=0; $i <= $password_length ;$i++) {
  $_rand = int(rand 62);
  $password .= $chars[$_rand];
 }
 return $password;
}

for ($i=0; $i < 10; $i++){
	print randomPassword(8) . "n";
}

Add hard drive to extend existing LVM volume 

3 Comments

Previously I posted how to add a hard drive and create a new volume in LVM. This time we’ll add a new hard drive and then increase or extend the size of an existing volume or partition. This is an example using an RHEL 5 derivative, OEL 5. The server is really a VM inside VMware vSphere 4, but that is of no consequence to what we are doing.

In this example, we have an existing /data partition of 350 GB. Well, it’s just not big enough, so we’ll add another 100 GB hard drive and give it to the /data partition.

After adding the 100 GB hard drive to the VM through vCenter, connect to the server through SSH or the console. Here’s the existing setup:

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
7.7G 2.2G 5.2G 30% /
/dev/sda1 99M 24M 71M 26% /boot
tmpfs 1.5G 0 1.5G 0% /dev/shm
/dev/mapper/VolGroup01-LogVol00
345G 15G 313G 5% /data

# cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
Vendor: VMware Model: Virtual disk Rev: 1.0
Type: Direct-Access ANSI SCSI revision: 02
Host: scsi0 Channel: 00 Id: 01 Lun: 00
Vendor: VMware Model: Virtual disk Rev: 1.0
Type: Direct-Access ANSI SCSI revision: 02

First we tell Linux to find the new drive in the correct location:

# echo "scsi add-single-device 0 0 2 0" > /proc/scsi/scsi

Now we see the new drive has been detected:

# cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
Vendor: VMware Model: Virtual disk Rev: 1.0
Type: Direct-Access ANSI SCSI revision: 02
Host: scsi0 Channel: 00 Id: 01 Lun: 00
Vendor: VMware Model: Virtual disk Rev: 1.0
Type: Direct-Access ANSI SCSI revision: 02
Host: scsi0 Channel: 00 Id: 02 Lun: 00
Vendor: VMware Model: Virtual disk Rev: 1.0
Type: Direct-Access ANSI SCSI revision: 02

If we check fdisk, we see the new drive is blank like we expect:

# fdisk -l

Disk /dev/sda: 12.8 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1566 12474472+ 8e Linux LVM

Disk /dev/sdb: 375.8 GB, 375809638400 bytes
255 heads, 63 sectors/track, 45689 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 45689 366996861 83 Linux

Disk /dev/sdc: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdc doesn’t contain a valid partition table

So let’s give it a partition:

# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won’t be recoverable.

The number of cylinders for this disk is set to 13054.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0×0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-13054, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-13054, default 13054):
Using default value 13054

Command (m for help): p

Disk /dev/sdc: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdc1 1 13054 104856223+ 83 Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Fdisk now confirms that everything looks good:

# fdisk -l

Disk /dev/sda: 12.8 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1566 12474472+ 8e Linux LVM

Disk /dev/sdb: 375.8 GB, 375809638400 bytes
255 heads, 63 sectors/track, 45689 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 45689 366996861 83 Linux

Disk /dev/sdc: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdc1 1 13054 104856223+ 83 Linux

Verify that LVM can see the drive:

# lvmdiskscan
…skip…
/dev/sdc1 [ 100.00 GB]
…skip…

Create a physical volume within LVM:

# pvcreate /dev/sdc1
Physical volume “/dev/sdc1″ successfully created

Add/Extend this physical volume to the volume group:

# vgextend VolGroup01 /dev/sdc1
Volume group “VolGroup01″ successfully extended

Extend the logical volume to use the full size of the volume group:

# lvm lvextend -l +100%FREE /dev/VolGroup01/LogVol00
Extending logical volume LogVol00 to 449.99 GB
Logical volume LogVol00 successfully resized

Actually resize the partition for the OS to recognize the new size:

# resize2fs -p /dev/mapper/VolGroup01-LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/mapper/VolGroup01-LogVol00 is mounted on /data; on-line resizing required
Performing an on-line resize of /dev/mapper/VolGroup01-LogVol00 to 117961728 (4k) blocks.
The filesystem on /dev/mapper/VolGroup01-LogVol00 is now 117961728 blocks long.

And the new drive space is now available to the user, who is already busy filling it:

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
7.7G 2.2G 5.2G 30% /
/dev/sda1 99M 24M 71M 26% /boot
tmpfs 1.5G 0 1.5G 0% /dev/shm
/dev/mapper/VolGroup01-LogVol00
443G 48G 373G 12% /data