sudoedit.com!

Just some guy on the internet.

In this post I want to cover one of the most commonly used features of lvm, extending a logical volume. If you were following along with the last post, “Working with logical volumes part 1”, then you should already have a volume group with a couple of live volumes attached.

With lvm you can quickly and easily extend a Linux file system on the fly without interrupting any services.

Becoming familiar with our lvm environment.

The first thing we should do is become familiar with our current lvm environment. Open your terminal and type sudo pvscan.

The pvscan command will display some basic information about your volume groups (if you have any). After running pvscan you should see the following output (assuming that you are following along from the previous post).

sudo pvscan    
  PV /dev/sdb    VG vgtest          lvm2 [10.00 GiB / 6.00 GiB free]


From the above we can see a few things. First is the location of our physical volume (/dev/sdb). Next is the Volume group it is attached to vgtest. Last you can see the size, 10 GB with 6GB free.

You can get more detailed output with pvdisplay

sudo pvdisplay
     --- Physical volume ---
    PV Name               /dev/sdb
    VG Name               vgtest
    PV Size               10.00 GiB / not usable 4.00 MiB
    Allocatable           yes
    PE Size               4.00 MiB
    Total PE              2559
    Free PE               1535
    Allocated PE          1024
    PV UUID               tVrpRc-U1yi-IMdP-ONog-jDFh-pjej-VfX9ct


Check the state of our current live volumes by running lvscan.

sudo lvscan

    ACTIVE     '/dev/vgtest/testlv' [2.00 GiB] inherit
    ACTIVE     '/dev/vgtest/omg_testlv' [2.00 GiB] inherit


In this case I have two logical volumes testlv and omg_testlv each of which is 2 GB in size. Once again we can see more information on these volumes by running lvdisplay

sudo lvdisplay

     --- Logical volume ---
    LV Path                /dev/vgtest/testlv
    LV Name                testlv
    VG Name                vgtest
    LV UUID                zw2JB7-gmmZ-4llC-FLf1-jJhE-B82R-Xz4Cxc
    LV Write Access        read/write
    LV Creation host, time fedora01, 2017-11-01 21:24:33 -0400
    LV Status              available
    # open                 1
    LV Size                2.00 GiB
    Current LE             512
    Segments               1
    Allocation             inherit
    Read ahead sectors     auto
    - currently set to     256
    Block device           253:2
    --- Logical volume ---
    LV Path                /dev/vgtest/omg_testlv
    LV Name                omg_testlv
    VG Name                vgtest
    LV UUID                h6eLKT-Aq5u-UXem-dgtA-OIXs-z901-lNRhNp
    LV Write Access        read/write
    LV Creation host, time fedora01, 2017-11-01 21:25:28 -0400
    LV Status              available
    # open                 1
    LV Size                2.00 GiB
    Current LE             512
    Segments               1
    Allocation             inherit
    Read ahead sectors     auto
    - currently set to     256
    Block device           253:3


Running vgdisplay much like pvdisplay and lvdisplay will output detailed information about our volume group.

sudo vgdisplay
    --- Volume group ---
    VG Name               vgtest
    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               10.00 GiB
    PE Size               4.00 MiB
    Total PE              2559
    Alloc PE / Size       1024 / 4.00 GiB
    Free  PE / Size       1535 / 6.00 GiB
    VG UUID               HkxLMU-ncGp-pKIc-l35e-Fbx0-EU80-Iublex


So, now that we have familiarized ourselves with the lvm environment that we previously set up, we know a few things.

  1. We have one physical volume, /dev/sdb which is 10 GB in size.
  2. That physical volume contains the volume group vgtest.
  3. The volume group vgtest contains two logical volumes testlv and omg_testlv each of which are 2 GB in size.

Resize a logical volume

Let's resize one of our logical volumes. Both of our volumes are 2 GB, but lets make testlv 4 GB using the lvextend command.

sudo lvextend /dev/vgtest/testlv -L 4G
  Size of logical volume vgtest/testlv changed from 2.00 GiB (512 extents) to 4.00 GiB (1024 extents).
  Logical volume vgtest/testlv successfully resized.


In this case I used the -L option to tell lvm that I wanted the final size of the volume to be 4G, alternatively you could use -L +2G to the same effect. Or you can use lowercase L -l to specify extents directly. I suggest you read the man page for lvextend if you plan to use it on a regular basis.

Once again run lvscan to verify that your volume has indeed expanded to 4 GB.

sudo lvscan
ACTIVE            '/dev/vgtest/testlv' [4.00 GiB] inherit
ACTIVE            '/dev/vgtest/omg_testlv' [2.00 GiB] inherit


Also notice that your /testlv file system has not changed. It is still 2 GB as far as the file system is concerned.

df -h
    Filesystem                     Size  Used Avail Use% Mounted on
    devtmpfs                       937M     0  937M   0% /dev
    tmpfs                          949M     0  949M   0% /dev/shm
    tmpfs                          949M  2.0M  947M   1% /run
    tmpfs                          949M     0  949M   0% /sys/fs/cgroup
    /dev/mapper/fedora-root         26G  6.2G   18G  26% /
    tmpfs                          949M   16K  949M   1% /tmp
    /dev/sda2                      976M  140M  770M  16% /boot
    /dev/sda1                      200M  9.5M  191M   5% /boot/efi
    /dev/mapper/vgtest-testlv      2.0G   35M  2.0G   2% /testlv
    /dev/mapper/vgtest-omg_testlv  2.0G   35M  2.0G   2% /omgtestlv
    tmpfs                          190M   16K  190M   1% /run/user/42
    tmpfs                          190M     0  190M   0% /run/user/1000


Notice the highlighted line. Why didn't the file system size increase?

When you do an extend operation keep in mind that you must also grow the underlying file system in order to make the additional space usable.

We formatted our file system as xfs in the last tutorial so we will use xfs_growfs to allow the file system to use the additional storage space.

sudo xfs_growfs /testlv
    meta-data=/dev/mapper/vgtest-testlv isize=512    agcount=4, agsize=131072 blks
    =                       sectsz=4096  attr=2, projid32bit=1
    =                       crc=1        finobt=1 spinodes=0 rmapbt=0
    =                       reflink=0
    data     =                       bsize=4096   blocks=524288, imaxpct=25
    =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal               bsize=4096   blocks=2560, version=2
    =                       sectsz=4096  sunit=1 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    data blocks changed from 524288 to 1048576


Check the size again with df -h This time you should notice that /testlv is now 4 GB.

df -h
    Filesystem                     Size  Used Avail Use% Mounted on
    devtmpfs                       937M     0  937M   0% /dev
    tmpfs                          949M     0  949M   0% /dev/shm
    tmpfs                          949M  2.0M  947M   1% /run
    tmpfs                          949M     0  949M   0% /sys/fs/cgroup
    /dev/mapper/fedora-root         26G  6.2G   18G  26% /
    tmpfs                          949M   16K  949M   1% /tmp
    /dev/sda2                      976M  140M  770M  16% /boot
    /dev/sda1                      200M  9.5M  191M   5% /boot/efi
    /dev/mapper/vgtest-testlv      4.0G   37M  4.0G   1% /testlv
    /dev/mapper/vgtest-omg_testlv  2.0G   35M  2.0G   2% /omgtestlv
    tmpfs                          190M   16K  190M   1% /run/user/42


The lvextend command has another option, -r will automatically increase the size of the file system without the need to call an additional command to extend the file system. Try it on the omg_testlv volume.

sudo lvextend /dev/vgtest/omg_testlv -L 4G -r
    Size of logical volume vgtest/omg_testlv changed from 2.00 GiB (512 extents) to 4.00 GiB (1024 extents).
    Logical volume vgtest/omg_testlv successfully resized.
    meta-data=/dev/mapper/vgtest-omg_testlv isize=512    agcount=4, agsize=131072 blks
    =                       sectsz=4096  attr=2, projid32bit=1
    =                       crc=1        finobt=1 spinodes=0 rmapbt=0
    =                       reflink=0
    data     =                       bsize=4096   blocks=524288, imaxpct=25
    =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal               bsize=4096   blocks=2560, version=2
    =                       sectsz=4096  sunit=1 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    data blocks changed from 524288 to 1048576


Notice this time the output of the lvextend command includes the xfx_growfs output. That is because lvm is smart enough to know how to resize common file systems for you, so that you only need to run one command to get the desired results.

Once you are done I suggest that you run through the initial familiarization steps again so that you can see the changes that we made in this tutorial. Compare the new output with the old.

In the next post we will add a new disk to volume group and learn how to move all of our data off of one disk and onto another.

Not too long ago I ran into a problem where a server with systemd would not shutdown or reboot through normal means.

When executing sudo shutdown -r now I would get a weird message back as output:

    Failed to start reboot.target: Connection timed out
    See system logs and 'systemctl status reboot.target' for details.
    Failed to open /dev/initctl: No such device or address
    Failed to talk to init daemon.


I'm still not entirely certain what caused the problem and the suggestion of running systemctl status reboot.target to troubleshoot simply resulted in the same message being displayed.

However, if you run into this problem and you just need to get your services back up and running you can force a reboot like this:

Try this first:

systemctl --force reboot


If that doesn't work, which it didn't for me, add another --force but know that this will unceremoniously kill all running process.

systemctl --force --force reboot


This of course assumes that you can still gain access to the terminal via ssh. This operation is essentially the same as holding down the power button on a physical machine or hitting the reset button on a VM.

According to the man page this can dangerous if you have a process running that is trying to save data so be certain that you want to forcibly kill off every process on your server before running this command. Try it with only a single --force first.

Part one of working with logical volumes will cover the basic's involved in creating logical volumes.

TL;DR

For those of you who just want the order of the commands.

sudo pvcreate </path/to/device>
sudo vgcreate <vgname> </path/to/device>
sudo lvcreate -n <lvname> -L <size> <vgname>
sudo mkfs.<filesystem> </path/to/lv>


What you need to follow this guide

  • A free disk (I used an empty virtual machine disk)
  • Any Linux distribution (In this example I'll be using Fedora 26, but the commands are the same across the entire Linux spectrum)
  • LVM packages (lvm2 – usually pre-installed)

What is LVM?

Logical Volume Management (LVM) offers a way to abstract a disk, multiple disks, or disk partitions into one logical volume. LVM file systems can be rearranged, resized, moved, removed, created, and deleted on the fly. They offer incredible flexibility when setting up a new system or when rethinking the storage layout of an existing system.

Logical Volume Management filesystems are made up of 4 major parts

  1. The physical device (physical volume)

    • This is the storage device or devices that will make up the volume group.
  2. The Volume Group

    • The volume group is essentially a disk or a group of physical devices that have been separated into a group. The Volume Group is just a pool of disks. It can contain just one disk (or partition), or many disks.
    • The volume group is a logical volume made up of physical volumes
  3. The Logical Volume

    • The logical volume is some portion of the volume group that will be dedicated to a particular filesystem. One volume group can have many live volumes.
  4. The filesystem

    • After you create a Live Volume you must format it into one of the many Linux filesystems. Most likely ext4 or xfs.

Here is what you should remember if you are new to LVM

  • Physical Volumes (the raw storage), make up Logical Volume Groups.
  • Volume Groups are a logical representation of physical devices.
  • Volume Groups contain Live Volumes which are formatted as filesystems.
  • Filesystems are responsible for storing your data.

Create a new Volume

Step one – identify your storage device

We can identify our storage devices using the fdisk command

sudo fdisk -l
    Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 4096 bytes
    I/O size (minimum/optimal): 4096 bytes / 4096 bytes
    Disklabel type: gpt
    Disk identifier: 0E3D261B-275C-49D0-8C9A-427B5CEEAD4F

    Device       Start      End  Sectors  Size Type
    /dev/sda1     2048   411647   409600  200M EFI System
    /dev/sda2   411648  2508799  2097152    1G Linux filesystem
    /dev/sda3  2508800 62912511 60403712 28.8G Linux LVM


    Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 4096 bytes
    I/O size (minimum/optimal): 4096 bytes / 4096 bytes


    Disk /dev/mapper/fedora-root: 25.8 GiB, 27703377920 bytes, 54108160 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 4096 bytes
    I/O size (minimum/optimal): 4096 bytes / 4096 bytes


    Disk /dev/mapper/fedora-swap: 3 GiB, 3221225472 bytes, 6291456 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 4096 bytes
    I/O size (minimum/optimal): 4096 bytes / 4096 bytes


The device I want is shown as Disk /dev/sdb 10GiB

Step two – create the physical volume

Use the pvcreate command to create the new physical volume.

sudo pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.


Step three – create the volume group

The vgcreate command, creates a new volume group

sudo vgcreate vgtest /dev/sdb
  Volume group "vgtest" successfully created


Step four – create a logical volume

sudo lvcreate -n testlv -L 2G vgtest
  Logical volume "testlv" created.


Maybe another one

sudo lvcreate -n omg_testlv -L 2G vgtest
  Logical volume "omg_testlv" created.


lvcreate takes several arguments. You must specify a name, a size, and the volume group to attach it to.

Step five – make the filesystems

I'm formatting my file systems as xfs in this case.

Start with testlv

sudo mkfs.xfs /dev/mapper/vgtest-testlv
    meta-data=/dev/mapper/vgtest-testlv isize=512    agcount=4, agsize=131072 blks
             =                       sectsz=4096  attr=2, projid32bit=1
             =                       crc=1        finobt=1, sparse=0, rmapbt=0, reflink=0
    data     =                       bsize=4096   blocks=524288, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal log           bsize=4096   blocks=2560, version=2
             =                       sectsz=4096  sunit=1 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0


Then omg_testlv

sudo mkfs.xfs /dev/mapper/vgtest-omg_testlv
    meta-data=/dev/mapper/vgtest-omg_testlv isize=512    agcount=4, agsize=131072 blks
             =                       sectsz=4096  attr=2, projid32bit=1
             =                       crc=1        finobt=1, sparse=0, rmapbt=0, reflink=0
    data     =                       bsize=4096   blocks=524288, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal log           bsize=4096   blocks=2560, version=2
             =                       sectsz=4096  sunit=1 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0


Step 3 – add the new filesystems to your fstab

Make a couple directories as mount points.

sudo mkdir /testlv
sudo mkdir /omgtestlv


Edit the /etc/fstab file and append the following.

/dev/mapper/vgtest-testlv /testlv               xfs     defaults        0 0
/dev/mapper/vgtest-omg_testlv /omgtestlv        xfs     defaults        0 0


Mount the filesystems

sudo mount -a


How do I know that this did something?

Check your file system mounts with df

df -hT
    Filesystem                    Type      Size  Used Avail Use% Mounted on
    devtmpfs                      devtmpfs  938M     0  938M   0% /dev
    tmpfs                         tmpfs     950M     0  950M   0% /dev/shm
    tmpfs                         tmpfs     950M  2.4M  948M   1% /run
    tmpfs                         tmpfs     950M     0  950M   0% /sys/fs/cgroup
    /dev/mapper/fedora-root       ext4       26G  5.6G   19G  24% /
    tmpfs                         tmpfs     950M   28K  950M   1% /tmp
    /dev/sda2                     ext4      976M  109M  800M  12% /boot
    /dev/sda1                     vfat      200M  9.5M  191M   5% /boot/efi
    tmpfs                         tmpfs     190M   24K  190M   1% /run/user/42
    tmpfs                         tmpfs     190M   40K  190M   1% /run/user/1000
    /dev/mapper/vgtest-testlv     xfs       2.0G   35M  2.0G   2% /testlv
    /dev/mapper/vgtest-omg_testlv xfs       2.0G   35M  2.0G   2% /omgtestlv


Notice our new volumes in the output of df.

Verify the Logical Volume with lvdisplay

sudo lvdisplay
      --- Logical volume ---
      LV Path                /dev/vgtest/testlv
      LV Name                testlv
      VG Name                vgtest
      LV UUID                zw2JB7-gmmZ-4llC-FLf1-jJhE-B82R-Xz4Cxc
      LV Write Access        read/write
      LV Creation host, time fedora01, 2017-11-01 21:24:33 -0400
      LV Status              available
      # open                 1
      LV Size                2.00 GiB
      Current LE             512
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     256
      Block device           253:2

      --- Logical volume ---
      LV Path                /dev/vgtest/omg_testlv
      LV Name                omg_testlv
      VG Name                vgtest
      LV UUID                h6eLKT-Aq5u-UXem-dgtA-OIXs-z901-lNRhNp
      LV Write Access        read/write
      LV Creation host, time fedora01, 2017-11-01 21:25:28 -0400
      LV Status              available
      # open                 1
      LV Size                2.00 GiB
      Current LE             512
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     256
      Block device           253:3


If you are going to be working with LVM on a regular basis you will want to familiarize yourself with the following commands:

vgdisplay, vgscan, pvdisplay, pvscan, lvdisplay, lvscan

Helpful Links:

CentOS LVM Docs

Ubuntu LVM Docs

There are many instances when it's useful to have multiple files open in vim, but if you aren't familiar with this tool you can find yourself needlessly jumping around between multiple windows.

If you are not already using vim start by opening a command prompt and type vimtutor, once you've become familiar with how to navigate, search, and edit a document with vim this post will make more sense to you.

Use vim to see what is different between two files

There are several ways to find differences between two files on a Linux server or desktop. I like to use vim when I'm scanning a configuration file for recent changes from an earlier iteration ( assuming of course that there is a backup of the last known, good, configuration).

Comparing two files is a common task and there are several ways to view the differences between multiple files, but occasionally you may want to do this visually side by side.

Create two files file1 and file2.

Make file1 looks like this:

This is a file to test diff
file 1


Similarly make file2 looks like this:

This is a file to test diff
file 2


With the -d option vim can open two, three, or four files and vim will open the files and highlight the line that is different. Try it.

vim -d file1 file2


You should see something similar to this:

vim diff

Notice how the file opens with the cursor set to the line that is different, this is so that you can quickly get to work at the exact place where the files differ. Also helpful, is the fact that vim will automatically highlight the entire line to quickly draw your attention to the differences.

Split

Using -d highlights a use for the split command. If you are working on a file and want to look at another file at the same time. Maybe to use some lines as a template, or just to compare an old configuration to a new one you can use the :split or :vsplit command.

Open file1 in vim.

vim file1


And then issue the :split command to open file2.

:split file2


The default split command will split the screen horizontally:

horizontal split

Close file2 and this time look at the vertical split by issuing the :vsplit or :vsp command

:vsp file2

This time, as the name suggests, the window will split horizontally allowing you to view files side by side. Personally I find this format to be the more useful of the two.

vertical split

You can navigate between each split by using the key combination ctrl-wand then the direction key in which you want to move. So, to move from file2 to file1 you would hit ctrl w and then the right arrow (or the L key if you don't like moving your hand quite so far).

There are other fancy things you can do with files in vim. If you are feeling a little wild look up tabs and buffers, both of which might make your life a little easier if you aren't already using them.