Ubuntu: Migrate to Grub2
From ReceptiveIT
This howto has been written to document a migration path from how I used to partition my servers, to my current partitioning scheme.
I used to use a USB flash disk to contain the /boot partition. Usually I would have a RAID-1 with the LVM taking the entire block devices. While using the entire block device is possible, it does not allow any space for a bootloader on the disk. The USB boot had the advantage of keeping the kernel and initial ramdisk on a disk that was outside of the raid and LVM subsystem. This was important while using grub-legacy as it did not understand LVM. Since Grub2 now understands LVM and some raid modes, it is now possible to have everything inside of a LVM, so we can ditch the USB disk requirement.
Make sure you have a backup before you attempt this procedure
Contents |
Collect Information
Get the Raid information
root@server:~# cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10]
md0 : active raid1 sda[0] sdb[1]
976762496 blocks [2/2] [UU]
unused devices: <none>
This tells us that we have a Raid-1 (mirror) with two disks and the raid array is md0. Since a Raid-1 is made up of two identical disks, we have the option of splitting the raid array, creating a new array with one of the disks and then moving the LVM to the new array.
Get the LVM information
root@server:~# pvscan PV /dev/md0 VG lvmname lvm2 [931.51 GiB / 714.51 GiB free] Total: 1 [931.51 GiB] / in use: 1 [931.51 GiB] / in no VG: 0 [0 ]
This tells us that we have a LVM with the name lvnname. This particular LVM is 931.51GB in size, but of that, 714.51GB is free space. The destination drive therefore needs to be 217GB minimum.
Split the Raid-1
Fail one of the disks. In this case it will be /dev/sdb
root@server:~# mdadm --manage --fail /dev/md0 /dev/sdb
Remove the failed drive from the array
root@server:~# mdadm --remove /dev/md0 /dev/sdb
Create a new Raid-1 with the spare disk
Sanitise the removed disk by removing the raid superblock
root@server:~# mdadm --misc --zero-superblock /dev/sdb
Partition the removed disk
root@server:~# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xd85c2e05.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-121601, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-121601, default 121601):
Using default value 121601
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux RAID autodetect)
Command (m for help): a
Partition number (1-4): 1
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
Create a new Raid-1 with the removed disk
root@server:~# mdadm --create --level=1 --raid-devices=2 /dev/md1 /dev/sdb1 missing mdadm: array /dev/md1 started.
Migrate LVM
The good news is that you can migrate the entire LVM while the system is running. The bad news is that it is a block level copy of all allocated blocks, so if you have a 1TB logical volume that has an empty filesystem in it, you will still be moving 1TB of data between disks. It can be slow. You have been warned.
Create the new physical volume on the new Raid-1
root@server:~# pvcreate /dev/md1
Extend the existing LVM onto the new physical volume
root@server:~# vgextend lvmname /dev/md1
Move all extents from the physical volume /dev/md0 to /dev/md1. This will take some time. You might consider doing this in a screen session.
root@server:~# pvmove -v /dev/md0 /dev/md1
Remove the physical volume /dev/md0 from the LVM
root@server:~# vgreduce lvmname /dev/md0 root@server:~# pvremove /dev/md0
Tidying up
Stop the old Raid-1 array /dev/md0
root@server:~# mdadm --manage --stop /dev/md0
Sanitise the other disk by removing the raid superblock
root@server:~# mdadm --misc --zero-superblock /dev/sda
Partition the removed disk
root@server:~# fdisk /dev/sda
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xd85c2e05.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-121601, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-121601, default 121601):
Using default value 121601
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux RAID autodetect)
Command (m for help): a
Partition number (1-4): 1
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
Add other disk to new Raid-1
root@server:~# mdadm --manage --add /dev/md1 /dev/sda1
Modify /etc/mdadm/mdadm.conf to reflect new UUID (Clean me up)
root@server:~# mdadm --examine --scan >> /etc/mdadm/mdadm.conf root@server:~# vi /etc/mdadm/mdadm.conf
Update the initial ramdisk for all kernels
root@server:~# update-initramfs -u
Remove USB boot device
This is a work-in-progress
mkdir /bootnew cd /bootnew cp -auxv /boot/* . cd / umount /boot rmdir /boot mv /bootnew /boot dpkg-reconfigure grub-pc (upgrade-from-grub-legacy)

