Merge root disk volumes

Lars Jönsson 2019-11-26

Information about how to merge all the physical volumes on the logical root volume into one physical volume.

Scenario

All data from the root volume will be copies to a new and the layout of the original disk will be changed.

Old Disk layout
      - 1 physical boot volume
      - 1 logical root volume consisting of 3 physical volumes
New Disk layout
      - 1 physical boot volume
      - 1 logical root volume consisting of 1 physical volume
fedora -> root volume group

Preparations

Temporary disk

An empty temporary disk needs to be connected to the machine during the merge. In this document it is assumed to be /dev/sdb.

Live CD

The machine needs to be booted with a Fedora Live cd. The root volume cannot be mounted during the layout change.

Merge the root disk volumes

Prepare the temporary disk

Create a volume table on the disk.

# parted /dev/sdb mktable msdos

Create a single volume.

# parted /dev/sdb mkpart primary 0% 100%

Check the partions on the disk

# parted /dev/sdb print
Number  Start   End     Size    Type     File system  Flags
        1024B   1049kB  1048kB           Free Space
 1      1049kB  21,5GB  21,5GB  primary

Create the filesystem on the disk.

# mkfs.ext4 /dev/sdb1

Backup the data to the temporary disk

Create mounting points.

# mkdir orig tmp

Mount the existing root volume and the temporary disk.

# mount /dev/fedora/root orig
# mount /dev/sdb1 tmp

Copy all data from the existing root volume to the temporary disk. This step takes some time to complete.

# rsync -avAXHP --numeric-ids orig/ tmp/

Remove all data except lost+found to be able to remove the physical volumes that will not be used anymore.

# find orig/ -mindepth 1 ! -regex '^orig/lost\+found\(/.*\)?' -delete
# umount orig
# e2fsck -f /dev/fedora/root
# resize2fs -M /dev/fedora/root

Create new layout on the root volume

Reduce the use of the logical volume to be able to remove the physical volumes.

# lvresize -L 100M /dev/fedora/root

Remove the phycal volumes from the fedora volume group and remove physical volumes from lvm.

# vgreduce fedora /dev/sda4
# vgreduce fedora /dev/sda3
# pvremove /dev/sda4
# pvremove /dev/sda3

Update the partition table with the new layout.

# parted /dev/sda rm 4
# parted /dev/sda rm 3
# parted /dev/sda resizepart 2 100%

Create the new layout of the logical root volume.

# pvresize /dev/sda2
# lvresize -l +100%FREE /dev/fedora/root

Resize the filesystem.

# resize2fs /dev/fedora/root

Restore the data to the root volume

Mount the volumes.

# mount /dev/fedora/root orig

Copy all data from the temporary disk to the root volume. This step takes some time to complete.

# rsync -avAXHP --numeric-ids tmp/ orig/

Check the the volumes seems be have the same content.

# ls orig tmp

Reboot your physical or virtual machine, please make sure that your Live CD and the temporary disk are removed.