Working with LVM

Install packages

apt-get install lvm2 dmsetup mdadm reiserfsprogs xfsprogs

Create partitions

fdisk /dev/sda

-create new partition (‘n’)
-change system partition type to lvm (‘t’)
-enter ‘8e’ for hex partition type
-write partition table to disk (‘w’)

Create physical volume

root@backupserv:~# pvcreate /dev/sdb1 /dev/sda2 /dev/sdc1
Can’t open /dev/sdb1 exclusively.  Mounted filesystem?
Wiping software RAID md superblock on /dev/sda2
Physical volume “/dev/sda2” successfully created
Wiping software RAID md superblock on /dev/sdc1
Physical volume “/dev/sdc1” successfully created

root@backupserv:~# pvdisplay
“/dev/sda2” is a new physical volume of “903.57 GB”
— NEW Physical volume —
PV Name               /dev/sda2
VG Name
PV Size               903.57 GB
Allocatable           NO
PE Size (KByte)       0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               zsP1EO-SlwB-nryo-gEP7-fInc-hJc4-ytVpLX

“/dev/sdc1” is a new physical volume of “931.51 GB”
— NEW Physical volume —
PV Name               /dev/sdc1
VG Name
PV Size               931.51 GB
Allocatable           NO
PE Size (KByte)       0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               V3RZ2b-FBsM-HoI6-aQED-ouu9-LpR1-m02VLO

Create a Volume Group
root@backupserv:~# vgcreate data /dev/sdc1 /dev/sda2
Volume group “data” successfully created

root@backupserv:~# vgdisplay
— Volume group —
VG Name               data
System ID
Format                lvm2
Metadata Areas        2
Metadata Sequence No  1
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                0
Open LV               0
Max PV                0
Cur PV                2
Act PV                2
VG Size               1.79 TB
PE Size               4.00 MB
Total PE              469780
Alloc PE / Size       0 / 0
Free  PE / Size       469780 / 1.79 TB
VG UUID               XJVXrA-m5xO-GARp-6WT0-JFlX-7ARj-jxJChV

root@backupserv:~# vgscan
Reading all physical volumes.  This may take a while…
Found volume group “data” using metadata type lvm2fdisk /dev/sda

Create Logical Volume

root@backupserv:~# lvcreate –name share1 –size 100G data
Logical volume “share1” created
root@backupserv:~# lvcreate –name share2 –size 500G data
Logical volume “share2” created

*run lvscan for new logical volume scan.

Format partition

root@backupserv:~# mkfs.ext3 /dev/data/share1
mke2fs 1.41.9 (22-Aug-2009)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
6553600 inodes, 26214400 blocks
1310720 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872

Writing inode tables: 192/800

Mount device

root@backupserv:~# mkdir /mnt/share2
root@backupserv:~# mount /dev/data/share2 /mnt/share2
/dev/mapper/data-share2
516061624    202652 489644572   1% /mnt/share2

Deleting an Volume Group

root@backupserv:~# vgremove data
Do you really want to remove volume group “data” containing 2 logical volumes? [y/n]: yes
Can’t remove open logical volume “share1”
root@backupserv:~# umount /mnt/share*
root@backupserv:~# vgremove data
Do you really want to remove volume group “data” containing 2 logical volumes? [y/n]: yes
Do you really want to remove active logical volume “share1”? [y/n]: yes
Logical volume “share1” successfully removed
Do you really want to remove active logical volume “share2”? [y/n]: yes
Logical volume “share2” successfully removed
Volume group “data” successfully removed

Delete Physical Volume

root@backupserv:~# pvremove /dev/sdc1 /dev/sdb1
Labels on physical volume “/dev/sdc1” successfully wiped
Physical Volume /dev/sdb1 not found

ref: http://www.howtoforge.com/linux_lvm_p2

Adding Additional Disk to LVM

create fdisk LVM partition first….

root@backupserv:~# pvcreate /dev/sdb1
Physical volume “/dev/sdb1” successfully created

root@backupserv:~# vgextend vol1 /dev/sdb1
Volume group “vol1” successfully extended
root@backupserv:~# vgdisplay vol1
— Volume group —
VG Name               vol1
System ID
Format                lvm2
Metadata Areas        3
Metadata Sequence No  5
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                1
Open LV               1
Max PV                0
Cur PV                3
Act PV                3
VG Size               2.70 TB
PE Size               4.00 MB
Total PE              708246
Alloc PE / Size       469238 / 1.79 TB
Free  PE / Size       239008 / 933.62 GB
VG UUID               RS2K3i-SOJ8-a230-fcQL-8PX0-Bpuj-qSUYKz

root@backupserv:~# lvextend -L +930G /dev/vol1/data1 /dev/sdb1
Extending logical volume data1 to 2.70 TB
Logical volume data1 successfully resized

root@backupserv:~# resize2fs /dev/vol1/data1
resize2fs 1.41.9 (22-Aug-2009)
Filesystem at /dev/vol1/data1 is mounted on /mnt/data; on-line resizing required
old desc_blocks = 115, new_desc_blocks = 173
….

*watch more space become available with ‘watch df -h’

Adding a new physical disk to an existing logical volume

[root@twiki-web-t1 ~]# vgextend app /dev/sdb1
Volume group “app” successfully extended

[root@twiki-web-t1 ~]# lvextend -L +1G /dev/mapper/app-home /dev/sdb1
Extending logical volume home to 3.00 GB
Logical volume home successfully resized

[root@twiki-web-t1 ~]# resize2fs /dev/mapper/app-home
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/mapper/app-home is mounted on /home; on-line resizing required
Performing an on-line resize of /dev/mapper/app-home to 786432 (4k) blocks.
The filesystem on /dev/mapper/app-home is now 786432 blocks long.

[root@twiki-web-t1 ~]# df -h /home
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/app-home  3.0G  1.8G  999M  65% /home

Done!

Creating LVM partitions

apt-get install lvm2 dmsetup mdadm reiserfsprogs xfsprogs

fdisk /dev/sda
format all disk, set to 8e (lvm part table)

root@backupserv:~# pvcreate /dev/sdb1 /dev/sda2 /dev/sdc1
Can’t open /dev/sdb1 exclusively.  Mounted filesystem?
Wiping software RAID md superblock on /dev/sda2
Physical volume “/dev/sda2” successfully created
Wiping software RAID md superblock on /dev/sdc1
Physical volume “/dev/sdc1” successfully created

root@backupserv:~# pvdisplay
“/dev/sda2” is a new physical volume of “903.57 GB”
— NEW Physical volume —
PV Name               /dev/sda2
VG Name
PV Size               903.57 GB
Allocatable           NO
PE Size (KByte)       0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               zsP1EO-SlwB-nryo-gEP7-fInc-hJc4-ytVpLX

“/dev/sdc1” is a new physical volume of “931.51 GB”
— NEW Physical volume —
PV Name               /dev/sdc1
VG Name
PV Size               931.51 GB
Allocatable           NO
PE Size (KByte)       0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               V3RZ2b-FBsM-HoI6-aQED-ouu9-LpR1-m02VLO

root@backupserv:~# vgcreate data /dev/sdc1 /dev/sda2
Volume group “data” successfully created

root@backupserv:~# vgdisplay
— Volume group —
VG Name               data
System ID
Format                lvm2
Metadata Areas        2
Metadata Sequence No  1
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                0
Open LV               0
Max PV                0
Cur PV                2
Act PV                2
VG Size               1.79 TB
PE Size               4.00 MB
Total PE              469780
Alloc PE / Size       0 / 0
Free  PE / Size       469780 / 1.79 TB
VG UUID               XJVXrA-m5xO-GARp-6WT0-JFlX-7ARj-jxJChV

root@backupserv:~# vgscan
Reading all physical volumes.  This may take a while…
Found volume group “data” using metadata type lvm2

root@backupserv:~# lvcreate –name share1 –size 100G data
Logical volume “share1” created
root@backupserv:~# lvcreate –name share2 –size 500G data
Logical volume “share2” created

Format partition

root@backupserv:~# mkfs.ext3 /dev/data/share1
mke2fs 1.41.9 (22-Aug-2009)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
6553600 inodes, 26214400 blocks
1310720 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872

Writing inode tables: 192/800

Find: xargs -vs- exec

‘-exec’ using trailing ”

[root@monk ~]# tar -cvf test.tar install.log
install.log
[root@monk ~]# time find /tmp/conf-2-xp -name *.xad -exec tar –append –file=/root/test.tar {} +;
find: /tmp/conf-2-xp/9a2f6257887d6e51f58ce2/amd64: Permission denied
find: /tmp/conf-2-xp/9a2f6257887d6e51f58ce2/i386: Permission denied
find: /tmp/conf-2-xp/System Volume Information: Permission denied
tar: Removing leading `/’ from member names

real    0m15.239s
user    0m0.464s
sys     0m2.628s
[root@monk ~]# du -h test.tar
310M    test.tar
[root@monk ~]# rm -f test.tar

*delete fiel and recreate since we are appending and comparing file size after each run.

‘-exec’ using trailing ‘+’

[root@monk ~]# tar -cvf test.tar install.log
install.log
[root@monk ~]# time find /tmp/conf-2-xp -name *.xad -exec tar –append –file=/root/test.tar {} ;
find: /tmp/conf-2-xp/9a2f6257887d6e51f58ce2/amd64: Permission denied
find: /tmp/conf-2-xp/9a2f6257887d6e51f58ce2/i386: Permission denied

real    0m39.015s
user    0m2.739s
sys     0m21.893s
[root@monk ~]# du -h test.tar
310M    test.tar

Using xargs.

*use -print0 option to prevent whitespace issue. ‘xargs -0′ passes first argument.

[root@monk ~]# rm test.tar
rm: remove regular file `test.tar’? yes
[root@monk ~]# tar -cvf test.tar install.log
install.log
[root@monk ~]# time find /tmp/conf-2-xp -name ‘*.xad’ -print0 | xargs -0 tar –append –file=/root/test.tar;
find: /tmp/conf-2-xp/9a2f6257887d6e51f58ce2/amd64: Permission denied
find: /tmp/conf-2-xp/9a2f6257887d6e51f58ce2/i386: Permission denied
find: /tmp/conf-2-xp/System Volume Information: Permission denied
tar: Removing leading `/’ from member names

real    0m15.073s
user    0m0.446s
sys     0m2.774s
[root@monk ~]# du -h test.tar
310M    test.tar

Result: Using ‘+’ is two times faster than using ”. -exec and xargs are about the same speed, when using ‘+’ for -exec.