Wednesday, May 16, 2012

Adding LVM from command line

This is how you setup a Physical Disk, Volume Group and a Logical Volume using the command line in Linux.

This example will be from a Redhat Enterprise 6.2 Installation.

So we will create a Physical Disk, Physical Volume, then a Volume Group, then a Logical Volume and then mount the volume.  Type only what is in "red".
 


Step #1 - Find out what device your new hard drive is assigned.

fdisk -l 

You should see all your disk including the one that you just added.  Here is the one that I just added.  

Disk /dev/sdb: 343.6 GB, 343597383680 bytes
255 heads, 63 sectors/track, 41773 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       41774   335544319+  8e  Linux LVM

Our new device is called "/dev/sdb1"

Step #2 - Partition the disk with fdisk. (Create Physical Disk)

fdisk /dev/sdb1
m - to see all the command options
n - to add the new partition
p - to make it a primary partition
1 - to make it the 1st partition
First cylinder (1-41773, default 1):  PRESS ENTER
Last cylinder, +cylinders or +size{K,M,G} (1-41773, default 41773): PRESS ENTER

***NOW WE NEED change the partition's system id
t - to change the partition's system type
L - to list all the system type
FOR LVM type 8e
8e - for LVM PRESS ENTER
w - to write table to disk and exit.

STEP #3 - Create a Physical Volume with pvcreate.

pvscan - will show you the physical volumes.
PV /dev/sda2   VG vg_redhat62   lvm2 [69.51 GiB / 0    free]
  Total: 1 [69.51 GiB] / in use: 1 [69.51 GiB] / in no VG: 0 [0   ]

As you can see our new physical disk does not show up.  It will show up after we create a physical volume.
pvcreate /dev/sdb1 - creates the physical volume.
 Writing physical volume data to disk "/dev/sdb1"
  Physical volume "/dev/sdb1" successfully created
pvscan - will now show our new physical volume
 PV /dev/sda2   VG vg_redhat62     lvm2 [69.51 GiB / 0    free]
  PV /dev/sdb1                      lvm2 [320.00 GiB]
  Total: 2 [389.51 GiB] / in use: 1 [69.51 GiB] / in no VG: 1 [320.00 GiB]

STEP #4 - Create the Volume Group
 vgcreate vg_data /dev/sdb1 - This creates the Volume Group.  give it a name and point it to your physical disk.
  Volume group "vg_data" successfully created
You can run either vgscan or vgdisplay to see your new Volume Group.

STEP #5 - Create a Logical Volume
lvcreate -l 100%FREE -n lg_data vg_data - This creates the Logical Volume.
  Logical volume "lg_data" created
The "-l 100%FREE" mean to use 100% of available space and "-n" is for naming.  So I called the new Logical Volume "lg_data" and pointed it to my new Volume Group "vg_data".

To check on the new Logical Volume run either lvscan or lvdisplay.
lvscan
  ACTIVE            '/dev/vg_data/lg_data' [320.00 GiB] inherit
  ACTIVE            '/dev/vg_redhat62/lv_root' [33.14 GiB] inherit
  ACTIVE            '/dev/vg_redhat62/lv_home' [28.67 GiB] inherit
  ACTIVE            '/dev/vg_redhat62/lv_swap' [7.70 GiB] inherit

STEP #6 - Format the new Logical Volume
 mkfs.ext4 -m 0 /dev/vg_data/lg_data - File type is ext4 the "-m 0" don't reserve disk space for superuser. It save 5%
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
20971520 inodes, 83885056 blocks
0 blocks (0.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
2560 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, 71663616, 78675968

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

STEP #7 - Mount the Logical Volume
Create a directory that you want to mount the volume to.
mkdir data
mount /dev/vg_data/lg_data /data

These steps created a physical disk, a physical volume, a Volume group, a Logical volume, formatted the volume and mounted it.
 df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_redhat62-lv_root
                       33G  2.5G   29G   9% /
tmpfs                 2.8G  272K  2.8G   1% /dev/shm
/dev/sda1             485M   53M  407M  12% /boot
/dev/mapper/vg_redhat62-lv_home
                       29G  174M   27G   1% /home
/dev/mapper/vg_data-lg_data
                      315G  195M  299G   1% /data

As you can see are new LVM is listed last and is mounted.   

No comments:

Post a Comment