Why?
As I frequently reinstall my OS (for various reasons) but in the mean time I still want to be able to access my old OS for daily use, I do a lot resizing of filesystems and partitioning of my hard disk. When the time has come to use the new OS installation as a daily driver, I like to reclaim my old OS space so I can have all the space available.
Now there can be roughly two situations to be in: either your old OS is physically after your new OS or vice versa. In the first situation the solution is simple: remove your old partition, extend your current partition to the end and resize2fs your existing partition to fill up in the remaining space. The latter is more cumbersome. If you delete the old partition, you are left with free space before your current filesystem. If you were to resize2fs your current filesystem to use up the free space in the beginning, it would have to move each and every file as the filesystem can only expand at the end. This problem can be solved with the use of LVM which introduces another layer in between the physical blocks on your disk and the blocks as used in the filesystem.
So the first time I came across LVM, I wondered if I can convert my existing filesystem into an LVM partition. As I couldn’t find any (working) solutions on the internet, I decided to work out my own solution. Was this faster than just backing up all my data? No way near. In the time it took to work this all out I could have reinstalled my OS dozens of times. And you only have to apply this once, because once you switched to LVM you will never have this problem again. Did I also mention it is extremely dangerous to mess with your filesystem like this?
Before we get started
Before we get started, I want you to know that messing with your filesystem is extremely dangerous. You should have a backup of all your data because one miscalculation will mess up your filesystem.
Also I assume you know how filesystems and partitions work and how you can manipulate them on a low level. You also should be well informed what LVM is, how it works, what its benefits are and have a basic understanding of the technical details. If you are wondering if your experience meets these requirements, this article probably isn’t for you.
Also needless to say, this operation can only be done when none of the partitions is mounted, so a live media is needed. I use Ubuntu 18.10 in this example as a root user.
Let’s get started
In this example I will use a 10 GiB disk partitioned as follows:
Device Start End Sectors Size Type
/dev/sda1 2048 976895 974848 476M EFI System
/dev/sda2 976896 2930687 1953792 954M Linux filesystem
/dev/sda3 2930688 20969471 18038784 8,6G Linux filesystem
with ext4 filesystems on sda2 and sda3, both utilizing the full partition. sda3 contains the root filesystem.
I recommend you to check all the concerning filesystems with fsck first. Also make sure you have the needed LVM utilities installed on your existing installation (lvm2 in this example).
We need to calculate the size our new Physical Volume will be. This PV will eventually encapsulate the Logical Volume which will contain our existing filesystem. I will use Physical Extents of size 4 MiB as that seems to be the standard nowadays. In my case sda3 is 18038784 sectors big. This results in 8808 MiB of space. So with 4 MiB PE’s we have exactly 2202 PE’s.
However if your calculations return a non-integer of PE’s, you have to round it down, calculate the number of sectors needed and resize your filesystem accordingly. For example if your partition (and filesystem) is 18038776 sectors big, this will result in 2201,99… 4MiB PE’s. So effectively only 2201 PE’s fit in this partition and you have to resize your filesystem (not partition!) to 18030592 sectors. I assume you know how to do this with resize2fs.
A PV needs exactly 1 MiB (2048 sectors) in front of the LV to store its metadata. As we don’t have that space, we have to free it at the end from sda2. First decrease the filesystem on sda2 by 2048 sectors. Then decrease the size of sda2 by 2048 sectors as well so we have enough unpartioned space before sda3.
resize2fs /dev/sda2 1951744s
fdisk /dev/sda
> d
> 2 (delete partition 2)
> n
> 2 (recreate partition 2)
> 976896 (first sector is the same)
> +1951743 (size is total sectors-1)
> n (keep existing filesystem signature)
> w (write changes to disk)
Now extend sda3 to contain these extra 2048 sectors.
fdisk /dev/sda
> d
> 3 (delete partition 3)
> n
> 3 (recreate partition 3)
> 2928640 (first sector is previously first sector-2048)
> 20969471 (last sector stays the same)
> w (write changes to disk)
Make a PV on sda3 with only 1 copy of the metadata in the beginning of the PV.
pvcreate -M2 --metadatacopies 1 /dev/sda3
Make a Volume Group with PE size 4MiB with the name vg0 containing only sda3.
vgcreate -M2 -s 4M vg0 /dev/sda3
Make an LV with 2202 PE’s and without zero’ing any existing signature in the LV.
lvcreate -l2202 -Zn vg0
Finally check if the filesystem is alright.
fsck -f /dev/mapper/vg0-lvol0
To boot back into the system we have to repair GRUB. This can be different dependent on which OS you run and in which mode (EFI or legacy).
mkdir rootmount
mount /dev/mapper/vg0-lvol0 rootmount/
mount /dev/sda1 rootmount/boot/efi
mount --bind /dev rootmount/dev
mount --bind /dev/pts rootmount/dev/pts
mount --bind /proc rootmount/proc
mount --bind /sys rootmount/sys
sudo chroot rootmount/
grub-install /dev/sda
update-grub
Although not strictly necessary, it is also good practice to change the partition type of your newly create LVM partition to ‘8E’ (Linux LVM).
Now you should be able to reboot back into your existing installation while now being on an Logical Volume.
great info I already have a full 6tb hard drive with ext4 data (no OS) I am trying to change it to LVM …
At no point here in the tutorial did you ever show how the filesystem looked like.
You show how it is in the beginning, and makes us more and more blind as we follow along.
Exactly how does your filesystem look like here at this area in the text “Now extend sda3 to contain these extra 2048 sectors.”.
I understand the topic is hard, but please make it more easily understandable by everyone!
Assuming you mean partition layout instead of filesystem, I agree some overviews of the partition layout in between might clear this up. However all information is there to calculate it yourself.
The partition layout at the point in the text looks like this:
Device Start End Sectors Size Type
/dev/sda1 2048 976895 974848 476M EFI System
/dev/sda2 976896 2928639 1951744 953M Linux filesystem
/dev/sda3 2928640 20969471 18040832 8,6G Linux filesystem
Up until there it’s just basic filesystem and partition resizing. My assumption at the time of writing this article was that if you are looking for this specific use case, you are already familiar with this.
It wasn’t specifically a question, but more of an example, because there is more info that is barely talked about.
What do you mean by: “keep existing filesystem signature” Do you mean to keep it as Linux filesystem / EXT4 after resizing the partition?
Is it meant to be 2048 sectors in front AND after the main partition?
In my case, I was asked to delete the signature at this part “lvcreate -l2202 -Zn vg0”.
I’ll give it a try again in a few hours.
When deleting and recreating the partition (so resizing in fact), fdisk recognizes there is already a valid partition at the startsector and asks to remove the filesystem signature. You have to answer no to that question if you don’t want to damage the partition in front of the one you’re working on.
No, only 2048 sectors are needed in front of the main partition. When creating an PV with 1 metadata copy, it places that metadata only in the first 2048 sectors.
When you then create an LV, it recognizes there is already valid filesystem signature, just like fdisk did. You should not zero that signature because it is your main filesystem signature. Also note that the l2202 argument is for my 2202 PEs. You need to change that to whatever it is in your case.
Ok, so far I see 1 thing I misunderstood, and that was to assign the unallocated space to the main partition in the beginning. I first thought lvcreate would do this for me.
I did as you said to not wipe the ext4, but it won’t let me create a vg0 group
ubuntu@ubuntu:~$ sudo pvcreate -M2 –metadatacopies 1 /dev/sda3
WARNING: ext4 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: n
Aborted wiping of ext4.
1 existing signature left on the device.
ubuntu@ubuntu:~$ sudo vgcreate -M2 -s 4M vg0 /dev/sda3
WARNING: ext4 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: n
Aborted wiping of ext4.
1 existing signature left on the device.
ubuntu@ubuntu:~$ sudo lvcreate -l468697 -Zn vg0
Volume group “vg0” not found
Cannot process volume group vg0
fdisk -l /dev/sda before:
Device Start End Sectors Size Type
/dev/sda1 2048 1050623 1048576 512M EFI System
/dev/sda2 1050624 67457023 66406400 31.7G Linux swap
/dev/sda3 67457024 3907029134 3839572111 1.8T Linux filesystem
fdisk -l /dev/sda now:
Device Start End Sectors Size Type
/dev/sda1 2048 1050623 1048576 512M EFI System
/dev/sda2 1050624 67454975 66404352 31.7G Linux swap
/dev/sda3 67454976 3907022847 3839567872 1.8T Linux filesystem
Do you know what might be wrong here ?
If you can see my mail address, I would be glad to answer you there, or we can keep the replies going to help others coming from duckduckgo
Can you more clearly show your math and how you arrive at the various values? Some simple A divided by B = C or A x B = C kind of math.
Pascal your topic is great. Its perfectly clear for people are already familiar with this.
If people don’t get it , don’t try it.. don’t have skis for it.. and will make mistakes and eventually destroy data!
Somehow, I accidentally made a LV that didn’t extend the full partition, resulting in FSCK throwing an error about a corrupted super block. The solution was
“`
lvextend -l +100%FREE /dev/mapper/vg0-lvol0
“`