DTechs

Move your boot partition to new partition

To move your boot partition to another partition with the following hard conditions: ( Boot partitions are still required to be primary partitions. )

  1. You will use the same disk ( you have only 1 disk )
  2. Your disk does not have any free space to create another partition
  3. You are using Logical Volumes in 1 physical partition with all the space had been used.

Solution  — what has been done:

Step 0:  Boot the system with a live ubuntu cd. Due to the logical volume are root and swap and we will migrate boot, all the tasks needs to be done in the live session.

root@t1:~# df -h
Filesystem Size Used Avail Use% Mounted on

/dev/mapper/rootvg-rootlv 8,8G 3,6G 4,8G 43% /
/dev/sda2 248M 57M 179M 25% /boot

root@t1:~# pvs
 PV VG Fmt Attr PSize PFree
 /dev/sda3 rootvg lvm2 a-- 9,50g 0 

root@t1:~# lvs
 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
 rootlv rootvg -wi-ao---- 9,00g 
 swaplv rootvg -wi-ao---- 512,00m 

root@t1:~# vgs
 VG #PV #LV #SN Attr VSize VFree
 rootvg 1 2 0 wz--n- 9,50g 0
root@t1:~# fdisk -l /dev/sda
Disk /dev/sda: 10 GiB, 10737418240 bytes, 20971520 sectors
...
Disk /dev/sda: 10 GiB, 10737418240 bytes, 20971520 sectors
...
Disklabel type: dos
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 526335 524288 256M ef EFI (FAT-12/16/32)
/dev/sda2 * 526336 1050623 524288 256M 83 Linux
/dev/sda3 1050624 20971519 19920896 9,5G 8e Linux LVM: dos

1- Resize the file system

First, it needs to shrink your filesystem that will create empty space for later use. There should be free space in file system to shrink safely from the end of filesystem. if there is no empty space, you need to delete something to create empty space.

root@ubuntu:~# mount /dev/rootvg/rootlv /mnt/
root@ubuntu:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rootvg-rootlv 8.8G 3.6G 4.8G 43% /mnt
root@ubuntu:~# umount /mnt 

root@ubuntu:~# resize2fs /dev/rootvg/rootlv 6G
resize2fs 1.42.13 (17-May-2015)
Please run 'e2fsck -f /dev/rootvg/rootlv' first.

root@ubuntu:~# e2fsck -f /dev/rootvg/rootlv 
e2fsck 1.42.13 (17-May-2015)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/rootvg/rootlv: 187725/589824 files (0.1% non-contiguous), 997639/2358272 blocks

root@ubuntu:~# resize2fs /dev/rootvg/rootlv 6G 
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/rootvg/rootlv to 1572864 (4k) blocks.
The filesystem on /dev/rootvg/rootlv is now 1572864 (4k) blocks long.

2- Resize the logical volume

Now, we had the filesystem shrunk. But the residing logical volume still spanning all the storage.So we need to shrink it as well.

root@ubuntu:~# pvs --segments -v
 Using physical volume(s) on command line.
 PV VG Fmt Attr PSize PFree Start SSize LV Start Type PE Ranges 
 /dev/sda3 rootvg lvm2 a-- 9.50g 0 0 1536 rootlv 0 linear /dev/sda3:0-1535 
 /dev/sda3 rootvg lvm2 a-- 9.50g 0 1536 128 swaplv 0 linear /dev/sda3:1536-1663
 /dev/sda3 rootvg lvm2 a-- 9.50g 0 1664 767 rootlv 1536 linear /dev/sda3:1664-2430

root@ubuntu:~# lvreduce -L -1800M /dev/rootvg/rootlv 
 WARNING: Reducing active logical volume to 7.24 GiB
 THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce rootlv? [y/n]: y
 Size of logical volume rootvg/rootlv changed from 9.00 GiB (2303 extents) to 7.24 GiB (1853 extents).
 Logical volume rootlv successfully resized.

root@ubuntu:~# pvs --segments -v
 Using physical volume(s) on command line.
 PV VG Fmt Attr PSize PFree Start SSize LV Start Type PE Ranges 
 /dev/sda3 rootvg lvm2 a-- 9.50g 1.76g 0 1536 rootlv 0 linear /dev/sda3:0-1535 
 /dev/sda3 rootvg lvm2 a-- 9.50g 1.76g 1536 128 swaplv 0 linear /dev/sda3:1536-1663
 /dev/sda3 rootvg lvm2 a-- 9.50g 1.76g 1664 317 rootlv 1536 linear /dev/sda3:1664-1980
 /dev/sda3 rootvg lvm2 a-- 9.50g 1.76g 1981 450 0 free

3- Move the other logical volumes

Even the shrinking had been done. ( and now we have a free space in the physical volume, this part may end up as in the middle of the physical volume. If there are logical volumes after the shrunked logical volume, we need to move the other volumes so that usage will contiguous on the physical volume.

pvmove --alloc anywhere /dev/sda3:1536-1663

4- Resize the Physical Volume of the Volume Group

Please dont forget to resize your filesystem for 1 more time to extend its boundries to the logical volume. ( to fully utilize the logical volume )

root@ubuntu:~# resize2fs /dev/rootvg/rootlv 
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/rootvg/rootlv to 1897472 (4k) blocks.
The filesystem on /dev/rootvg/rootlv is now 1897472 (4k) blocks long.

Now, we have free space at the end of the Physical volume. To shrink it from volume group:

root@ubuntu:~# pvresize --setphysicalvolumesize 8G /dev/sda3
 Physical volume "/dev/sda3" changed
 1 physical volume(s) resized / 0 physical volume(s) not resized
root@ubuntu:~# pvs --segments -v
 Using physical volume(s) on command line.
 PV VG Fmt Attr PSize PFree Start SSize LV Start Type PE Ranges 
 /dev/sda3 rootvg lvm2 a-- 8.00g 264.00m 0 1536 rootlv 0 linear /dev/sda3:0-1535 
 /dev/sda3 rootvg lvm2 a-- 8.00g 264.00m 1536 128 swaplv 0 linear /dev/sda3:1536-1663
 /dev/sda3 rootvg lvm2 a-- 8.00g 264.00m 1664 317 rootlv 1536 linear /dev/sda3:1664-1980
 /dev/sda3 rootvg lvm2 a-- 8.00g 264.00m 1981 66 0 free

5- Resize the disk partition & create new partition and filesystem

It is easier and error free to  use the “Gparted”.

screenshot-from-2016-12-04-21-54-38 screenshot-from-2016-12-04-21-52-56

These are the images of the operation.

7- Copy the boot filesystem content to new filesystem

root@ubuntu:~# mkdir /mnt/old_boot
root@ubuntu:~# mkdir /mnt/new_boot

root@ubuntu:~# mount /dev/sda2 /mnt/old_boot/
root@ubuntu:~# mount /dev/sda4 /mnt/new_boot/

root@ubuntu:/mnt# cd old_boot/

root@ubuntu:/mnt/old_boot# ls
abi-4.4.0-31-generic memtest86+.bin
config-4.4.0-31-generic memtest86+.elf
grub memtest86+_multiboot.bin
initrd.img-4.4.0-31-generic System.map-4.4.0-31-generic
lost+found vmlinuz-4.4.0-31-generic

root@ubuntu:/mnt/old_boot# cd ..

root@ubuntu:/mnt# cd new_boot/
root@ubuntu:/mnt/new_boot# ls
lost+found

root@ubuntu:/mnt/new_boot# cp -pr ../old_boot/* .
lroot@ubuntu:/mnt/new_boot# ls
abi-4.4.0-31-generic memtest86+.bin
config-4.4.0-31-generic memtest86+.elf
grub memtest86+_multiboot.bin
initrd.img-4.4.0-31-generic System.map-4.4.0-31-generic
lost+found vmlinuz-4.4.0-31-generic

8- Change the fstab & you can reboot your system.

root@ubuntu:/mnt/new_boot# blkid 
/dev/sda2: UUID="3e064059-1bb9-4592-9ae8-afe63226010b" TYPE="ext2" PARTUUID="37af4a52-02"
/dev/sr0: UUID="2016-07-19-21-27-51-00" LABEL="Ubuntu 16.04.1 LTS amd64" TYPE="iso9660" PTUUID="40a863e7" PTTYPE="dos"
/dev/loop0: TYPE="squashfs"
/dev/sda3: UUID="61ETpL-kSJj-styl-1q8L-AWca-3NV9-PD3snl" TYPE="LVM2_member" PARTUUID="37af4a52-03"
/dev/mapper/rootvg-rootlv: UUID="236ac39b-88c9-4948-9f86-e15fe807d406" TYPE="ext4"
/dev/mapper/rootvg-swaplv: UUID="0526193a-776f-4c75-b5cc-6c37be25c05d" TYPE="swap"
/dev/sda4: UUID="485150ec-fb2e-4fb8-b20a-7d8c0a6ab043" TYPE="ext2" PARTUUID="37af4a52-04"

root@ubuntu:/mnt# mkdir root
root@ubuntu:/mnt# mount /dev/rootvg/rootlv root/
root@ubuntu:/mnt# cd root/
root@ubuntu:/mnt/root# vi etc/fstab 
<Change the fstab here>
<Now the final status of the fstab>

root@ubuntu:/mnt/root# cat etc/fstab
/etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/mapper/rootvg-rootlv / ext4 errors=remount-ro 0 1
# /boot was on /dev/sda2 during installation
/dev/mapper/rootvg-swaplv none swap sw 0 0
UUID=485150ec-fb2e-4fb8-b20a-7d8c0a6ab043 /boot ext2 defaults 0 2


Now, it is ok to reboot your system.