Solved: How to label a disk in Linux

In this post we will see how to label a new disk in Linux and create a new LVM partition.

Step 1 Scan the new LUNS.

Step 2 List the available disks and identify the one you want to use.

  fdisk -l |grep /dev/ |grep -v dm

Step 3 Use fdisk to label the disk. Here we are using disk sdb .

[root@cloudvedas scsi_host]# fdisk /dev/sdb
 Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
 Be careful before using the write command.

Device does not contain a recognized partition table
 Building a new DOS disklabel with disk identifier 0x909f2c7b.

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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 label type: dos
 Disk identifier: 0x909f2c7b

Device Boot Start End Blocks Id System

Command (m for help):

Step 4 Create Primary partition using option “n” of size 200MB.

Command (m for help): n
 Partition type:
 p primary (0 primary, 0 extended, 4 free)
 e extended
 Select (default p): p
 Partition number (1-4, default 1):
 First sector (2048-2097151, default 2048):
 Using default value 2048
 Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +200M
 Partition 1 of type Linux and of size 200 MiB is set

(Don’t miss the + used before 200M. It’s very critical else it won’t work as expected)

Step 4 Now let’s check our new partition

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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 label type: dos
 Disk identifier: 0xc1c8c781

Device Boot Start End Blocks Id System
 /dev/sdb1 2048 411647 204800 83 Linux

Command (m for help):

Step 5 In the above output we can see that the new partition is a standard linux partition.  Let’s Change the partition type to Linux LVM. Here we will use “8e” which is a Hex code for LVM. You can type L to list all codes.

Command (m for help): t
 Selected partition 1
 Hex code (type L to list all codes): 8e

-Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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 label type: dos
 Disk identifier: 0xc1c8c781

Device Boot Start End Blocks Id System
 /dev/sdb1 2048 411647 204800 8e Linux LVM

Command (m for help):

Step 6 Finally save the changes with option “w”

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

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

Step 7 Let’s list the partition table to cross check that our partition table is saved correctly.

[root@cloudvedas /]# fdisk -l /dev/sdb

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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 label type: dos
 Disk identifier: 0x0aa5ab95

Device Boot Start End Blocks Id System
 /dev/sdb1 2048 411647 204800 8e Linux LVM
 [root@cloudvedas /]#

Cool! So, we have now a LVM partition which we can bring under LVM control using pvcreate. Learn more about LVM in this crash course.

LVM Crash course

In this post we will give you a crash course on Linux LVM (Logical Volume Manager). This post will cover the most used LVM commands in a Linux Admin life.

Before we start first let’s go through the concepts.

Physical Volumes (PVs): Storage devices (physical hard disks, partitions, RAID arrays etc.) provides raw storage
Volume Groups (VGs): Combine multiple PVs to create a group. They can be mirror or concatenation of PVs.
Logical Volumes (LVs): Create smaller volumes from the Volume Group(VG) as per your size requirement. You can can use the LVs to create filesystem on them.

Below commands are for RHEL 7.

All the below commands will require root access. You can either run them with sudo  or switch to root user as mentioned below.

sudo su -

Install LVM

[root@cloudvedas ~]# yum install lvm2*

Check LVM version

[root@cloudvedas ~]# lvm version

Label the new disk and bring it under LVM using pvcreate

Create PV (Physical Volume)

[root@cloudvedas ~]# pvcreate /dev/sdb1

Show PV

[root@cloudvedas ~]# pvs /dev/sdb1

Create VG (Volume Group)

[root@cloudvedas ~]# vgcreate cvsvg /dev/sdb1

Show VG

[root@cloudvedas ~]# vgs cvsvg

Display VG

[root@cloudvedas ~]# vgdisplay cvsvg

Extend VG

[root@cloudvedas ~]# vgextend cvsvg /dev/sdb2

Create LV (Logical Volume)

[root@cloudvedas ~]# lvcreate -L 500M cvsvg -n cldvdsvol1

Display LV 

[root@cloudvedas ~]# lvdisplay /dev/cvsvg/cldvdsvol1

Create mirror volume

 lvcreate -L 100M -m1 -n cvmirrorvol1 cvsvg

Create striped volume

[root@cloudvedas ~]# lvcreate -L 50M -i 3 -I 64 -n stripevol cvsvg

Here

-i|–stripes (Number of disks in stripe)
-I|Stripe size

Extend LV

lvextend -L +20M /dev/cvsvg/stripevol

De-activate volume

lvchange -an /dev/cvsvg/stripevol

Activate volume

lvchange -ay /dev/cvsvg/stripevol

Remove LV

Ensure filesystem is unmounted before running this

lvremove /dev/cvsvg/stripevol

Create filesystem

mkfs -t ext4 /dev/cvsvg/cldvdsvol1

Create directory

mkdir /CVFS1

Mount filesystem

mount /dev/cvsvg/cldvdsvol1  /CVFS1

All the available LVM commands

[root@cloudvedas ~]# lvm help
 Available lvm commands:
 Use 'lvm help <command>' for more information

devtypes Display recognised built-in block device types
 dumpconfig Dump configuration
 formats List available metadata formats
 help Display help for commands
 lvchange Change the attributes of logical volume(s)
 lvconvert Change logical volume layout
 lvcreate Create a logical volume
 lvdisplay Display information about a logical volume
 lvextend Add space to a logical volume
 lvmchange With the device mapper, this is obsolete and does nothing.
 lvmdiskscan List devices that may be used as physical volumes
 lvmsadc Collect activity data
 lvmsar Create activity report
 lvreduce Reduce the size of a logical volume
 lvremove Remove logical volume(s) from the system
 lvrename Rename a logical volume
 lvresize Resize a logical volume
 lvs Display information about logical volumes
 lvscan List all logical volumes in all volume groups
 pvchange Change attributes of physical volume(s)
 pvresize Resize physical volume(s)
 pvck Check the consistency of physical volume(s)
 pvcreate Initialize physical volume(s) for use by LVM
 pvdata Display the on-disk metadata for physical volume(s)
 pvdisplay Display various attributes of physical volume(s)
 pvmove Move extents from one physical volume to another
 pvremove Remove LVM label(s) from physical volume(s)
 pvs Display information about physical volumes
 pvscan List all physical volumes
 segtypes List available segment types
 tags List tags defined on this host
 vgcfgbackup Backup volume group configuration(s)
 vgcfgrestore Restore volume group configuration
 vgchange Change volume group attributes
 vgck Check the consistency of volume group(s)
 vgconvert Change volume group metadata format
 vgcreate Create a volume group
 vgdisplay Display volume group information
 vgexport Unregister volume group(s) from the system
 vgextend Add physical volumes to a volume group
 vgimport Register exported volume group with system
 vgmerge Merge volume groups
 vgmknodes Create the special files for volume group devices in /dev
 vgreduce Remove physical volume(s) from a volume group
 vgremove Remove volume group(s)
 vgrename Rename a volume group
 vgs Display information about volume groups
 vgscan Search for all volume groups
 vgsplit Move physical volumes into a new or existing volume group
 version Display software and driver version information
[root@cloudvedas ~]#

How to add logical volume for swap in Redhat Linux

In our last post we have seen how to add a file for swap space.

In this post we will see how to add a LVM2 Logical Volume as swap.

Here we have a VG name VG1 in which we will create a volume LV1 of 1GB.

# lvcreate VG1 -n LV1 -L 1G

Format the new swap space using mkswap:

# mkswap /dev/VG1/LV1

Update /etc/fstab file with below entry:

# /dev/VG1/LV1 swap swap defaults 0 0

Enable the extended logical volume:

# swapon -v /dev/VG1/LV1