some basic operations of LVM

 

1.change the partition id to LVM format(id=8e)
 
after "fdisk /dev/sdb",you can go through fdisk–>t—>8e to change the partition to linux LVM format
 
//to check the partition id information 
fdisk -l
 
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        2458    19743853+  83  Linux
/dev/sda2            2459        2610     1220940   82  Linux swap / Solaris
 
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         125     1004031    8e  Linux LVM
/dev/sdb2             126         261     1092420   8e  Linux LVM
 
Disk /dev/sdc: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1         125     1004031   8e  Linux LVM
/dev/sdc2             126         261     1092420   8e  Linux LVM
 
2.create pv
pvcreate /dev/sdb1 /dev/sdb2
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
 
//query the pv info
pvscan
PV /dev/sdb1                      lvm2 [980.50 MB]
PV /dev/sdb2                      lvm2 [1.04 GB]
 
3.create vg
[root@hundsun ~]# vgcreate testvg /dev/sdb1 /dev/sdb2
Volume group "testvg" successfully created
//query the vg information you just created
 
[root@hundsun ~]# vgscan
Reading all physical volumes.  This may take a while…
Found volume group "testvg" using metadata type lvm2
 
4.display the information
//you can see the pv information 
 
[root@hundsun ~]# pvdisplay
  "/dev/sdb1" is a new physical volume of "980.50 MB"
  — NEW Physical volume —
  PV Name               /dev/sdb1
  VG Name
  PV Size               980.50 MB
  Allocatable           NO
  PE Size (KByte)       0
  Total PE                  0
  Free PE                  0
  Allocated PE          0
  PV UUID               UuuhUL-TIJx-JT0w-1yqv-ugWx-aWaj-gExW6w
 
  "/dev/sdb2" is a new physical volume of "1.04 GB"
  — NEW Physical volume —
  PV Name               /dev/sdb2
  VG Name
  PV Size               1.04 GB
  Allocatable           NO
  PE Size (KByte)       0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               gi9xoW-qvv7-pSWP-ovcG-vjrt-NaI1-KwzyAa
 
5.display vg information
//display the information of vg
 
[root@hundsun ~]# vgdisplay
  — Volume group —
  VG Name               testvg
  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               2.00 GB
  PE Size               4.00 MB
  Total PE              511
  Alloc PE / Size       0 / 0
  Free  PE / Size       511 / 2.00 GB
  VG UUID               M8rjyz-egoA-Oyda-u8Ou-nS4l-gkrf-1Jjen6
 
6.delete a vg
[root@hundsun ~]# vgremove testvg
Volume group "testvg" successfully removed
 
7.create LV
[root@hundsun mapper]# lvcreate -L 200M -n firstLV testvg
Logical volume "firstLV" created
 
//a device will be created at /dev/mapper/{vg_name-lv_name}
[root@hundsun mapper]# ls -l /dev/mapper/testvg-firstLV
brw-rw—- 1 root disk 253, 0 Oct 15 05:09 /dev/mapper/testvg-firstLV
 
//lvdisplay will display the all the lv information
[root@hundsun testvg]# lvdisplay
  — Logical volume —
  LV Name                /dev/testvg/firstLV
  VG Name                testvg
  LV UUID                vf8a9c-jHAC-She5-yzqh-rvod-osv7-vaSYfp
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                200.00 MB
  Current LE             50
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  – currently set to     256
  Block device           253:0
 
[root@hundsun testvg]# pwd
/dev/testvg
[root@hundsun testvg]# ls -lrt
total 0
lrwxrwxrwx 1 root root 26 Oct 15 05:09 firstLV -> /dev/mapper/testvg-firstLV
 
//you can see the /dev/mapper/lv was referenced by /dev/testvg/firstLV
 
//after create lv,use mkfs.ext3 to mk a ext3 filesystem and mount it 
 
8.create a ext3 filesystem using the lv just created and mount it
 
[root@hundsun testvg]# mkfs.ext3 /dev/testvg/firstLV
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729
 
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
 
//mount it to a point
[root@hundsun /]# mount -t    ext3    /dev/testvg/firstLV /data
[root@hundsun /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              19G   12G  5.9G  67%   /
tmpfs                 507M     0  507M   0%      /dev/shm
none                  507M  104K  507M   1%    /var/lib/xenstored
/dev/mapper/testvg-firstLV        194M  5.6M  179M   4% /data
 
9.extend the lv and the mount point filesystem using lvextend
//extend the lv using lvextend,but the mount point of filesystem can not sync the change untill
//you resize2fs -p /dev/testvg/{lvname}
//before you resize2fs the filesyetem,the /data have 200M and the +100M had not take place
 
[root@hundsun /]# lvextend -L +100M /dev/testvg/firstLV
  Extending logical volume firstLV to 300.00 MB
  Logical volume firstLV successfully resized
[root@hundsun /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              19G   12G  5.8G  67% /
tmpfs                 507M     0  507M   0% /dev/shm
none                  507M  104K  507M   1% /var/lib/xenstored
/dev/mapper/testvg-firstLV
                      194M  5.6M  179M   4% /data
 
//resize the filesystem to call the change of lv
[root@hundsun /]# resize2fs -p /dev/testvg/firstLV
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/testvg/firstLV is mounted on /data; on-line resizing required
Performing an on-line resize of /dev/testvg/firstLV to 307200 (1k) blocks.
The filesystem on /dev/testvg/firstLV is now 307200 blocks long.
 
[root@hundsun /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              19G   12G  5.8G  67% /
tmpfs                 507M     0  507M   0% /dev/shm
none                  507M  104K  507M   1% /var/lib/xenstored
/dev/mapper/testvg-firstLV
                      291M  6.1M  270M   3% /data
 
–EOF–