Linux Volume Management

The Linux Logical Volume Manager (LVM) is a mechanism for virtualizing disks. It allows you to abstract the physical layout of component storage devices for easier and flexible administration. It can create "virtual" disk partitions out of one or more physical hard drives, allowing you to grow, shrink, or move partitions from drive to drive as your requirements change. It also allows you to create larger partitions than you could achieve with a single drive.

Topics




LVM Basics

The main advantages of LVM are increased abstraction, flexibility, and control. Logical volumes can have meaningful names. Volumes can be resized dynamically as space requirements change and migrated between physical devices within the pool on a running system or exported easily. LVM also offers advanced features like snapshotting, striping, and mirroring.

To use LVM, you must understand several elements. First are the regular physical hard drives attached to the computer. The disk space on these devices is chopped up into partitions. Finally, a filesystem is written directly to a partition. By comparison, in LVM, Volume Groups (VGs) are split up into logical volumes (LVs), where the filesystems ultimately reside

Each Volume Group (VG) is made up of a pool of Physical Volumes (PVs). You can extend (or reduce) the size of a Volume Group by adding or removing as many PVs as you wish, provided there are enough PVs remaining to store the contents of all the allocated LVs. As long as there is available space in the VG, you can also grow and shrink the size of your LVs at will (although most filesystems don't like to shrink).

Physical Volumes:

Physical block devices or other disk-like devices (for example, other devices created by device mapper, like RAID arrays) are used by LVM as the raw building material for higher levels of abstraction. Physical volumes are regular storage devices. LVM writes a header to the device to allocate it for management.

Volume Groups

LVM combines physical volumes into storage pools known as volume groups. Volume groups abstract the characteristics of the underlying devices and function as a unified logical device with combined storage capacity of the component physical volumes.

Logical Volumes

A volume group can be sliced up into any number of logical volumes. Logical volumes are functionally equivalent to partitions on a physical disk, but with much more flexibility. Logical volumes are the primary component that users and applications will interact with.




Recipes

A. Creating new disk partion, physical volume, logical group and logical volume

Use fdisk command with list (-l) switch to list existing disks. fdisk is a menu-driven program for creation and manipulation of partition tables on a hard disk. Hard disks are divided into partitions and this division is described in the partition table. Using fdisk you can create a new partition, delete an existing partition, or change existing partition. Using fidsk you are allowed to create a maximum of four primary partition, and any number of logical partitions, based on the size of the disk.

> fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x000dab3d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    20971519     9436160   8e  Linux LVM

Disk /dev/mapper/cl-root: 8585 MB, 8585740288 bytes, 16769024 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 /dev/mapper/cl-swap: 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


This can be done by physically adding a new disk. If you are using a virtual machine, add a new virtual disk. You should be able to do this without shutting down system.

Reboot to get Linux to recognize new disk and use fdisk to make sure it works.

> reboot

Note that we have a new 5G disk called /dev/sdb with no partitions table listed at the botton section of the outpt.

> fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x000dab3d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    20971519     9436160   8e  Linux LVM

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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 /dev/mapper/cl-root: 8585 MB, 8585740288 bytes, 16769024 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 /dev/mapper/cl-swap: 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

As an alternative to rebooting, you can use this command

ls /sys/class/scsi_host/ | while read host ; do echo "- - -" > /sys/class/scsi_host/$host/scan ; done

fdisk is a menu-driven program for creation and manipulation of partition tables on a hard disk. Hard disks are divided into partitions and this division is described in the partition table. Using fdisk you can create a new partition, delete an existing partition, or change existing partition. Using fidsk you are allowed to create a maximum of four primary partition, and any number of logical partitions, based on the size of the disk.

Run fdisk for disk /dev/sdb

> 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.

Command (m for help): ^C

Use m to show menu

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help):

Create a new 40M partition and write (w) changes to disk.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): +40M
Partition 1 of type Linux and of size 40 MiB is set
Command (m for help): w
The partition table has been altered!

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

Now if you do an fdisk again, you will see new partition called /dev/sdb1

fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x000dab3d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    20971519     9436160   8e  Linux LVM

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: 0xfd11b535


   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       83967       40960   83  Linux

Disk /dev/mapper/cl-root: 8585 MB, 8585740288 bytes, 16769024 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 /dev/mapper/cl-swap: 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

Next we want to change partition type to type Linux LVM.

> fdisk /dev/sdb
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: 5368 MB, 5368709120 bytes, 10485760 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: 0x5d93b896

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   8e  Linux LVM

5) Create a Physical Volume with command pvcreate
        pvcreate /dev/sdb1
Physical Volume

Physical volume is the actual storage device that will be used in the LVM configuration. It can be an entire disk, a partition on disk or a LUN on the SAN. You can use pvcreate to create the physical volume. To use the device for an LVM logical volume the device must be initialized as a physical volume (PV). The LVM label identifies the device as an LVM physical volume. It contains a random unique identifier (the UUID) for the physical volume. The pvcreate command initializes Physical Volume for later use by the Logical Volume Manager (LVM). Each Physical Volume can use a disk partition, or the whole disk.

Syntax

pvcreate [physical volume name]

where physicalvolumename is name of your disk or partition. The fdisk -l command will provide you with this name.

Create Physical Volume where name is a partition created with fdisk or disk name

> pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
 

use pvs to display all physical volumes. Note that Physical Volume (PV) /dev/sdb1 has no associated Volume Group (VG)

> pvs
  PV         VG Fmt  Attr PSize  PFree
  /dev/sda2  cl lvm2 a--  <9.00g     0
  /dev/sdb1     lvm2 ---  40.00m 40.00m

Use pvdisplay to display information about a specific physical volume or all physical volumes

> pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               cl
  PV Size               <9.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              2303
  Free PE               0
  Allocated PE          2303
  PV UUID               m6sySf-hYek-Pcpn-6ST4-AvRc-OXl1-brv53d

  "/dev/sdb1" is a new physical volume of "40.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name
  PV Size               40.00 MiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               hVMo7W-zuOj-oUql-lCJi-Me1b-7HZW-CzN5kG

6) Create a Volume Group using a Physical Volumes
        vgcreate myVolumeGroup /dev/sdb1
Volume Groups (VG)

Logical volume management is a technique for deploying logical rather than physical storage. With LVM, "logical" partitions can span across physical hard drives and can be resized (unlike traditional ext3 "raw" partitions). A physical disk is divided into one or more physical volumes (PVs), and logical volume groups (VGs) are created by combining PVs.

Physical volumes are combined into volume groups (VGs). This creates a pool of disk space out of which logical volumes can be allocated. Within a volume group, the disk space available for allocation is divided into units of a fixed-size called extents. An extent is the smallest unit of space that can be allocated, Within a physical volume, extents are referred to as physical extents. A logical volume is allocated into logical extents of the same size as the physical extents. The extent size is thus the same for all logical volumes in the volume group. The volume group maps the logical extents to physical extents.

syntax:

vgcreate [name] [list of physical volumes]

example:

 > vgcreate myVolumeGroup /dev/sdb1
  Volume group "myVolumeGroup" successfully created

If you want volume group to contain multiple partitions:

 > vgcreate myVolumeGroup /dev/sdb1  /dev/sdb2  /dev/sdb3

To display all volume groups:

>  vgs
  VG            #PV #LV #SN Attr   VSize  VFree
  cl              1   2   0 wz--n- <9.00g     0
  myVolumeGroup   1   0   0 wz--n- 36.00m 36.00m

display contents of volume group or all volume groups.

 > vgdisplay
  --- Volume group ---
  VG Name               myVolumeGroup
  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               36.00 MiB
  PE Size               4.00 MiB
  Total PE              9
  Alloc PE / Size       0 / 0
  Free  PE / Size       9 / 36.00 MiB
  VG UUID               VNmBiJ-39lb-c941-YIKU-1b0n-Gtz3-fuLq2A

  --- Volume group ---
  VG Name               cl
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <9.00 GiB
  PE Size               4.00 MiB
  Total PE              2303
  Alloc PE / Size       2303 / <9.00 GiB
  Free  PE / Size       0 / 0
  VG UUID               oHLLvo-RdiR-PtyA-oHa8-kyjw-0KdN-kKdyZ5

7) Create a Logical Volume using part of Volume Group
        lvcreate -L 10M -n myLogicalVolume myVolumeGroup
Logical Volumes

A logical volume provides storage virtualization. With a logical volume, you are not restricted to physical disk sizes. In addition, the hardware storage configuration is hidden from the software so it can be resized and moved without stopping applications or unmounting file systems.

syntax

lvcreate -n [logical volume name] - L [size] [volume group name]

where
n = name
L = size

Create a logical volume called myLogicalVolume using 12M of volme group myVolumeGroup

lvcreate -L 10M -n myLogicalVolume myVolumeGroup  
  Logical volume "myLogicalVolume" created.
 
> lvdisplay
  --- Logical volume ---
  LV Path                /dev/myVolumeGroup/myLogicalVolume
  LV Name                myLogicalVolume
  VG Name                myVolumeGroup
  LV UUID                fcuP8G-hpw7-yEiE-eUzD-zBt2-XigK-umy2MJ
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2017-12-08 22:57:43 -0500
  LV Status              available
  # open                 0
  LV Size                12.00 MiB
  Current LE             3
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

  --- Logical volume ---
  LV Path                /dev/cl/swap
  LV Name                swap
  VG Name                cl
  LV UUID                VvlQgm-o9Qg-vTNA-mWyl-zdOP-umwH-Adr4xm
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2017-12-08 15:56:57 -0500
  LV Status              available
  # open                 2
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/cl/root
  LV Name                root
  VG Name                cl
  LV UUID                zfVhd6-J1aw-9kgB-5ifE-AwH9-y8z4-gZashh
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2017-12-08 15:56:58 -0500
  LV Status              available
  # open                 1
  LV Size                <8.00 GiB
  Current LE             2047
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

Extending a volume group: extend volume group to 12Gig

lvextend -L12G /dev/myvg/lvol0

once logical volume is created, we can do fdisk (fdisk /dev/myVolumeGroup/lvol0) and make a file system

To look for existing logical drives:

lvmdiskscan scans all SCSI, (E)IDE disks, multiple, loop and network block devices in the system and lists them

# lvmdiskscan

/dev/sda1 [      29.30 GiB]
/dev/sda2 [       3.91 GiB]
/dev/sda3 [      46.80 GiB]
0 disks
3 partitions
0 LVM physical volume whole disks
0 LVM physical volumes
lvchange lvextend lvmconfig lvmpolld lvremove lvscan lvconvert lvm lvmdiskscan lvmsadc lvrename lvcreate lvmchange lvmdump lvmsar lvresize lvdisplay lvmconf lvmetad lvreduce lvs


making directory to mount logical drive
mkdir /mnt/myMountPoint
formatting Logical Volumes using mkfs ("Make file system")

mkfs is a front-end program for the various file system builders (mkfs.fstype) available under Linux. It is used to build a Linux file system on a device, usually a hard disk partition. filesys is either the device name (e.g. /dev/hda1, /dev/sdb2), or a regular file that shall contain the file system. blocks is the number of blocks to be used for the file system. mkfs can be used to create several types of file systems including:

  • mkfs - Some Linux Filesystems
    mkfs.ext3 - Linux filesystem. Enancemenmt of mkfs.ext and mkfs.ext2
    mkfs.ext4 - Linux filesystem. Enancemenmt of mkf3.ext with built-in journaling
    mkfs.xfs - XFS filesystem
    mkfs.msdos - DOS filesystem
    mkfs.reiserfs - includes build in jopurnaling system

    XFS is a highly scalable, high-performance file system. The mkfs.xfs command constructs an XFS filesystem by writing on a special file using the values found in the arguments of the command line.

    syntax:

    mkfs.[file system type] [logical volume]
    

    example:

    mkfs.ext4 /dev/sda5
    

8) Formatting Logical Volumes using mkfs ("Make file system")
        mkfs.ext4 /dev/myVolumeGroup/myLogicalVolume
formatting Logical Volumes using mkfs ("Make file system")

mkfs is simply a front-end for the various file system builders (mkfs.fstype) available under Linux. It is used to build a Linux file system on a device, usually a hard disk partition. filesys is either the device name (e.g. /dev/hda1, /dev/sdb2), or a regular file that shall contain the file system. blocks is the number of blocks to be used for the file system. mkfs can be used to create several types of file systems including:

  • mkfs - Linux filesystem
  • mkfs.ext3 - Linux filesystem. Enancemenmt of mkfs.ext and mkfs.ext2
  • mkfs.ext4 - Linux filesystem. Enancemenmt of mkf3.ext with built-in journaling
  • mkfs.xfs - XFS filesystem
  • mkfs.msdos - DOS filesystem
  • mkfs.reiserfs - includes build in jopurnaling system

XFS is a highly scalable, high-performance file system. The mkfs.xfs command constructs an XFS filesystem by writing on a special file using the values found in the arguments of the command line.

syntax:

mkfs.xfs [logical volume]

example:

 > mkfs.ext4 /dev/myVolumeGroup/myLogicalVolume
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
3072 inodes, 12288 blocks
614 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=12582912
2 block groups
8192 blocks per group, 8192 fragments per group
1536 inodes per group
Superblock backups stored on blocks:
        8193

Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

> lvdisplay
  --- Logical volume ---
  LV Path                /dev/myVolumeGroup/myLogicalVolume
  LV Name                myLogicalVolume
  VG Name                myVolumeGroup
  LV UUID                Kz7sOf-e3L6-YrjD-KVGq-Unin-4ohQ-R69Pcu
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2017-03-10 12:43:34 -0800
  LV Status              available
  # open                 0
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0



Mounting Volumes

used to "mount" a file directory of a device or partition to a directory

examples:

Mount myLogicalVolume to directory /mnt/myMountPoint

/dev/mapper/myVolumeGroup-myLogicalVolume /mnt/myMountPoint ext4 defaults 0 0 

to see if it works

df -h
/dev/mapper/cl-root                             17G  1.3G   16G   8% /
devtmpfs                                       478M     0  478M   0% /dev
tmpfs                                          489M     0  489M   0% /dev/shm
tmpfs                                          489M  6.8M  482M   2% /run
tmpfs                                          489M     0  489M   0% /sys/fs/cgroup
tmpfs                                          489M  8.0K  489M   1% /tmp
/dev/sda1                                     1014M  168M  847M  17% /boot
/dev/mapper/myVolumeGroup-myLogicalVolume       11M  204K  9.6M   3% /mnt/myMountPoint
tmpfs                                           98M     0   98M   0% /run/user/0

9) Make directory to mount file system and mount file system using mount command
        mkdir /mnt/myMountPoint
        mount /dev/myVolumeGroup/myLogicalVolume /mnt/myMountPoint

9.1 Create a directory on which to mount logical drive
mkdir /mnt/myMountPoint
9.2 Mount Volume

used to "mount" a file directory of a device or partition to a directory

examples:

Mount /dev/sda1 to directory /mnt/myMountPoint

  mount /dev/mapper/myVolumeGroup-myLogicalVolume  /mnt/myMountPoint          

to see if it worked

 >  df -h
Filesystem                                 Size  Used Avail Use% Mounted on
/dev/mapper/cl-root                        8.0G  1.2G  6.9G  15% /
devtmpfs                                   478M     0  478M   0% /dev
tmpfs                                      489M     0  489M   0% /dev/shm
tmpfs                                      489M  6.8M  482M   2% /run
tmpfs                                      489M     0  489M   0% /sys/fs/cgroup
/dev/sda1                                 1014M  168M  847M  17% /boot
tmpfs                                       98M     0   98M   0% /run/user/0
/dev/mapper/myVolumeGroup-myLogicalVolume   11M  204K  9.6M   3% /mnt/myMountPoint

10) Add an entry to /etc/fstab file to make it permanent.

       /dev/mapper/myVolumeGroup-myLogicalVolume /mnt/myMountPoint defaults ext4 0 0


Add an entry to /etc/fstab file to make it permanent.

/dev/mapper/myVolumeGroup-myLogicalVolume /mnt/myMountPoint defaults  ext4   0  0 

Make sure to get syntax correctly or system may fail on reboot!


To test syntax before rebooting

mount -a

example of fstab entry
/etc/fstab

# <device>             <dir>         <type>    <options>             <dump> <fsck>
/dev/sda1              /             ext4      defaults,noatime      0      1
/dev/sda2              none          swap      defaults              0      0
/dev/sda3              /home         ext4      defaults,noatime      0      2

Where:

  • device - describes the block special device or remote filesystem to be mounted.
  • dir describes the mount directory,
  • type the file system type,
  • options the associated mount options,
  • dump is checked by the dump utility. This field is usually set to 0, which disables the check,
  • fsck - sets the order for filesystem checks at boot time. For the root device it should be 1, For other partitions it should be 2, or 0 to disable checking,

Introduction to fstab
The configuration file /etc/fstab contains the necessary information to automate the process of mounting partitions. Partitions listed in fstab can be configured to automatically mount during the boot process. The options for mount and fstab are similar.

The syntax of a fstab entry is :

[Device] [Mount Point] [File System Type] [Options] [Dump] [Pass]

fields

description

<device>

The device/partition (by /dev location or UUID) that contain a file system.
example: /dev/myVolumeGroup/myLogicalVolume

<mount point>

The directory on your root file system (aka mount point) from which it will be possible to access the content of the device/partition (note: swap has no mount point). A mount point is a location on your directory tree to mount the partition. In Centos/Red Hat, the default location is the /mnt directory. You may use any name you wish for the mount point, but you must create the mount point before you mount the partition. Mount points should not have spaces in the names.

example: /dev/myVolumeGroup/myLogicalVolume

<file system type>

Type of file system. Common types include: ext2, ext3, ext4, reiserFS, XFS

<options>

Use defaults in most cases. See documenation for special cases.

example: defaults

<dump>

Enable or disable backing up of the device/partition (the command dump). This field is usually set to 0, which disables it.

<pass num>

Controls the order in which fsck checks the device/partition for errors at boot time. The root device should be 1. Other partitions should be 2, or 0 to disable checking.

Fsck order is to tell fsck what order to check the file systems, if set to "0" file system is ignored.

Often a source of confusion, there are only 3 options :

  • 0 == do not check.
  • 1 == check this partition first.
  • 2 == check this partition(s) next

Please refer to the examples section for sample entries. We have provided you some detailed explanations of each field:

Device

By default, Ubuntu now uses UUID to identify partitions.

UUID=xxx.yyy.zzz

To list your devices by UUID use blkid

sudo blkid

Alternative ways to refer to partitions:

  • Label : LABEL=label
  • Network ID
    • Samba : //server/share
    • NFS : server:/share
    • SSHFS : sshfs#user@server:/share
  • Device : /dev/sdxy (not recommended)

Mount point

A mount point is a location on your directory tree to mount the partition. The default location is /media although you may use alternate locations such as /mnt or your home directory.

You may use any name you wish for the mount point, but you must create the mount point before you mount the partition.

For example : /media/windows

sudo mkdir /media/windows

File System Type

You may either use auto or specify a file system. Auto will attempt to automatically detect the file system of the target file system and in general works well. In general auto is used for removable devices and a specific file system or network protocol for network shares.

Examples:

  • auto
  • vfat - used for FAT partitions.
  • ntfs, ntfs-3g - used for ntfs partitions.
  • ext4, ext3, ext2, jfs, reiserfs, etc.
  • udf,iso9660 - for CD/DVD.
  • swap.

Options

Options are dependent on the file system.

You may use "defaults" here and some typical options may include :

  • Ubuntu 8.04 and later uses relatime as default for linux native file systems. You can find a discussion of relatime here : http://lwn.net/Articles/244829. This relates to when and how often the last access time of the current version of a file is updated, i.e. when it was last read.

  • defaults = rw, suid, dev, exec, auto, nouser, and async.
  • ntfs/vfat = permissions are set at the time of mounting the partition with umask, dmask, and fmask and can not be changed with commands such as chown or chmod.
    • I advise dmask=027,fmask=137 (using umask=000 will cause all your files to be executable). More permissive options would be dmask=000,fmask=111.

  • For mounting samba shares you can specify a username and password, or better a credentials file. The credentials file contains should be owned by root.root with permissions = 0400 .

Common options :

  • sync/async - All I/O to the file system should be done (a)synchronously.
  • auto - The filesystem can be mounted automatically (at bootup, or when mount is passed the -a option). This is really unnecessary as this is the default action of mount -a anyway.
  • noauto - The filesystem will NOT be automatically mounted at startup, or when mount passed -a. You must explicitly mount the filesystem.
  • dev/nodev - Interpret/Do not interpret character or block special devices on the file system.
  • exec / noexec - Permit/Prevent the execution of binaries from the filesystem.
  • suid/nosuid - Permit/Block the operation of suid, and sgid bits.
  • ro - Mount read-only.
  • rw - Mount read-write.
  • user - Permit any user to mount the filesystem. This automatically implies noexec, nosuid,nodev unless overridden.
  • nouser - Only permit root to mount the filesystem. This is also a default setting.
  • defaults - Use default settings. Equivalent to rw, suid, dev, exec, auto, nouser, async.
  • _netdev - this is a network device, mount it after bringing up the network. Only valid with fstype nfs.

For specific options with specific file systems see:

Dump

This field sets whether the backup utility dump will backup file system. If set to "0" file system ignored, "1" file system is backed up.

Dump is seldom used and if in doubt use 0.

Pass (fsck order)

Fsck order is to tell fsck what order to check the file systems, if set to "0" file system is ignored.

Often a source of confusion, there are only 3 options :

  • 0 == do not check.
  • 1 == check this partition first.
  • 2 == check this partition(s) next

In practice, use "1" for your root partition, / and 2 for the rest. All partitions marked with a "2" are checked in sequence and you do not need to specify an order.

Use "0" to disable checking the file system at boot or for network shares.

You may also "tune" or set the frequency of file checks (default is every 30 mounts) but in general these checks are designed to maintain the integrity of your file system and thus you should strongly consider keeping the default settings.

xxxx


Other commands:

blkid

The blkid program prints the type of content (e.g. filesystem or swap) that a block device holds. When device is specified, tokens from only this device are displayed. The fstab file uses can use UUID to identify partitions to mount.

example:

> blkid
/dev/sda1: UUID="d3c2f6a4-9eeb-41d8-8a56-35899de9b422" TYPE="xfs"
/dev/sda2: UUID="2f4bda65-9282-454e-b485-c02ac1b1d10a" TYPE="swap"
/dev/sda3: UUID="7170e712-f9df-43a4-b9bb-777ddb66eaa4" TYPE="xfs"
/dev/sdb1: UUID="vmhfqT-35TA-YLsr-4n8c-HUSl-VUtg-zYvccG" TYPE="LVM2_member"
/dev/mapper/myVolumeGroup-myLogicalVolume: UUID="f28f02f5-8ff7-4657-8af6-c4e2331a75e1" TYPE="ext4"

To un-mount use command umount.

umount /mnt/myLogicalVolume
or 
umount /dev/sda5 

Make sure that you are not in the directory you want to umount.



To Resize a Logical Volume and file system
lvrersize -L 14G /dev/dir/myDir

then increase size of file system

                xfs_growfs /dev/dir/myDir

                
Mount remote file systems or remote volumes

This feature is useful to attach windows volumes to Linux<>

to mount /temp to 10.1.1.14/temp

mount -t cifs  -o username=myuser password=Password //10.1.1.14/temp /temp

This will only remain until reboot. To maintain permanently add to /etc/fstab file

//10.1.1.14/temp    /temp   cifs    users,rw   username=myuser,password=Password //10.1.1.14/temp /temp    0  0
Using LUKS for encryption

LUKS is the standard for Linux hard disk encryption. By providing a standard on-disk-format, it does not only facilitate compatibility among distributions, but also provides secure management of multiple user passwords.

Install cryptsetup utility

# yum install cryptsetup-luks
To encrpt /dev/xvdc.
# cryptsetup -y -v luksFormat /dev/xvdc

Initialize the volume, and set an initial key or passphrase

# cryptsetup luksOpen /dev/xvdc backup2

see mapping for /dev/mapper/backup2

ls -l /dev/mapper/backup2

use status for the mapping:

# cryptsetup -v status backup2

Dump LUKS headers

# cryptsetup luksDump /dev/xvdc

Format LUKS partition

Clean drive: write zeros to /dev/mapper/backup2 encrypted device. This will allocate block data with zeros.

# dd if=/dev/zero of=/dev/mapper/backup2




B. Remove disk/partion/physical volume/logical group/logical volume

Steps to completely undue previous process and delete/remove disk, partion, physical volume, logical group and logical volume

1. unmount partion
umount /dev/myVolumeGroup/myLogicalVolume
2. remove mount point
rm -r /dev/myVolumeGroup/myLogicalVolume
3. Remove Volume Group
> vgremove myVolumeGroup
Do you really want to remove volume group "myVolumeGroup" containing 1 logical volumes? [y/n]: y
Do you really want to remove active logical volume myVolumeGroup/myLogicalVolume? [y/n]: y
  Logical volume "myLogicalVolume" successfully removed
  Volume group "myVolumeGroup" successfully removed
4. Remove Physical Volume
> pvremove /dev/sdb1
  Labels on physical volume "/dev/sdb1" successfully wiped.
5. in VM console
goto VM proprties and delete disk 2 and disk 3
6. remove entry in file /etc/fstab
7. reboot and check everthing is gone



C. Rename a Logical Volume

To rename a logical Volume use lvrename, but first have to umount the logical volume.

umount /dev/myVolumeGroup/myLogicalVolume 

lvrename   /dev/myVolumeGroup/myLogicalVolume  /dev/myVolumeGroup/myLogicalVolumeNEW 

mount /dev/myVolumeGroup/myLogicalVolumeNEW  /mnt/myMountPoint

add entry to /etc/fstab file to make it permanent.

/dev/myVolumeGroup/myLogicalVolumeNEW /mnt/myMountPoint  defaults ext4  0 0 

To confirm change has been done:

> df -h
Filesystem                                    Size  Used Avail Use% Mounted on
/dev/sda3                                      28G  1.2G   27G   4% /
devtmpfs                                      479M     0  479M   0% /dev
tmpfs                                         489M     0  489M   0% /dev/shm
tmpfs                                         489M   50M  439M  11% /run
tmpfs                                         489M     0  489M   0% /sys/fs/cgroup
/dev/sda1                                     297M  152M  145M  52% /boot
tmpfs                                          98M     0   98M   0% /run/user/0
/dev/mapper/myVolumeGroup-myLogicalVolumeNEW  976M  2.6M  907M   1% /mnt/myMountPoint



D. To resize a Logical Volume

To resize a logical Volume, we first have to un mount the logical volume.

umount /dev/myVolumeGroup/myLogicalVolume 

e2fsck is used to check a Linux extended file system. the -f switch forces checking even if filesystem is marked clean.

> e2fsck -f  /dev/myVolumeGroup/myLogicalVolume
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/myVolumeGroup/myLogicalVolume: 11/128016 files (0.0% non-contiguous), 26684/512000 blocks

Next perform lvresize command and re-mount partition. Note that this will likely destroy any filesystem located on that volume, so take appropriate backup first.

lvresize -L 400M /dev/myVolumeGroup/myLogicalVolume

  WARNING: Reducing active logical volume to 400.00 MiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce myVolumeGroup/myLogicalVolume? [y/n]: y
  Size of logical volume myVolumeGroup/myLogicalVolume changed from 500.00 MiB (125 extents) to 400.00 MiB (100 extents).
  Logical volume myVolumeGroup/myLogicalVolume successfully resized.

CHeck volume again

> e2fsck -f  /dev/myVolumeGroup/myLogicalVolume

Resize the filesystem to match the LVs size, check and mount it

> resize2fs -p /dev/myVolumeGroup/myLogicalVolume

> mount /dev/myVolumeGroup/myLogicalVolume  /mnt/myMountPoint

The resize2fs program will resize ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink an unmounted file system located on device. If the filesystem is mounted, it can be used to expand the size of the mounted filesystem, assuming the kernel supports on-line resizing.

> resize2fs -f -p /dev/myVolumeGroup/myLogicalVolume



E. To extend and existing logical volume

extend to 2.5G to existing logical volume

lvextend -L2500M /dev/myVolumeGroup/myLogicalVolume

add 200M to existing logical volume

lvextend -L+200M /dev/myVolumeGroup/myLogicalVolume



       

F. Using parted to create a partition instead of fdisk

The GNU parted utility is an alternative to the fdisk utility. It is equally useful for creating space for new operating systems, reorganizing disk usage, copying data on hard disks and disk imaging. The package contains a library, libparted, as well as well as a command-line frontend, parted, which can also be used in scripts. Note that some parted commands are not included in Red Hat/Centos version (for example: resize, resizepart).


Parted has similar functionality as fdisk. One reason to use parted is that it supports disks bigger than 2TB.

Here we use parted to see existing disks and partitions:

> parted -l
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 32.2GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system     Flags
 1      1049kB  316MB   315MB   primary  xfs             boot, lvm
 2      316MB   2463MB  2147MB  primary  linux-swap(v1)
 3      2463MB  32.2GB  29.7GB  primary  xfs             lvm


Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: pc98
Disk Flags:

use parted on a specific disk

> parted /dev/sdb 
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.

type m for menu

(parted) m
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, 
                                           all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted

On a new disk, the first think you have to do is mklabel command. This command creates a new disk label, of type label-type. The new disk label will have no partitions. For some reason, we use the msdos type for Linux OS.

(parted) mklabel msdos 

Make a new 3 Gig Partion using parted:


(parted) mkpart primary ext4 1Gig 3Gig
Warning: You requested a partition from 1074MB to 3221MB (sectors 2097152..6291456).
The closest location we can manage is 1078MB to 3224MB (sectors 2104515..6297479).
Is this still acceptable to you?
Yes/No? Yes
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? i

See what was created

(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: pc98
Disk Flags:

Number  Start   End     Size    File system  Name     Flags
 1      1078MB  3224MB  2147MB               primary

If you make a mistake you can use rm to remove disk partition

(parted) rm
Partition number? 1

Use quit to exit

(parted) quit
Information: You may need to update /etc/fstab.

You can now format the partition or perform LMV commands on the new partition.

For whole disk devices only the partition table must be erased, which will effectively destroy all data on that disk. This can be done by zeroing the first sector with:

dd if=/dev/zero of=PhysicalVolume bs=512 count=1