How to Manage and Use LVM (Logical Volume Management) in Ubuntu

banner-1

In our previous article we told you what LVM is and what you may want to use it for, and today we are going to walk you through some of the key management tools of LVM so you will be confident when setting up or expanding your installation.

As stated before, LVM is a abstraction layer between your operating system and physical hard drives. What that means is your physical hard drives and partitions are no longer tied to the hard drives and partitions they reside on. Rather, the hard drives and partitions that your operating system sees can be any number of separate hard drives pooled together or in a software RAID.

To manage LVM there are GUI tools available but to really understand what is happening with your LVM configuration it is probably best to know what the command line tools are. This will be especially useful if you are managing LVM on a server or distribution that does not offer GUI tools.

Most of the commands in LVM are very similar to each other. Each valid command is preceded by one of the following:

  • Physical Volume = pv
  • Volume Group = vg
  • Logical Volume = lv

The physical volume commands are for adding or removing hard drives in volume groups. Volume group commands are for changing what abstracted set of physical partitions are presented to your operating in logical volumes. Logical volume commands will present the volume groups as partitions so that your operating system can use the designated space.

Downloadable LVM Cheat Sheet

To help you understand what commands are available for each prefix we made a LVM cheat sheet. We will cover some of the commands in this article, but there is still a lot you can do that won’t be covered here.

All commands on this list will need to be run as root because you are changing system wide settings that will affect the entire machine.

image.png

How to View Current LVM Information

The first thing you may need to do is check how your LVM is set up. The s and display commands work with physical volumes (pv), volume groups (vg), and logical volumes (lv) so it is a good place to start when trying to figure out the current settings.

The display command will format the information so it’s easier to understand than the s command. For each command you will see the name and path of the pv/vg and it should also give information about free and used space.

pvdisplay

The most important information will be the PV name and VG name. With those two pieces of information we can continue working on the LVM setup.

Creating a Logical Volume

Logical volumes are the partitions that your operating system uses in LVM. To create a logical volume we first need to have a physical volume and volume group. Here are all of the steps necessary to create a new logical volume.

Create physical volume

We will start from scratch with a brand new hard drive with no partitions or information on it. Start by finding which disk you will be working with. (/dev/sda, sdb, etc.)

Note: Remember all of the commands will need to be run as root or by adding ‘sudo’ to the beginning of the command.

fdisk -l

If your hard drive has never been formatted or partitioned before you will probably see something like this in the fdisk output. This is completely fine because we are going to create the needed partitions in the next steps.

fdisk

Our new disk is located at /dev/sdb so lets use fdisk to create a new partition on the drive.
There are a plethora of tools that can create a new partition with a GUI, including Gparted, but since we have the terminal open already, we will use fdisk to create the needed partition.

From a terminal type the following commands:

fdisk /dev/sdb

This will put you in a special fdisk prompt.

fdisk00

Enter the commands in the order given to create a new primary partition that uses 100% of the new hard drive and is ready for LVM. If you need to change the partition size or want multiple partions I suggest using GParted or reading about fdisk on your own.

Warning: The following steps will format your hard drive. Make sure you don’t have any information on this hard drive before following these steps.

  • n = create new partition
  • p = creates primary partition
  • 1 = makes partition the first on the disk

Push enter twice to accept the default first cylinder and last cylinder.

fdisk01

To prepare the partition to be used by LVM use the following two commands.

  • t = change partition type
  • 8e = changes to LVM partition type
fdisk02

Verify and write the information to the hard drive.

  • p = view partition setup so we can review before writing changes to disk
  • w = write changes to disk
fdisk03

After those commands, the fdisk prompt should exit and you will be back to the bash prompt of your terminal.

Enter pvcreate /dev/sdb1

to create a LVM physical volume on the partition we just created.
You may be asking why we didn’t format the partition with a file system but don’t worry, that step comes later.

pvcreate
Create volume Group

Now that we have a partition designated and physical volume created we need to create the volume group. Luckily this only takes one command.

vgcreate vgpool /dev/sdb1

Vgpool is the name of the new volume group we created. You can name it whatever you’d like but it is recommended to put vg at the front of the label so if you reference it later you will know it is a volume group.

Create logical volume

To create the logical volume that LVM will use:

lvcreate -L 3G -n lvstuff vgpool

lvcreate

The -L command designates the size of the logical volume, in this case 3 GB, and the -n command names the volume. Vgpool is referenced so that the lvcreate command knows what volume to get the space from.

Format and Mount the Logical Volume

One final step is to format the new logical volume with a file system. If you want help choosing a Linux file system, read our how to that can help you choose the best file system for your needs.

mkfs -t ext3 /dev/vgpool/lvstuff

mkfs

Create a mount point and then mount the volume somewhere you can use it.

mkdir /mnt/stuff mount -t ext3 /dev/vgpool/lvstuff /mnt/stuff

mount

Resizing a Logical Volume

One of the benefits of logical volumes is you can make your shares physically bigger or smaller without having to move everything to a bigger hard drive. Instead, you can add a new hard drive and extend your volume group on the fly. Or if you have a hard drive that isn’t used you can remove it from the volume group to shrink your logical volume.

There are three basic tools for making physical volumes, volume groups, and logical volumes bigger or smaller.

Note: Each of these commands will need to be preceded by pv, vg, or lv depending on what you are working with.

  • resize – can shrink or expand physical volumes and logical volumes but not volume groups
  • extend – can make volume groups and logical volumes bigger but not smaller
  • reduce – can make volume groups and logical volumes smaller but not bigger

Let’s walk through an example of how to add a new hard drive to the logical volume “l(fā)vstuff” we just created.

Install and Format new Hard Drive

To install a new hard drive follow the steps above to create a new partition and add change it’s partition type to LVM (8e). Then use pvcreate to create a physical volume that LVM can recognize.

Add New Hard Drive to Volume Group

To add the new hard drive to a volume group you just need to know what your new partition is, /dev/sdc1 in our case, and the name of the volume group you want to add it to.

This will add the new physical volume to the existing volume group.

vgextend vgpool /dev/sdc1

vgextend
Extend Logical Volume

To resize the logical volume we need to say how much we want to extend by size instead of by device. In our example we just added a 8 GB hard drive to our 3 GB vgpool. To make that space usable we can use lvextend or lvresize.

lvextend -L8G /dev/vgpool/lvstuff

lvextend1

While this command will work you will see that it will actually resize our logical volume to 8 GB instead of adding 8 GB to the existing volume like we wanted. To add the last 3 available gigabytes you need to use the following command.

lvextend -L+3G /dev/vgpool/lvstuff

lvextend2

Now our logical volume is 11 GB in size.

Extend File System

The logical volume is 11 GB but the file system on that volume is still only 3 GB. To make the file system use the entire 11 GB available you have to use the command resize2fs. Just point resize2fs to the 11 GB logical volume and it will do the magic for you.

resize2fs /dev/vgpool/lvstuff

resize2fs

Note: If you are using a different file system besides ext3/4 please see your file systems resize tools.

Shrink Logical Volume

If you wanted to remove a hard drive from a volume group you would need to follow the above steps in reverse order and use lvreduce and vgreduce instead.

  1. resize file system (make sure to move files to a safe area of the hard drive before resizing)
  2. reduce logical volume (instead of + to extend you can also use – to reduce by size)
  3. remove hard drive from volume group with vgreduce

Backing up a Logical Volume

Snapshots is a feature that some newer advanced file systems come with but ext3/4 lacks the ability to do snapshots on the fly. One of the coolest things about LVM snapshots is your file system is never taken offline and you can have as many as you want without taking up extra hard drive space.

banner-2

When LVM takes a snapshot, a picture is taken of exactly how the logical volume looks and that picture can be used to make a copy on a different hard drive. While a copy is being made, any new information that needs to be added to the logical volume is written to the disk just like normal, but changes are tracked so that the original picture never gets destroyed.

To create a snapshot we need to create a new logical volume with enough free space to hold any new information that will be written to the logical volume while we make a backup. If the drive is not actively being written to you can use a very small amount of storage. Once we are done with our backup we just remove the temporary logical volume and the original logical volume will continue on as normal.

Create New Snapshot

To create a snapshot of lvstuff use the lvcreate command like before but use the -s flag.

lvcreate -L512M -s -n lvstuffbackup /dev/vgpool/lvstuff

lvcreate-snapshot

Here we created a logical volume with only 512 MB because the drive isn’t being actively used. The 512 MB will store any new writes while we make our backup.

Mount New Snapshot

Just like before we need to create a mount point and mount the new snapshot so we can copy files from it.

mkdir /mnt/lvstuffbackup mount /dev/vgpool/lvstuffbackup /mnt/lvstuffbackup

mount-snapshot

Copy Snapshot and Delete Logical Volume

All you have left to do is copy all of the files from /mnt/lvstuffbackup/ to an external hard drive or tar it up so it is all in one file.

Note: tar -c will create an archive and -f will say the location and file name of the archive. For help with the tar command useman tar
in the terminal.

tar -cf /home/rothgar/Backup/lvstuff-ss /mnt/lvstuffbackup/

snapshot-backup

Remember that while the backup is taking place all of the files that would be written to lvstuff are being tracked in the temporary logical volume we created earlier. Make sure you have enough free space while the backup is happening.

Once the backup finishes, unmount the volume and remove the temporary snapshot.

umount /mnt/lvstuffbackup lvremove /dev/vgpool/lvstuffbackup/

lvremove

Deleting a Logical Volume

To delete a logical volume you need to first make sure the volume is unmounted, and then you can use lvremove to delete it. You can also remove a volume group once the logical volumes have been deleted and a physical volume after the volume group is deleted.

Here are all the commands using the volumes and groups we’ve created.

umount /mnt/lvstuff lvremove /dev/vgpool/lvstuff vgremove vgpool pvremove /dev/sdb1 /dev/sdc1

delete-lvm

That should cover most of what you need to know to use LVM. If you’ve got some experience on the topic, be sure to share your wisdom in the comments.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市送滞,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌翰撑,老刑警劉巖罩旋,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件啊央,死亡現(xiàn)場離奇詭異,居然都是意外死亡涨醋,警方通過查閱死者的電腦和手機(jī)瓜饥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來浴骂,“玉大人乓土,你說我怎么就攤上這事∷菥” “怎么了趣苏?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長梯轻。 經(jīng)常有香客問我食磕,道長,這世上最難降的妖魔是什么喳挑? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任彬伦,我火速辦了婚禮,結(jié)果婚禮上伊诵,老公的妹妹穿的比我還像新娘单绑。我一直安慰自己,他們只是感情好曹宴,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布搂橙。 她就那樣靜靜地躺著,像睡著了一般笛坦。 火紅的嫁衣襯著肌膚如雪区转。 梳的紋絲不亂的頭發(fā)上唯袄,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天,我揣著相機(jī)與錄音蜗帜,去河邊找鬼恋拷。 笑死,一個(gè)胖子當(dāng)著我的面吹牛厅缺,可吹牛的內(nèi)容都是我干的蔬顾。 我是一名探鬼主播,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼湘捎,長吁一口氣:“原來是場噩夢啊……” “哼诀豁!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起窥妇,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤舷胜,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后活翩,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體烹骨,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年材泄,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了沮焕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,902評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡拉宗,死狀恐怖峦树,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情旦事,我是刑警寧澤魁巩,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站姐浮,受9級特大地震影響谷遂,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜单料,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一埋凯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧扫尖,春花似錦白对、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春条摸,著一層夾襖步出監(jiān)牢的瞬間悦污,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工钉蒲, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留切端,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓顷啼,卻偏偏與公主長得像踏枣,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子钙蒙,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,495評論 0 23
  • 凜凜酷寒冬茵瀑, 烈烈滿山楓。 青青幾翠竹躬厌, 挺挺一孤松马昨。
    李煉閱讀 255評論 0 2
  • 這個(gè)工具有個(gè)bt的地方在于,每個(gè)文件的residue總數(shù)不能超過20w扛施。所以要是結(jié)果不太正常鸿捧,那就實(shí)施輸入少數(shù)的數(shù)...
    keaidelele閱讀 4,559評論 1 51
  • 今天又被拉去相親了,自己都不知道相了多少回親了煮嫌,在不斷的相親的過程中笛谦,只是感覺自己越來越會(huì)演了抱虐,在不同的相親男面前...
    虛心請教閱讀 499評論 0 0
  • 我們在工作中是不是經(jīng)常會(huì)遇到這樣的情況: 領(lǐng)導(dǎo)布置了一項(xiàng)緊急任務(wù)恳邀,需要其他部門的同事提供一份數(shù)據(jù)懦冰,他遲遲不鳥我,好...
    samge閱讀 549評論 0 1