Introduction

Here is an example where this could be useful: you have a RAID with a size greater than 2TB and you have an existing system installed on a non-(U)EFI hardware (or virtual hardware). By default, Ubuntu installs MBR onto a disk on a non-U(EFI) system. My use case is a file sharing VM (Xen-based virtualization) on a 5TB RAID-5. In examples (given in a comment after a generalized command) assume that /dev/xvda is a source disk and /dev/xvdb is a destination disk (xvd - stands for Xen Virtual Disk) and there is a volume group 'ubuntu-vg' on a source disk. The source disk's layout is assumed to be as follows: ext2_Boot_Partition, LVM_Partition. Physical volume names are identical to the respective device names. 'Disk' and 'Device' terms are used somewhat interchangeably. Source and destination disks could be of a different size so I added *optional* steps that allow you to do the resizing for ext4 file system (would also work for ext2 and ext3).

1. Create a GPT record on a new disk (using parted or gparted)

sudo parted /dev/<device> # e.g. parted /dev/xvdb
mklabel gpt

2. Create a BIOS boot partition (BBP)

Create a 1MB BIOS boot partition (BBP) (set a bios_grub flag in GParted) at the start of the GPT disk without file system (unformatted or cleared in GParted) which is used to store grub stage 1.5.

# You cannot start at 0MB because that would mean the first sector of the disk, where the MBR resides.
mkpart <bbpPartitionName> 1MB 2MB # e.g. mkpart bbp 1MB 2MB

# Mark the first partition as a BBP
set 1 bios_grub on

3. Create a partition for a destination Physical Volume

Create a partition for a Physical Volume which is going to be used as a destination for moving.

# given you want to use the rest of the space for LVM partition
mkpart <LVMPartitionName> 2MB 100% # e.g. mkpart lvmPartition 2MB 100%

# exit parted
quit

4. Create a destination Physical Volume

Create a Physical Volume which is going to be used as a destination for moving.

sudo pvcreate /dev/<device> # e.g. pvcreate /dev/xvdb2

5. Add the created Physical Volume to the existing Volume Group

sudo vgextend <volumeGroupName> <physicalVolumePath> # e.g. vgextend ubuntu-vg /dev/xvdb2

6. Move the source Physical Volume to the destination PV

# you can also move Logical Volumes (see a manpage for pvmove)

sudo pvmove /dev/<sourcePhysicalVolume> /dev/<destinationPhysicalVolume> # e.g. pvmove /dev/xvda2 /dev/xvdb2

7. Remove the source Physical Volume from the existing Volume Group

sudo vgreduce <volumeGroupName> <sourcePhysicalVolume> # e.g. sudo vgreduce /dev/xvda2

8. Mount the boot partition on the source disk and the 'root' LV

Mount the boot partition on the source disk and the 'root' LV (which was just moved to the destination PV).

sudo mkdir /mnt/bootp/ && sudo mkdir /mnt/rootp
sudo mount /dev/<ext2_Boot_Partition> /mnt/bootp && sudo mount /dev/mapper/<rootLogicalVolume> /mnt/rootp
# e.g. sudo mount /dev/xvda1 /mnt/bootp && sudo mount /dev/mapper/ubuntu--vg-root /mnt/rootp

9. Copy /boot directory from the boot partition on the source disk to the '/' of the root LV

sudo cp -r /mnt/bootp/* /mnt/rootp/boot

10. Bind the directories that grub needs to access to detect operating systems

sudo mount --bind /dev /mnt/rootp/dev &&
sudo mount --bind /dev/pts /mnt/rootp/dev/pts &&
sudo mount --bind /proc /mnt/rootp/proc &&
sudo mount --bind /sys /mnt/rootp/sys

11. Chroot into the 'root' LV

sudo chroot /mnt/rootp

12. Install grub into the destination disk

Install grub into the destination disk which contains the BIOS boot partition (this is going to write grub stage 1.5 into BBP).

sudo grub-install /dev/<destinationDevice> # e.g. grub-install /dev/xvdb

# recheck the device map (see https://www.gnu.org/software/grub/manual/html_node/Invoking-grub_002dinstall.html)
sudo grub-install --recheck /dev/<destinationDevice> # e.g. grub-install --recheck /dev/xvdb

sudo update-grub2

13. Comment out (or remove) fstab entry corresponding to the old boot partition

Comment out (or remove) fstab entry corresponding to the old boot partition (which we got rid of by copying its contents directly to '/'). Otherwise the operating system is going to fail to boot, not being able to mount the boot partition.

# Comment out the current fstab entry for the boot partition
sed -i "s|.*UUID.*/boot.*|#\0|" /mnt/rootp/etc/fstab

# or remove the entry completely
# e.g. sed -i "s|.*UUID.*/boot.*||" /mnt/rootp/etc/fstab

14) *Optional* 'Unhide' menu entries in generated boot.cfg (the generated file has menu entries in hidden state sometimes for some reason)

# In /boot/grub/grub.cfg (since we are chroot-ed) change
# set timeout_style=hidden
# to the following:
# set timeout_style=menu

sudo sed -i 's/set timeout_style=hidden/set timeout_style=menu/' /mnt/rootp/boot/grub/grub.cfg

15. *Optional* (VM-related) Fix GPT to have a record located at the end of the destination disk

Fix GPT to reside at the end of the new disk using parted (say 'Fix' when prompted) and resize the LVM partition. This is required if you resized your virtual disk image in some way, for example

qemu-img resize <imageName> +<size><unit>
# e.g. qemu-img resize ubuntu-owncloud.img +1000G

sudo parted
# Type 'Fix' a couple of times

unit MB
print

resize <partitionNumber> <startSize> <endSize>

16. *Optional* Extend the root Logical Volume to occupy all free space on the new PV

lvextend -l +100%FREE /dev/<volumeGroupName>/<rootLogicalVolume>

17. *Optional* Resize the filesystem on the root Logical Volume

In this case ext4 is assumed. Would also work for ext2 and ext3.

# If your kernel supports online resizing then this is possible in userspace
# see a note here https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Storage_Administration_Guide/ext4grow.html
# or the respective manpage.
# The size parameter is not required: the default behavior is to enlarge a file system using all free space available on a partition (all free space on the LV used in our case)

sudo resize2fs /dev/<volumeGroupName>/<rootLogicalVolume> # e.g. sudo resize2fs /dev/ubuntu-vg/root

See Also

External Links


CategoryHardware

How to Migrate Ubuntu installed on a LVM Logical Volume from a MBR disk to a GPT disk on non-(U)EFI hardware without data loss (last edited 2014-08-08 05:53:42 by rusdmitry)