Ubuntu – LVM Hard disk partitioning after Ubuntu installation

lvmpartitioningsystem-installation

I'm a new user of Ubuntu and when I have install it, it takes the whole hard-disk space. When I tried to divide the hard-disk using Gparted partition Editor from external Ubuntu on USB, I found what you can see on the following screenshot. I can't divide the hard disk.

this photo

Can anybody helps me?

Best Answer

As we can see on the screen shot from the question you are currently using LVM on the partition /dev/sda5. Gparted have limited support of LVM and this is the reason to show it as full while in fact it is not full.

If this is a new installation the easiest way is to backup your user's files, reinstall the OS and split the disk during the installation process - but this is another question. Otherwise backup your user's files and follow the next steps on your own risk.

Here is short manual how to shrink this partition step-by-step. I'm assuming you are running Live Ubuntu from an installation CD/USB and you want to resize the existing Ubuntu installation. For the current example I'm using virtual machine (powered by VMWare) and some outputs could be slightly different from the real situation.


Investigate your LVM setup

There are two basic terms:

  • VG - volume group - that you can imagine as equivalent of the entire free space (disk drive) within LVM.

  • LV - logical volume - that you can imagine as equivalent of partition.

Open new terminal window and execute the command sudo lvs that will output all VGs and their LVs. If you've used the automatic installation process of Ubuntu the output should be as the follow.

$ sudo lvs
  LV     VG        Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root   ubuntu-vg -wi-a----- 21,52g                                                    
  swap_1 ubuntu-vg -wi-a-----  8,00g  

Here we have VG called ubuntu-vg, one LV called swap_1 and another one called root, that we want to resize.

To output the current characteristics of the root LV you can use the command sudo lvdisplay /dev/ubuntu-vg/root. To check the free space on this LV you can use df -h /dev/ubuntu-vg/root, but first you should mount it:

$ sudo mount /dev/ubuntu-vg/root /mnt
$ df -h /dev/ubuntu-vg/root
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-root   22G  5,0G   16G  25% /mnt

We can see there is 16G free space. Here is what Gparted shows for this installation at this point (it is identical with your case):

enter image description here


Shrink the partition

The main source of this part is the article How to Extend/Reduce LVM’s on TechMint. The first advice given there is: Before starting, it is always good to backup the data, so that it will not be a headache if something goes wrong.

Here we will reduce the root logical volume, then will move swap_1 into the beginning of the free space within the volume group and then we will resize /dev/sda5.

1. Mount the file system and defragment it:

sudo mount /dev/ubuntu-vg/root /mnt
sudo e4defrag /mnt

2. Unmount the file system for reducing (check if it is unmounted by the command: df -h):

umount /dev/ubuntu-vg/root 

3. Check for the file-system error using following command (the test must pass in all 5 steps; 0.1-1% of non-contiguous files will give us good chance to reduce the file-system):

sudo e2fsck -ff /dev/ubuntu-vg/root

4. Reduce the file-system with 5G (for this example) and Reduce the LV with 5GB:

sudo resize2fs /dev/ubuntu-vg/root 17G  # 22 - 5 = 17
sudo lvreduce -L -5G /dev/ubuntu-vg/root

5. Resize the file-system back and check for the file-system errors:

sudo resize2fs /dev/ubuntu-vg/root
sudo e2fsck -ff /dev/ubuntu-vg/root

If there is any error that means the file-system is corrupted and maybe we should reinstall and use our backup. If there is no error the process should be successfully finished. Note: At this point you could create another LV.

6. Move the free space out of VG. The command pvs could show us where the free space is located within the partition /dev/sda5. In this example the swap is to the end, so we will delete and create it again (using its PE size), thus it will be moved into the begging of the free space.

$ sudo pvs -v --segments /dev/sda5
    Using physical volume(s) on command line.
    Wiping cache of LVM-capable devices
  PV         VG        Fmt  Attr PSize  PFree Start SSize LV     Start Type   PE Ranges          
  /dev/sda5  ubuntu-vg lvm2 a--  29.52g 5.00g     0  4229 root       0 linear /dev/sda5:0-4228   
  /dev/sda5  ubuntu-vg lvm2 a--  29.52g 5.00g  4229  1280            0 free                      
  /dev/sda5  ubuntu-vg lvm2 a--  29.52g 5.00g  5509  2048 swap_1     0 linear /dev/sda5:5509-7556

$ sudo lvremove /dev/ubuntu-vg/swap_1
  Do you really want to remove and DISCARD active logical volume swap_1? [y/n]: y
  Logical volume "swap_1" successfully removed

$ sudo lvcreate -n swap_1 -l 2048 ubuntu-vg
  Logical volume "swap_1" created.

$ sudo pvs -v --segments /dev/sda5
    Using physical volume(s) on command line.
    Wiping cache of LVM-capable devices
  PV         VG        Fmt  Attr PSize  PFree Start SSize LV     Start Type   PE Ranges          
  /dev/sda5  ubuntu-vg lvm2 a--  29.52g 5.00g     0  4229 root       0 linear /dev/sda5:0-4228   
  /dev/sda5  ubuntu-vg lvm2 a--  29.52g 5.00g  4229  2048 swap_1     0 linear /dev/sda5:4229-6276
  /dev/sda5  ubuntu-vg lvm2 a--  29.52g 5.00g  6277  1280            0 free                      

$ sudo pvresize --setphysicalvolumesize 25G /dev/sda5
  Physical volume "/dev/sda5" changed

7. Now you can use some tool like Gparted to resize the partition /dev/sda5 and create a new one or leave the space unallocated to use it for the another OS installation.

enter image description here

Note the small gap of empty space that we commit due to incorrect calculations. We can make more precise calculations (honestly I've lost my self within GB, G and GiB) or we can add it to some of the LVs by a command as this:

sudo lvresize -l +100%FREE /dev/ubuntu-vg/root
sudo resize2fs /dev/ubuntu-vg/root

Use sudo pvs -v --segments /dev/sda5 to investigate the new state.

8. Now you can reboot the system. Within the example I created one NTFS partition - /dev/sda6, shown on the next image.

enter image description here

9. The final step is to enable the swap (as it is show on the picture above):

sudo mkswap /dev/ubuntu-vg/swap_1 # we could do that in the previous step 7
sudo swapon /dev/ubuntu-vg/swap_1 # or reboot the system

References:

Related Question