Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Please help with understanding, and changing, swap: LUKS, LVM

  1. #1
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,689
    Distro
    Lubuntu 18.04 Bionic Beaver

    Question Please help with understanding, and changing, swap: LUKS, LVM

    Background

    I installed Ubuntu 20.04 using its default full-disk encryption, using the entire disk (so everything else was overwritten). The Ubuntu installation created three partitions:

    • /dev/nvme0n1p1 (512MiB) EFI System Partition
    • /dev/nvme0n1p2 (732 MiB) /boot
    • /dev/nvme0n1p3 (475GiB) System partition

    The Ubuntu installation encrypted the System partition with LUKS (version 2): /dev/mapper/nvme0n1p3_crypt

    On that, it created a LVM with two virtual partitions, viz. root and swap_1. Here is the full structure:
    Code:
    $ sudo pvscan
      PV /dev/mapper/nvme0n1p3_crypt   VG vgubuntu        lvm2 [<475.71 GiB / 0    free]
      Total: 1 [<475.71 GiB] / in use: 1 [<475.71 GiB] / in no VG: 0 [0   ]
    Code:
    $ sudo vgscan
      Found volume group "vgubuntu" using metadata type lvm2
    Code:
    $ sudo lvscan
      ACTIVE            '/dev/vgubuntu/root' [474.75 GiB] inherit
      ACTIVE            '/dev/vgubuntu/swap_1' [980.00 MiB] inherit
    As you can see, the swap is a virtual partition of 980 MiB.

    First, my question about the swap

    I had understood that Ubuntu 18.04 and after used a swap file rather than a swap partition by default, so I was surprised to find that it used a swap partition.

    I know that an SSD needs a certain flexibility to prevent overuse of a specific area, thereby causing it to fail. Wouldn't a swap file be better than a swap partition on an SSD?

    Second, my request about the swap

    I wish to increase the size of the swap, because sometimes I use applications that use a lot of data (virtual machines, video editing, etc.).

    I might also want to enable hibernation, if that's possible, so I'd need to increase swap size significantly. However, this is not a must-have; I'm prepared to accept that I can't use hibernate.

    So, bearing this in mind, what is the best way to go about increasing the swap size?

    1. Using a Live CD, decrease the size of the root virtual partition and increase the size of the swap virtual partition?
    2. Using a Live CD, delete the virtual swap partition, increase the size of the root partition, and create a swap file (amending /etc/fstab accordingly)?
    3. Something else?

    And…

    Whichever turns out to be the best way, how exactly do I go about it? I have enough knowledge of LVM to be dangerous! In other words, I'm fully familiar with the concepts, but I'm unpracticed at using it.

    Thank you

  2. #2
    Join Date
    Jun 2007
    Location
    Arizona U.S.A.
    Beans
    4,851

    Re: Please help with understanding, and changing, swap: LUKS, LVM

    Swapfile - I understand that you get a swapfile when there is no swap partition found by the installer during installation.

    Increasing swap - Option (2) looks risk free. I would try that but not bother with increasing the size of the root logical volume - it already takes up 474 G of 475 G available.

    Option (1) requires use of LVM commands lvreduce and lvextend.

  3. #3
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    22,152
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Please help with understanding, and changing, swap: LUKS, LVM

    I know that an SSD needs a certain flexibility to prevent overuse of a specific area, thereby causing it to fail. Wouldn't a swap file be better than a swap partition on an SSD?
    No. There's no difference, except the swap file has a tiny bit more overhead due to the file system. This assumes you won't hibernate.

    We need more detailed information to help you with the next questions.
    Use
    Code:
    sudo pvs
    sudo vgs
    sudo lvs
    df -hT -x squashfs -x tmpfs -x devtmpfs
    lsblk -e 7 -o name,size,type,fstype,mountpoint
    free -hm
    to provide that information. It is very important that code tags be used and you show both the commands, all options and output. Those commands provide some redundant overlap and most of the options are here to prevent showing fake-storage devices which are not important at all.

    It looks like the installation has screwed you by allocating all the storage to the root LV. I can see 2 solutions to this. There's the easy way and there's the "right" way. If the install is recent and you plan to use it more than a year, I'd go the more-hassle-but-right way. For a short-term need, I'd go with the easy solution.

    The installer allocating more than 35-50G to the "root" LV is a huge failure in my mind. Here's my desktop disk layout:
    Code:
    $ df -hT -x squashfs -x tmpfs -x devtmpfs
    Filesystem                      Type  Size  Used Avail Use% Mounted on
    /dev/mapper/vgubuntu--mate-root ext4   17G   11G  5.0G  69% /
    /dev/mapper/vgubuntu--mate-home ext4   12G  7.6G  3.7G  68% /home
    /dev/vda1                       vfat  511M  7.1M  504M   2% /boot/efi
    
    $ free -hm
                  total        used        free      shared  buff/cache   available
    Mem:          3.8Gi       928Mi       229Mi       7.0Mi       2.7Gi       2.7Gi
    Swap:         4.1Gi        47Mi       4.1Gi
    I want the smallest "root" LV I can have.
    Then I want the "home" LV to be sized for that I'll need in the next few months. With LVM, increasing an LV is trivial, 5 seconds.
    But reducing an LV is a huge hassle. To reduce the "root" LV will require booting from alternate media so it is unused. Because you've used encryption (I do as well), that means getting to the LVM objects required unlocking the encrypted container first. Depending on the live-boot environment that you use, just clicking in a file manager might prompt you for access. Be certain that you know the name of the encrypted container. If the file manager doesn't make it easy, you'll need to use cryptsetup to open the volume, then vgchange -ay to activate the LVM, then lvreduce --resizefs ... yadda, yadda, yadda. Basically it is like an onion. Each layer has to be unwrapped and handled in the proper order going in, changing things, and coming back out.

    You decide which method.

    BTW,
    Using a Live CD, delete the virtual swap partition, increase the size of the root partition, and create a swap file (amending /etc/fstab accordingly)?
    actually doesn't require using a Live CD. You can do this on the running system.
    Last edited by TheFu; November 30th, 2020 at 11:10 PM.

  4. #4
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    22,152
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Please help with understanding, and changing, swap: LUKS, LVM

    I don't think hibernation works with swapfiles and definitely not inside LVM + LUKS encryption.
    I use standby with LVM + LUKS encryption, nightly and have for years. It works.

  5. #5
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,689
    Distro
    Lubuntu 18.04 Bionic Beaver

    Re: Please help with understanding, and changing, swap: LUKS, LVM

    Quote Originally Posted by Dennis N View Post
    Swapfile - I understand that you get a swapfile when there is no swap partition found by the installer during installation.
    Thanks. Is it automatic, so that if I get rid of the swap partition, will Ubuntu automatically create a swap file?
    Quote Originally Posted by TheFu View Post
    No. There's no difference, except the swap file has a tiny bit more overhead due to the file system.
    That's good to know.
    Quote Originally Posted by TheFu View Post
    I want the smallest "root" LV I can have.
    Then I want the "home" LV to be sized…
    It looks like the installation has screwed you by allocating all the storage to the root LV.
    The "root" is for the entire system, including /home. They're not separate partitions. I'm happy to stick with that, because when the time comes to upgrade to 22.04, I'll reinstall from scratch rather than upgrading from 20.04 to 22.04. I maintain regular multiple full backups of all of my data, so data loss is not an issue.
    Quote Originally Posted by TheFu View Post
    We need more detailed information…
    Here are my answers.
    Code:
    $ sudo pvs
      PV                          VG       Fmt  Attr PSize    PFree
      /dev/mapper/nvme0n1p3_crypt vgubuntu lvm2 a--  <475.71g    0
    Code:
    $ sudo vgs
      VG       #PV #LV #SN Attr   VSize    VFree
      vgubuntu   1   2   0 wz--n- <475.71g    0
    Code:
    $ sudo lvs
      LV     VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      root   vgubuntu -wi-ao---- 474.75g
      swap_1 vgubuntu -wi-ao---- 980.00m
    Code:
    $ df -hT -x squashfs -x tmpfs -x devtmpfs
    df: /run/user/1000/doc: Operation not permitted
    Filesystem                Type  Size  Used Avail Use% Mounted on
    /dev/mapper/vgubuntu-root ext4  467G  136G  307G  31% /
    /dev/nvme0n1p2            ext4  705M  206M  448M  32% /boot
    /dev/nvme0n1p1            vfat  511M   40M  472M   8% /boot/efi
    Code:
    $ lsblk -e 7 -o name,size,type,fstype,mountpoint
    NAME                    SIZE TYPE  FSTYPE      MOUNTPOINT
    nvme0n1                 477G disk              
    |-nvme0n1p1             512M part  vfat        /boot/efi
    |-nvme0n1p2             732M part  ext4        /boot
    `-nvme0n1p3           475.7G part  crypto_LUKS 
      `-nvme0n1p3_crypt   475.7G crypt LVM2_member 
        |-vgubuntu-root   474.8G lvm   ext4        /
        `-vgubuntu-swap_1   980M lvm   swap        [SWAP]
    Code:
    $ free -hm
                  total        used        free      shared  buff/cache   available
    Mem:           15Gi       2.6Gi       6.1Gi       795Mi       6.6Gi        11Gi
    Swap:         979Mi          0B       979Mi
    Quote Originally Posted by TheFu View Post
    If the install is recent and you plan to use it more than a year, I'd go the more-hassle-but-right way. For a short-term need, I'd go with the easy solution.
    Fairly recent, and I'll go for the "right way" if it's more robust.
    Quote Originally Posted by TheFu View Post
    … reducing an LV is a huge hassle.
    That's OK; I'm happy to delete the swap virtual partition, and increase root into that unused space.
    Quote Originally Posted by TheFu View Post
    … clicking in a file manager might prompt you for access.
    The file manager in Ubuntu is Nautilus, and I don't know how to go about doing that in Nautilus. I'll have to use the command line.
    Quote Originally Posted by TheFu View Post
    BTW, actually doesn't require using a Live CD. You can do this on the running system.
    LVM is mighty flexible! That's good to know.
    Quote Originally Posted by TheFu View Post
    I don't think hibernation works with swapfiles and definitely not inside LVM + LUKS encryption.
    It might not work with swap files, but it definitely can work with a swap partition in LUKS and LVM — I've made it work (albeit that the link is slightly out of date).

  6. #6
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    22,152
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Please help with understanding, and changing, swap: LUKS, LVM

    The "right way": https://ubuntuforums.org/showthread....7#post13883277

    The "easy way" would be to
    1. disable the swap, swapoff
    2. delete the swap LV, lvremove
    3. create a swap file inside the "root" LV filesystem, fallocate
    4. format the swap file into swap, mkswap
    5. lock down the swap file permissions/ownership, 600 root:root, then
    6. fix the fstab to use the swap file, removing the swap LV - sudoedit

    I think this is lazy, wrong, and poor storage architecture.

    Of course, there are different opinions on all of this.
    Last edited by TheFu; November 30th, 2020 at 11:12 PM. Reason: added permissions/owner stuff.

  7. #7
    Join Date
    Jun 2019
    Location
    Dirndl-land
    Beans
    634
    Distro
    Lubuntu 20.04 Focal Fossa

    Re: Please help with understanding, and changing, swap: LUKS, LVM

    Quote Originally Posted by TheFu View Post
    I don't think hibernation works with swapfiles
    I've never been able to get hibernate to work with a swapfile, but it works perfectly with a swap partition. I'm aware that the *ubuntu developers disencourage hibernate and have it disabled as default.

  8. #8
    Join Date
    Nov 2020
    Beans
    1

    Re: Please help with understanding, and changing, swap: LUKS, LVM

    there's no difference, except the swap file has a tiny bit more overhead due to the file system.

  9. #9
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,689
    Distro
    Lubuntu 18.04 Bionic Beaver

    Re: Please help with understanding, and changing, swap: LUKS, LVM

    Quote Originally Posted by TheFu View Post
    That's probably overkill for me. I'll stick with one partition for my root and home.
    Quote Originally Posted by TheFu View Post
    The "easy way" would be to…
    Thank you. I'm going to go this way, partly because it matches my needs, partly because it's safer, even if it's "lazy, wrong, and poor storage architecture"

    I realise that this means going without hibernate, and that's OK, I'll live with it.
    Quote Originally Posted by ml9104 View Post
    I've never been able to get hibernate to work with a swapfile, but it works perfectly with a swap partition. I'm aware that the *ubuntu developers disencourage hibernate and have it disabled as default.
    Is it discouraged because of the complications with a swap file? Or is there a fundamental reason behind the thinking?

  10. #10
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,689
    Distro
    Lubuntu 18.04 Bionic Beaver

    Re: Please help with understanding, and changing, swap: LUKS, LVM

    I have gone through the process.

    Here are my results.
    Quote Originally Posted by TheFu View Post
    disable the swap, swapoff
    Code:
    $ sudo swapoff --all
    Quote Originally Posted by TheFu View Post
    fix the fstab … removing the swap LV - sudoedit
    Edited /etc/fstab to remove the swap.
    Quote Originally Posted by TheFu View Post
    delete the swap LV, lvremove
    Code:
    $ sudo lvremove vgubuntu/swap_1
    Extend the root partition:
    Code:
    $ sudo lvscan
      ACTIVE            '/dev/vgubuntu/root' [474.75 GiB] inherit
    
    $ sudo lvextend --extents=100%FREE --resizefs vgubuntu/root
      New size given (245 extents) not larger than existing size (121536 extents)
    
    $ sudo lvscan
      ACTIVE            '/dev/vgubuntu/root' [474.75 GiB] inherit
    As you can see, the root partition wasn't extended, so I now have a little wasted space on the disk.

    Why is this, do you know? Did I enter the command incorrectly?
    Quote Originally Posted by TheFu View Post
    create a swap file inside the "root" LV filesystem, fallocate
    Code:
    $ sudo fallocate --length=4g /swapfile
    Quote Originally Posted by TheFu View Post
    lock down the swap file permissions/ownership, 600 root:root
    Code:
    $ sudo chmod go-r /swapfile
    Quote Originally Posted by TheFu View Post
    format the swap file into swap, mkswap
    Code:
    $ sudo mkswap --label=swap /swapfile
    sudo swapon /swapfile

    Quote Originally Posted by TheFu View Post
    fix the fstab to use the swap file … - sudoedit
    Code:
    /swapfile swap swap sw 0 0
    I've restarted my computer and it seems to work…

    BUT…

    See my next post.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •