Back
4 min read

Repartitioning LUKS-crypted LVM on Debian

This is crash howto, so few important notes are needed:

  • You may and you probably will loose your data, so backup is highly recommended.
  • I’m assuming you have all needed tools to provide actions described below.
  • I’m also assuming, that you have basic knowledge of Linux command line and some of it’s tools, like fdisk for example.
  • This howto isn’t about creating encrypted LVM – it assumes you already have one.

One side note: it may be possible, to provide all of these actions using software with GUI. It’s up to you to try for example tools like palimpsestand/or system-config-lvm.

Let’s look at the starting situation:

  1. There’s one hard disk: /dev/sda (~320G).
  2. There was Windows XP installed on 80G partition (NTFS).
  3. There was later on added Debian, installed on following partitions:
    |-/boot, ext2, ~512M  
    |-LUKS-crypted LVM  
       |-/root, ext4, ~10G  
       |-swap, ~4G  
       |-/home, ext4, ~226G

This dual-boot was working swell, but I decided, that I don’t  need Windows partition anymore, as I can use KVM to use it (when needed)  as a guest. It turned out, that it would be a good idea to add some  free space to /rootpartition and rest of the freed space add to /home  partition (it also may be a good idea, to create dedicated partition for  KVM). Let’s play.

$ sudo fdisk -cu /dev/sda
(inside fdisk): p    (to list new partition table)
Dysk /dev/sda: 320.1 GB, bajtów: 320072933376
głowic: 255, sektorów/ścieżkę: 63, cylindrów: 38913, w sumie sektorów: 625142448
Jednostka = sektorów, czyli 1 * 512 = 512 bajtów
Rozmiar sektora (logiczny/fizyczny) w bajtach: 512 / 512
Rozmiar we/wy (minimalny/optymalny) w bajtach: 512 / 512
Identyfikator dysku: 0x64656469

Urządzenie Rozruch   Początek      Koniec   Bloków   ID  System
/dev/sda1   *          63   165427919    82713928+   7  HPFS/NTFS
/dev/sda2       165429248   166404095      487424   83  Linux
/dev/sda3       166406142   625141759   229367809    5  Rozszerzona
/dev/sda5       166406144   625141759   229367808   83  Linux

First, we need to remove Windows NTFS partition:

(inside fdisk): d    (to delete partition)
(inside fdisk): 1    (to choose which partition is to be removed)
(inside fdisk): p    (to list new partition table)

Dysk /dev/sda: 320.1 GB, bajtów: 320072933376
głowic: 255, sektorów/ścieżkę: 63, cylindrów: 38913, w sumie sektorów: 625142448
Jednostka = sektorów, czyli 1 * 512 = 512 bajtów
Rozmiar sektora (logiczny/fizyczny) w bajtach: 512 / 512
Rozmiar we/wy (minimalny/optymalny) w bajtach: 512 / 512
Identyfikator dysku: 0x64656469

Urządzenie Rozruch   Początek      Koniec   Bloków   ID  System
/dev/sda2       165429248   166404095      487424   83  Linux
/dev/sda3       166406142   625141759   229367809    5  Rozszerzona
/dev/sda5       166406144   625141759   229367808   83  Linux

OK, now save changes, exit and reboot machine:

(inside fdisk): w    (to write changes and quit fdisk)

$ sudo shutdown -r now

After reboot, launch fdisk again and create Linux LVM partition:

$ sudo fdisk -cu /dev/sda
(inside fdisk): n    (to create new partition)
(inside fdisk): 1    (to define where to place her)
(inside fdisk): 2048    (default)
(inside fdisk): 165429247    (default)
(inside fdisk): p    (to list new partition table)

Dysk /dev/sda: 320.1 GB, bajtów: 320072933376
głowic: 255, sektorów/ścieżkę: 63, cylindrów: 38913, w sumie sektorów: 625142448
Jednostka = sektorów, czyli 1 * 512 = 512 bajtów
Rozmiar sektora (logiczny/fizyczny) w bajtach: 512 / 512
Rozmiar we/wy (minimalny/optymalny) w bajtach: 512 / 512
Identyfikator dysku: 0x64656469

Urządzenie Rozruch   Początek      Koniec   Bloków   ID  System
/dev/sda1            2048   165429247    82713600   83  Linux
/dev/sda2       165429248   166404095      487424   83  Linux
/dev/sda3       166406142   625141759   229367809    5  Rozszerzona
/dev/sda5       166406144   625141759   229367808   83  Linux

(inside fdisk): t    (to change partition type)
(inside fdisk): 1    (to choose partition which is to be changed)
(inside fdisk): L    (to list available partition types)
(inside fdisk): 8e    (to choose Linux LVM)
(inside fdisk): p    (to list new partition table)

Dysk /dev/sda: 320.1 GB, bajtów: 320072933376
głowic: 255, sektorów/ścieżkę: 63, cylindrów: 38913, w sumie sektorów: 625142448
Jednostka = sektorów, czyli 1 * 512 = 512 bajtów
Rozmiar sektora (logiczny/fizyczny) w bajtach: 512 / 512
Rozmiar we/wy (minimalny/optymalny) w bajtach: 512 / 512
Identyfikator dysku: 0x64656469

Urządzenie Rozruch   Początek      Koniec   Bloków   ID  System
/dev/sda1            2048   165429247    82713600   8e  Linux LVM
/dev/sda2       165429248   166404095      487424   83  Linux
/dev/sda3       166406142   625141759   229367809    5  Rozszerzona
/dev/sda5       166406144   625141759   229367808   83  Linux

(inside fdisk): w    (to write changes and quit fdisk)

And reboot:

$ sudo shutdown -r now

Now we need to use pvcreate to initialize newly created partition to use LVM:

$ sudo pvcreate /dev/sda1
  Physical volume "/dev/sda1" successfully created

It’s time to add this physical volume to our LVM group, so we can use  free space (~80G) from it to add it to our logical volumes:

$ sudo vgextend foo /dev/sda1
  Volume group "foo" successfully extended

Where “foo” is a name of your LVM group. You may list all your logical volumes (with such informations like size etc.) using lvdisplay command. You can launch this command without specifying concrete  logical volume and it will print out informations about all of them.

Alright – it’s time to make use of this free space we gained from removing Windows:

$ sudo lvextend -L20G /dev/foo/root
  Extending logical volume root to 20,00 GiB
  Logical volume root successfully resized

Please notice, that -L command haven’t got + before 20G – it  means, that logical volume /dev/foo/root will be resized to exactly 20 GiB (20 GiB won’t be added to /dev/foo/root, cause it already had 10  GiB).

Now file system need be resized to follow new physical size:

$ sudo resize2fs /dev/foo/root

It will be done on-line, which means, you can use your system normally and you won’t have to reboot your machine after changes.

For operation described above I’ve used approx. 10 GiB from ~80 GiB  that were freed after WinXP removal. Now I want to add free space that  is left to /dev/foo/home logical volume:

$ sudo lvextend -l+100%FREE /dev/foo/home

Now file system need to follow new physical size:

$ sudo resize2fs /dev/foo/home

You may use pvdisplay, vgdisplay, lvdisplay and df (with -h flag, for example) to check whether the partitions were changed as you wanted to.

Further reading: