How to shrink Ubuntu LVM logical and physical volumes?
Solution 1
These are all the steps required to resize a LVM or LVM2 partition -
sudo lvresize --verbose --resizefs -L -150G /dev/ubuntu/root
sudo pvresize --setphysicalvolumesize {any size here} /dev/sda5
/dev/sda5: cannot resize to xxxxx extents as later ones are allocated.
You have to rearrange the unallocated space at the end of the LVM. That means after root and swap_1 partition. So, you need to move that free space using the command below
pvs -v --segments /dev/sda5
This will show the output like below
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g 0 xxx root 0 linear /dev/sda:0-xxx
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g xxx nnn 0 free
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g yyy zzz swap 0 linear /dev/sda5:yyy-zzz
Now use the command below to remove external fragmentation -
sudo pvmove --alloc anywhere /dev/sda5:yyy-zzz
Now let us see how it goes -
pvs -v --segments /dev/sda5
This will show the output like below
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g 0 xxx root 0 linear /dev/sda:0-xxx
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g xxx nnn swap 0 linear /dev/sda5:xxx-yyy(-1)
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g yyy zzz 0 free
After that use the gparted and resize the LVM to maximum used area and rest will be in unallocated space. Enjoy...
Solution 2
I'd, now that you've got the smaller root LV, lvremove
the swap LV and recreate it. This should force it to be directly appended to the root LV in terms of position. Once that is done you should be able to pvresize
the physical volume. Then it's the scary part in editing the partition table (fdisk
) to downsize the partition to the PV's new size, thereby freeing up space for the Windows installer to use.
For the downsizing of the partition I'd recommend to go slightly larger, by a few MB, than the PV reports it's size as and upsize the PV and root LV to fill the free extents. That way you can be sure you're A) using all of the partition's size for LVM and not losing space by making it inaccessible, and B) not cutting off vital data from the PV by misinterpreting binary KiB/MiB/GiB for decimal KB/MB/GB or vice versa during the fdisk operation.
The new swap, if you create a new one to ensure it's appended to the root LV as I suggest above, will have a different UUID and therefore your /etc/fstab will not match. Once the resizing is all finished, you will need to mount /dev/ubuntu/root /mnt
edit /mnt**/etc/fstab**.
In the fstab file you need to find the swap entry similar to below and update the hexidecimal after UUID= with the value reported by blkid
.
Steps for blkid
on my system are as follows:
run
blkid /dev/ubuntu/swap_1
which outputs
/dev/ubuntu/swap_1: UUID="9e99b37a-38af-4987-85eb-92048abd9825" TYPE="swap"
- copy the UUID value
9e99b37a-38af-4987-85eb-92048abd9825
edit /etc/fstab from your root LV and insert the UUID value in place of the old UUID.
find
UUID=**old-uuid** none swap sw 0 0
replace your new UUID in place of the old-uuid
UUID=9e99b37a-38af-4987-85eb-92048abd9825 none swap sw 0 0
alternatively, you could replace the UUID mechanism and use
/dev/ubuntu/swap_1
, e.g./dev/ubuntu/swap_1 none swap sw 0 0
Solution 3
Use KVPM from Software Center. I installed Ubuntu 14.10 LTS using entire disk one huge LVM (I know what's the point of one huge LVM) but wanted to add another two LVM's for another OS and a data drive. I used KVPM, it says KDE partition manager in description but it works great on 14.04. Here are the steps.
- Download 14.04LTS and make a bootable USB.
- Boot from USB, using TRY UBUNTU option so you are running on USB stick.
- Install KVPM from Software Center.
Run KVPM from the terminal of your choice,
sudo kvpm
- if you just runkvpm
, the live CD will give you an error and hang.Once, kvpm has opened the GUI, go to the tab labelled some_drive-vg
- Highlight the volume to be resized and right click for options, choose reduce volume.
- You can relabel the LVM at the same time you enter the new size.
- Follow the prompts to OK the change and voila, you will have unallocated free space.
- You can make as many new LVMs as you need with the available space. My display showed bight green unallocated space, simply right click the green space, you only option will be "Create new logical volume".
It took me longer to write this than it did to execute the whole process.
By the way, the /root partition I resized functions perfectly after all the changes. Just reboot to your HDD in your chosen OS and enjoy your new found volumes.
Related videos on Youtube
Hailwood
I could tell you all about me... but I'd prefer to let my work do the talking for me!
Updated on September 18, 2022Comments
-
Hailwood over 1 year
I have installed Ubuntu 12.10 as the only OS on my system.
I told Ubuntu to overwrite everything as I was in a hurry, hence I only have the two partitions Ubuntu creates, the Boot Partition of a couple a hundred MB, and then everything else.
The main partition is about 600GB,
I wish to resize this to 450GB, so I can install Windows 8 as a dual boot.
The issue is that I cannot work out how to shrink the partition, normally it's fine from the Gparted (gparted-live-0.14.1-6-i486.iso) live iso, Simply resize and you are good to go.
But this time Ubuntu has been installed with LVM.
So, the Ubuntu partition has a lock icon next to it. This prevents me from doing anything, I can select the partition, and there is an option for "deactivate" which If I am correct I believe is deactivating swap? If I do that I can manipulate the partition.
I am asking here if this is the correct thing to do before I bust my system.
- Boot from the live gparted iso.
- Select the Ubuntu partition.
- "Deactivate" the partition.
- Resize the partition.
- "Activate" the partition.
- Apply.
- Profit???
Is my thinking correct?
Edit #1
So, after some extended chat I came down to this command
sudo lvresize --verbose --resizefs -L -150G /dev/ubuntu/root
Which successfully shrunk the logical partition.
But Now I have the hard part.
Now I need to shrink the Physical Partition down so I can create a new Physical partition with the space to install Windows 8.
So I thought I could run
pvresize --setphysicalvolumesize {any size here} /dev/sda5
But I get back:
/dev/sda5: cannot resize to xxxxx extents as later ones are allocated.
Which I believe is due to the swap due to this image:
, so what's my next step?
$: lvdisplay --- Logical volume --- LV Path /dev/ubuntu/root LV Name root VG Name ubuntu LV UUID G7Z3dA-xska-8QpO-Y5oD-aIVO-D8l1-22s6dO LV Write Access read/write LV Creation host, time ubuntu, 2013-01-29 22:06:02 +1300 LV Status available # open 1 LV Size 540.42 GiB Current LE 138348 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 252:0 --- Logical volume --- LV Path /dev/ubuntu/swap_1 LV Name swap_1 VG Name ubuntu LV UUID 14qpRy-H5jl-dTAq-CVle-k7Bz-95Rm-Y46zOs LV Write Access read/write LV Creation host, time ubuntu, 2013-01-29 22:06:04 +1300 LV Status available # open 2 LV Size 7.97 GiB Current LE 2041 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 252:1
Additional Information Before Edit #1
Note that the above shot is taken from the Gparted version in the Ubuntu Repositories, not the latest versionmatthew@play:~$ sudo fdisk -l
Disk /dev/sda: 750.2 GB, 750156374016 bytes 255 heads, 63 sectors/track, 91201 cylinders, total 1465149168 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000c5bec Device Boot Start End Blocks Id System /dev/sda1 * 2048 499711 248832 83 Linux /dev/sda2 501758 1465147391 732322817 5 Extended /dev/sda5 501760 1465147391 732322816 8e Linux LVM Disk /dev/mapper/ubuntu-root: 741.3 GB, 741334843392 bytes 255 heads, 63 sectors/track, 90128 cylinders, total 1447919616 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/mapper/ubuntu-root doesn't contain a valid partition table Disk /dev/mapper/ubuntu-swap_1: 8560 MB, 8560574464 bytes 255 heads, 63 sectors/track, 1040 cylinders, total 16719872 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/mapper/ubuntu-swap_1 doesn't contain a valid partition table
matthew@play:~$ sudo lvs
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert root ubuntu -wi-ao-- 690.42g swap_1 ubuntu -wi-ao-- 7.97g
matthew@play:~$ sudo vgs
VG #PV #LV #SN Attr VSize VFree ubuntu 1 2 0 wz--n- 698.39g 0
matthew@play:~$ sudo pvs
PV VG Fmt Attr PSize PFree /dev/sda5 ubuntu lvm2 a-- 698.39g 0
-
gertvdijk over 11 yearsMake sure you run a recent GParted version (only recently gained LVM support - you are, so merely a note for other readers). And deactivating is, I think, deactivating the volume group (
vgchange -an myvgname
) which may be needed if you are going to change the physical volume. :) Oh, it's almost an answer. -
Hailwood over 11 years@gertvdijk Ping me in chat if you need any quick info.
-
Jamess about 11 yearsWas this resolved ? I am looking for reducing the physical partition to install another version of OS side by side.
-
Jamess about 11 yearsThis procedure along with edit's in question helped me to resize the partition. I could not find lvdelete command (12.04) and used lvremove to remove the swap partition. The pvresize worked like a charm. Then I used gparted live CD to resize the the partion to lower size. completed the operation and unallocated size got created. I would suggest the asker should create a complete answer.
-
Jamess about 11 yearsI incidently was not using UUID (did I change it, I dont remember), may systyem never used UUID for swap. So I could skip UUID editing steps.
-
Jamess about 11 yearsHow to recreate a swap lv 1. lvcreate -L 5G -n /dev/vg0/swap_1 2. mkswap /dev/vg0/swap_1, swapon -s /dev/vg0/swap_1, then validate the /etc/fstab entries and if needed change UUID.
-
Dani Llewellyn over 9 yearsThanks for the mention of
lvdelete
being incorrect in my answer. I've now fixed the text to cite the properlvremove
name.