You are not logged in.

#1 2018-01-15 03:15:05

GiorgianB
Member
Registered: 2016-04-17
Posts: 9

[SOLVED] LUKS on LVM: How to safely shrink a partition?

After realizing that my root partition was way to big and my /home partition way to small, I decided to go about resizing them. "Easy!", I thought. "With LVM it should be a breeze!". Well it turns out it was not so. I can't figure out how to resize an LVM partition with a LUKS partition in it. I had originally followed the instructions from the arch wiki here to create a LUKS on LVM setup. Currently it looks like this:

    NAME             MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
    sda                8:0    0 119,2G  0 disk  
    ├─sda1             8:1    0   512M  0 part  /boot
    └─sda2             8:2    0 118,8G  0 part  
      ├─lvm-lvroot   254:0    0    40G  0 lvm   
      │ └─cryptoroot 254:5    0    40G  0 crypt /
      ├─lvm-var      254:1    0    20G  0 lvm   
      │ └─var        254:6    0    20G  0 crypt /var
      ├─lvm-tmp      254:2    0    20G  0 lvm   
      │ └─tmp        254:7    0    20G  0 crypt /tmp
      ├─lvm-swap     254:3    0     8G  0 lvm   
      │ └─swap       254:8    0     8G  0 crypt [SWAP]
      └─lvm-home     254:4    0  30,8G  0 lvm   
        └─home       254:9    0  30,8G  0 crypt /home
    sr0               11:0    1 496,7M  0 rom   

where lvm-lvroot is the root partition and lvm-home is the home partition. Both are separate LUKS encrypted partitions also. How do I safely shrink lvm-lvroot without losing any data, and grow lvm-home?

Thanks!

Last edited by GiorgianB (2018-01-15 16:20:35)

Offline

#2 2018-01-15 08:14:22

chicha
Member
From: France
Registered: 2007-04-20
Posts: 271

Re: [SOLVED] LUKS on LVM: How to safely shrink a partition?

Hello,

When you have a stacked filesystem (LVM + LUKS + EXT, ...) you grow from the outer block to the inner block and you shrink from the inner block to the outer block.

In your case (I will suppose your partitions are ext4 formatted) :

  1. Before any operation do a backup of your system to an external disk

  2. You shrink lvm-root at ext4 level

  3. You shrink lvm-root at cryptsetup level

  4. You shrink lvm-root at lvm logical level

  5. You grow lvm-home at lvm logical level

  6. You grow lvm-home at cryptsetup level

  7. You grow lvm-home at ext4 level

The link provided for cryptsetup resize is for an LVM-on-LUKS setup. It is just for you to see which command to use for cryptsetup.
Also, unless you have very good reasons to do it, the next time I would advise to setup LVM on LUKS instead of LUKS on LVM: You would not need to resize at cryptsetup level in such case (and you have only one password to setup for your root filesystem).

Last read the man pages of cryptsetup, resize2fs and lvresize to make sure you understand what you are doing and adapt the examples on the wiki with your real case.

Offline

#3 2018-01-15 08:56:37

frostschutz
Member
Registered: 2013-11-15
Posts: 1,658

Re: [SOLVED] LUKS on LVM: How to safely shrink a partition?

One thing to take particular note of is that the LUKS header uses 2MiB or so.

So with LUKS on top of LVM, if you shrink the filesystem to 20G, and shrink the LUKS to 20G, the LV needs to be 20G + 2MiB.

If you'd rather have the LV to be 20G even, you need to shrink both filesystem and LUKS to 20G - 2 MiB.

Another approach is to shrink the filesystem considerably more than necessary (e.g. to 15G), then shrink the LV without worries to 20G, cryptsetup resize (or close/open), and then finally grow the filesystem again to whatever size it came out as. However if you know what you're doing, and don't confuse GiB (1024) vs. GB (1000) units, these extra steps are unnecessary.

----

In your case one thing that stands out is that you have given 20G to /tmp which should usually not be larger than a few kilobytes, a lot of people just go with tmpfs for /tmp these days, so you could simply take those 20G as a whole to add to your home. That is unless you know you have an application that wantonly puts gigs of trash in /tmp.

Last edited by frostschutz (2018-01-15 09:05:45)

Offline

#4 2018-01-15 14:23:12

GiorgianB
Member
Registered: 2016-04-17
Posts: 9

Re: [SOLVED] LUKS on LVM: How to safely shrink a partition?

Thanks. What I was doing wrong was I was doing cryptsetup resize THEN resize2fs, instead of the other way around. In retrospect your way makes much more sense. Thanks!

Offline

#5 2018-01-15 15:03:15

chicha
Member
From: France
Registered: 2007-04-20
Posts: 271

Re: [SOLVED] LUKS on LVM: How to safely shrink a partition?

Please mark your post as [SOLVED] once actually solved.

Offline

Board footer

Powered by FluxBB