I have an Ubuntu 11.10 system with two 500GB disks. The partition tables look like this:

/dev/sda1      primary   465.52GB
/dev/sda2      extended  243.17MB
  -> /dev/sda5 logical   243.14MB
/dev/sdb1      primary   465.76GB

sda1 and sdb1 are in a single LVM physical volume group containing a single logical volume containing a single logical filesystem which is mounted as /. sda5 is mounted as /boot.

The problem comes when I want to upgrade to Ubuntu 12.04, which requires at least 247MB free on /boot. So I need to reduce the size of sda1 so that I can increase the size of sda2 and sda5. How the heck do I do that? I can find how to shrink the logical volume group, but I'm not at all clear on how to clear out the end part of sda1 so that I can reduce the physical volume group. Does pvresize just deal with this automagically? Or is that wild wishful thinking?

I guess the alternatives are to back everything up onto something or other and recreate the thing from scratch or find out whether GRUB2 supports using LVM for /boot.


After shrinking the first partition that holds the first physical volume with pvresize, you need to grow the /boot partition.

In order to extend the /boot partition you would have to move its start point on the first disk. You can accomplish that with parted using the move command.

Then you can resize the partition again with parted and finally grow the filesystem.

  • Yes, I realise that, but there is a logical volume already using that space on sda1 - will pvresize automatically move data out of the way into sdb1? Or do I need to do that manually and, if so, how? – Tom Jun 3 '12 at 13:37
  • If the space at the end of sda1 is used by data, pvresize will refuse to proceed. In that case you will have to use pvmove to move extents from one physical volume to another. – Dmitri Chubarov Jun 3 '12 at 13:46

In 12.04, enabling the LVM and raid modules in GRUB will make it larger than the traditional 62 sectors available for the bootloader. Supposedly most systems these days have partitions aligned to the 1MB mark so there's usually plenty of space. You can use one of the fdisks or parted to check the starting location of the sda1 partition (I know Debian's installer is vague about where everything is going, not sure if Ubuntu's installer is any different). Be sure to check before trying, there seems to be a lot of reports that if there isn't enough space, grub will complain that there's not enough space to work but try anyway and leave the system unbootable.

As an example to see how to use fdisk to tell, here is fdisk /dev/sda on my Debian system that I installed long, long ago: (Use command "u" to show sectors, then "p" to print the table and "q" to quit without changes)

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1              63     1959929      979933+  83  Linux

This shows that in my case, there are only 62 sectors before it. I wouldn't be able to boot straight from LVM.


I've figured this out. Before I start, I'll say that the numbers below may not add up - I've tried to recreate exactly what I did, but I've had to backtrack through the numbers. My advice is to do everything you can in kilobytes, because all the tools agree on how big they are. Be VERY careful using fdisk, that the partition is at least as big as the physical volume it contains, because it won't warn you if you try to do something stupid.

First, I needed to boot from a flash stick (because the root filesystem is in the LV). Then check the filesystem is okay:

e2fsck -f /dev/mapper/root

Then resize the filesystem:

$ df
Filesystem            1K-blocks      Used Available Use% Mounted on
/dev/mapper/root      935093704 472743436 414857916  54% /

$ resize2fs /dev/mapper/root 934093704k

Then resize the logical volume:

$ lvresize -L 934093704K /dev/mapper/root

Check the filesystem is still okay:

$ e2fsck -f /dev/mapper/root

Now have a look at the physical volumes - /dev/sdb1 should have some free space at the end:

$ pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sda1  toad lvm2 a-   465.76g  4.00m
  /dev/sdb1  toad lvm2 a-   465.76g  1.28g

We need to move this free space to the end of /dev/sdb1. Start by seeing how many extents exist on each drive:

$ pvs --segments

  PV         VG   Fmt  Attr PSize   PFree  Start  SSize
  /dev/sda1  toad lvm2 a-   465.76g  4.00m      0 119173
  /dev/sdb1  toad lvm2 a-   465.76g  1.28g      0 119141

Then move some from one to the other: $ pvmove /dev/sda1:118923:119173 /dev/sdb1

$ pvs --segments

  PV         VG   Fmt  Attr PSize   PFree  Start  SSize
  /dev/sda1  toad lvm2 a-   464.55g  1.00g      0 119141
  /dev/sdb1  toad lvm2 a-   465.76g 284.0m      0 119141
  /dev/sdb1  toad lvm2 a-   465.76g 284.0m 119141 119173

Now you can resize the physical volume using pvresize:

$ pvresize --setphysicalvolumesize 487112704k /dev/sda1

Now you can resize the partition using fdisk. A warning here: my partitions were set up so that /dev/sda1 started at sector 63. I had assumed that this was the default and so didn't actually note it down. Then fdisk would only let me recreate the partition starting at sector 2048, and I didn't know it was wrong - now I have an unusable system! Luckily, you can re-delete and re-recreate the partition without any ill effects, but I had to guess where the partition was supposed to start. So NOTE WELL WHERE YOUR PARTITION STARTS BEFORE YOU DELETE IT!

$ fdisk /dev/sda
Command (m for help): p

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders, total 976773168 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: 0x000730a3

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1              63   974227455   488113696+  8e  Linux LVM
/dev/sda2       974229504   976771071     1270784    5  Extended
/dev/sda5       974231552   976771071     1269760   83  Linux

Command (m for help): d
Partition number (1-5): 1

Command (m for help): n
Partition type:
   p   primary (0 primary, 1 extended, 3 free)
   l   logical (numbered from 5)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-976773167, default 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-974229503, default 974229503): +487113696K

Command (m for help): x

Expert command (m for help): b
Partition number (1-5): 1
New beginning of data (1-974229439, default 2048): 63

I moved and resized /dev/sda2 and /dev/sda5 using GParted, which makes me a lot less nervous. Then, lastly:

$ mkdir disk
$ mount /dev/sda5 disk
$ grub-install --boot-directory=./disk /dev/sda

to re-install the boot-loader in the new location.

Note that I have first created the partition starting at 2048 and then moved the start to 63.

The LVM commands here will tell you if you are about to do something stupid, like make a physical volume smaller than the allocated extents it contains, but some will not, especially fdisk - it is quite easy to end up resizing the partition so that it chops off the end of the physical volume. This is a Bad Thing. Make sure the partition is at least as big as the physical volume!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.