So i’m studying for my re-sit of my RHCE (I got an awful exam 2 months ago, which had 3-4 questions that werent covered in the course that totally threw me – legit, apparently. I bet the other vendor accreditations like Cisco would agree (Oh, MPLS was covered in CCNA, we just dont put it in the course book..*rolls eyes*).
Part of this revision means i need to cover off some of the items i rarely cover in my day to day role, so step up to the plate Mr LVM and lets have a review.
As part of the course, we covered the option of how to create file systems, partitions, etc but the more interesting was LVM’s (PV’s, VG’s and LV’s) and specifically around the items less covered. So, without further adieu – lets go from having a disk, to having LV’s:
1. Initial setup:
[root@host]# fdisk -cul
Disk /dev/xvdj: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders, total 31457280 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x6711a043
Device Boot Start End Blocks Id System
[root@host]#
Here we have just a single disk, “xvdj”, which has a size of 16.1GB. We are going to carve this up and use it with LVM.
2. Partitioning:
[root@host]# fdisk -cu /dev/xvdj
Command (m for help): p
Disk /dev/xvdj: 16.1 GB, 16106127360 bytes 255 heads, 63 sectors/track, 1958 cylinders, total 31457280 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x6711a043
Device Boot Start End Blocks Id System
Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First sector (2048-31457279, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-31457279, default 31457279): +2G
Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First sector (4196352-31457279, default 4196352): Using default value 4196352 Last sector, +sectors or +size{K,M,G} (4196352-31457279, default 31457279): +2G
Command (m for help): p
Disk /dev/xvdj: 16.1 GB, 16106127360 bytes 255 heads, 63 sectors/track, 1958 cylinders, total 31457280 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x6711a043
Device Boot Start End Blocks Id System /dev/xvdj1 2048 4196351 2097152 83 Linux /dev/xvdj2 4196352 8390655 2097152 83 Linux
Command (m for help): w The partition table has been altered!
Calling ioctl() to re-read partition table. Syncing disks.
So now we have 2 partitions, /dev/xvdj1 and /dev/xvdj2 – that are 2GB in size each.
3. Set up the partitions for use LVM:
In this step, we need to pvcreate the 2 partitions and get them ready to be carved up with LVM:
[root@host]# pvcreate /dev/xvdj1 Physical volume "/dev/xvdj1" successfully created [root@host]# pvcreate /dev/xvdj2 Physical volume "/dev/xvdj2" successfully created [root@host]# pvs PV VG Fmt Attr PSize PFree /dev/xvdj1 lvm2 a-- 2.00g 2.00g /dev/xvdj2 lvm2 a-- 2.00g 2.00g [root@host]#
Simple as that really – we just create a PV container on there so that we can now use VG’s (Volume Groups) as below.
4. Volume Groups
Now in this stage, we can start to be a little fancy and some of this comes up in the RHCE if your unlucky – physical extents, i.e. block size for a better term. You can set the PE size to anything, but by default its set to 4M.
If we create a logical volume therefore with 100 PE’s, we’ll have a… 400MB partition (good maths there!). What they may ask, is “change the PE to 16M and create a 128M partition” or something similar. So, to do this, we need to create our volume groups with a PE size of 16MB, as below:
[root@host]# vgcreate -s 16MB VG001 /dev/xvdj1 Volume group "VG001" successfully created [root@host]# vgdisplay --- Volume group --- VG Name VG001 System ID Format lvm2 Metadata Areas 1 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 1 Act PV 1 VG Size 1.98 GiB PE Size 16.00 MiB Total PE 127 Alloc PE / Size 0 / 0 Free PE / Size 127 / 1.98 GiB VG UUID v9c5HZ-FBqq-7R65-G02e-QCGE-0Lma-9Yrbug
[root@host]#
Voila. We have 16MB PE’s, and below it, a total of 127 usable PE’s. Next, we want to add our other partition into the same VG:
[root@host]# vgextend VG001 /dev/xvdj2 Volume group "VG001" successfully extended [root@host]# vgs VG #PV #LV #SN Attr VSize VFree VG001 2 0 0 wz--n- 3.97g 3.97g [root@host]#
There we are – 4GB of storage. So now we have our 2 partitions carved up from the same disk, added to the same VG (kind of pointless really but a good exercise!), which uses 16MB per physical extent. Now we are ready to carve this baby up.
5. Logical volumes
Here we need to create our Logical volume, from our VG001 pool above, using extents (128/16=8) – and we are going to name it LV001:
[root@host]# lvcreate --extents 8 -n LV001 VG001 Logical volume "LV001" created [root@ip-10-37-161-18 ec2-user]# lvdisplay --- Logical volume --- LV Path /dev/VG001/LV001 LV Name LV001 VG Name VG001 LV UUID SgCP1g-TuWN-agcm-UdUa-21N2-59MA-CbhuO0 LV Write Access read/write LV Creation host, time host, 2013-07-08 11:09:59 -0400 LV Status available # open 0 LV Size 128.00 MiB Current LE 8 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
[root@host]
As we can see, the size is 128MB, and we have done it with just 8 LE’s (so we’ve achieved the exam objective, in my example).
6. Partitioning
Next, lets create a filesystem on this LVM and found it so we can use it.
[root@host]# mkfs.ext4 /dev/VG001/LV001 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 32768 inodes, 131072 blocks 6553 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=67371008 16 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 35 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@host]#
7. Labelling
Another item in the RHCE that may trip you up is mounting in /etc/fstab using UUID or Label (the latter tripped me up). So, to view the UUID/Label, simply run:
[root@host]# blkid /dev/xvde1: LABEL="_/" UUID="698958e2-367a-4e8c-8e03-fddeb7baa028" TYPE="ext4" /dev/xvdj1: UUID="sSb8ZK-X8jx-71Zh-cGM9-XoCo-MdQQ-TIMWvj" TYPE="LVM2_member" /dev/xvdj2: UUID="QZz9Ab-HTVB-Nhn6-IrtM-Def4-fZUP-UCd3Jh" TYPE="LVM2_member" /dev/mapper/VG001-LV001: UUID="d2b2f21b-c58d-47f2-a0b0-6f83ca2c9e69" TYPE="ext4" [root@host]#
The “UUID=” is fairly simple, however the label isnt set so this can be tricky. To set the label, we use tune2fs as below:
[root@host]# tune2fs -L "Label001" /dev/mapper/VG001-LV001 tune2fs 1.41.12 (17-May-2010) [root@host]# blkid /dev/xvde1: LABEL="_/" UUID="698958e2-367a-4e8c-8e03-fddeb7baa028" TYPE="ext4" /dev/xvdj1: UUID="sSb8ZK-X8jx-71Zh-cGM9-XoCo-MdQQ-TIMWvj" TYPE="LVM2_member" /dev/xvdj2: UUID="QZz9Ab-HTVB-Nhn6-IrtM-Def4-fZUP-UCd3Jh" TYPE="LVM2_member" /dev/mapper/VG001-LV001: UUID="d2b2f21b-c58d-47f2-a0b0-6f83ca2c9e69" TYPE="ext4" LABEL="Label001" [root@host]#
8. Mounting using UUID/Label
To mount using the label or UUID is remarkably simple once you have it. Simple set the syntax as following:
From to filesystem options bootoptions
So for UUID it would be:
UUID=12313131 /mountpoint ext4 defaults 0 1
Or for label it would be:
LABEL="Label001" /temp ext4 defaults 0 0
Then simply “mount -a” and voila, we are mounting using the label!
[root@host]# mount /dev/xvde1 on / type ext4 (rw) none on /proc type proc (rw) none on /sys type sysfs (rw) none on /dev/pts type devpts (rw,gid=5,mode=620) none on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0") none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) /dev/mapper/VG001-LV001 on /temp type ext4 (rw)
Closing thoughts
And that is a brief overview of how to go from 1 disk, to partitions, set-up via PE’s (to be difficult), extended, and then carved and mounted, using labels. Enjoy!