How to shrink Ubuntu LVM logical and physical volumes?

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.

  1. Boot from the live gparted iso.
  2. Select the Ubuntu partition.
  3. "Deactivate" the partition.
  4. Resize the partition.
  5. "Activate" the partition.
  6. Apply.
  7. 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.

Gparted New Shot

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:

Darn Swap!, 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

Gparted Screenshot
Note that the above shot is taken from the Gparted version in the Ubuntu Repositories, not the latest version

matthew@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 

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:

  1. run

    blkid /dev/ubuntu/swap_1
    

    which outputs

    /dev/ubuntu/swap_1: UUID="9e99b37a-38af-4987-85eb-92048abd9825" TYPE="swap"
    
  2. copy the UUID value

    9e99b37a-38af-4987-85eb-92048abd9825

  3. 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