Score:0

How to extend LVM swap space

kz flag

I have an Ubuntu 18.04.5 system (ext4) with 1 GB of swap in a logical volume. I need to increase the total swap space to 5 GB.

Currently, I have this configuration:

$ sudo pvs
  PV         VG        Fmt  Attr PSize    PFree
  /dev/sda1  ubuntu-vg lvm2 a--  <931.51g    0 
  /dev/sdb1  ubuntu-vg lvm2 a--    <1.82t    0 
$ sudo vgs
  VG        #PV #LV #SN Attr   VSize  VFree
  ubuntu-vg   2   2   0 wz--n- <2.73t    0 
$ sudo lvs
  LV     VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root   ubuntu-vg -wi-ao----  <2.73t                                                    
  swap_1 ubuntu-vg -wi-ao---- 976.00m   
$ sudo pvscan
  PV /dev/sdb1   VG ubuntu-vg       lvm2 [<1.82 TiB / 0    free]
  PV /dev/sda1   VG ubuntu-vg       lvm2 [<931.51 GiB / 0    free]
  Total: 2 [<2.73 TiB] / in use: 2 [<2.73 TiB] / in no VG: 0 [0   ]
$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/root
  LV Name                root
  VG Name                ubuntu-vg
  LV UUID                Dt0Ebq-IBqb-J2WE-rCNW-lAZ2-BTxf-V45T0f
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2018-11-16 18:18:30 -0700
  LV Status              available
  # open                 1
  LV Size                <2.73 TiB
  Current LE             715153
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/swap_1
  LV Name                swap_1
  VG Name                ubuntu-vg
  LV UUID                FDmki1-o0Jk-aXP5-bip8-du01-HCoZ-AdFiGC
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2018-11-16 18:18:30 -0700
  LV Status              available
  # open                 2
  LV Size                976.00 MiB
  Current LE             244
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
$ sudo vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               <2.73 TiB
  PE Size               4.00 MiB
  Total PE              715397
  Alloc PE / Size       715397 / <2.73 TiB
  Free  PE / Size       0 / 0   
  VG UUID               MU6s8C-OQZp-n7m7-BWF7-JyYi-QRbe-Tq1klP

I read this article Ubuntu – How to modify the size of swap with LVM partitions, and the process seems straightforward. However, I did some more reading, Increase the size of an LVM logical volume, and the process seems much more involved and fraught with serious issues if I make a mistake.

Is the first article correct for my system, or do I need to follow the second article to increase the swap size? I could also just make an additional swap file of 4 GB instead of changing the current swap volume. Is that equivalent to increasing the current 1 GB swap volume from a performance standpoint?

Thanks!

Mark

HuHa avatar
es flag
Notice you can simply add _another_ swap LV or an additional swap _file_ without degrading performance. That might be easier than resizing the existing one. Just make sure you add it to `/etc/fstab` as well.
mondotofu avatar
cn flag
I would not put the swap partition on a logical volume. Do you have other storage available not allocated to the LVM?
HuHa avatar
es flag
@mondotofu: There is absolutely nothing wrong with using an LVM LV as swap. Why do you think that might be bad?
mondotofu avatar
cn flag
There's no need to spread this over multiple devices -- even worse if you're applying a RAID policy on the LV swap volume. It's meant as an immediate storage area to store the process state and related data when the load gets too high to allow other processes to run, and then to read back in as needed. Having the overhead of a logical volume to manage which device and disk blocks to find to enable swap delays the speeding of paging and swapping, and can reduce performance.
kz flag
All the storage I have is in the LVM in /dev/sda1 and /dev/sdb1. My post is focused on the two articles I cited, which show very different ways to increase my swap size using the LVM. The first article cited is very simple and straightforward, the second one involves many more steps, and indicates that the steps in the first article are actually wrong. I am not sure which one to believe or follow. Do you have any recommendations?
mondotofu avatar
cn flag
Yes, it's complicated. First off, you need to make a backup of your system before changing anything. By documenting your configuration with the output of all the commands, you've made a good first step towards recovery if you need it.
mondotofu avatar
cn flag
Second, read the man pages for LVM ... Actually LVM2 in this release. and consider GUI LVM managers such as KVPM mentioned in this article. https://www.linuxjournal.com/content/review-gui-lvm-tools because GParted can only help you with PV (physical volumes) which is only part of the scaffolding.
mondotofu avatar
cn flag
Instead of one LV for root and one LV for swap in your volume group, consider making multiple logical volumes for multiple purposes that you might mount as /boot, / (root), /home, /data, /archive. Think of the minimum and maximum amount of space you want for each area, but start out with the minimum size of each LV and use the built-in tools for LVM2 : lvexpand to add more space from the volume group, lvreduce, lvresize to change their size and return extents to the volume group. RTFM and plan for flexibility and change. LVM supports a number of storage options.
Score:0
cn flag

If you're not going to redo your LVM architecture, per my comments, then there are shortcuts:

you can use the swapoff technique in the first article. [Ubuntu – How to modify the size of swap with LVM partitions][1],

But first, let's be sure of your swap location (whether a file or a partition).

sudo swapon --show

I would expect something like this:

NAME                  TYPE      SIZE USED PRIO
/dev/ubuntu-vg/swap_1 partition 976M   0B   -2

If that's the case, then you can perform the following command:

sudo swapoff /dev/ubuntu-vg/swap_1

Then you can create a new swapfile in the root directory following the directions here in Where is the swapfile created?

Instead of a 2GB file, aim for a 5GB file. If the swap area is still not large enough, you can repeat the techniques until you get the right size for your needs.

sudo fallocate -l 5g /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Afterwards you won't require the LV for the swap.

Note this is a shortcut. Please give strong consideration to your architecture and storage needs. You can still redo it and give yourself flexibility by integrating good backup and recovery plans.

kz flag
I don't understand your recommendation to redo my LVM architecture. I am not tied to this architecture, it came with the computer which only has 2 physical devices. At this point, I would like to make a permanent swap space, just not sure what is the best approach. Perhaps a 5 GB swap file and somehow reclaim the 1 GB swap lv for normal storage. I have no idea how to do that.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.