Migrating Physical Volume root to LVM

From Labrats.us
Jump to navigationJump to search

Expand hard drive

Assuming you are on VMware, you just shut down and increase the virtual hard drive size. You should know how to do this, so I won;t include it here. You may also be able to add a physical or virtual disk.

Create LVM Physical Volume

Using fdisk, add a new partition with type 8e. If possible, leave room between the end of the last existing partition and the new partition.

Using pvcreate, create the physical lvm volume.

# pvcreate /dev/sda4

Create volume group

Using vgcreate, vreate new volume group

# vgcreate VolGroup00  /dev/sda4

Create new logical root volume

Make sure to create the new logical volume the same size as the existing volume. Use lvcreate.

# lvcreate -L 4G -n root VolGroup00

Format the new logical volume

Using mkfs.ext3 or mkfs.ext4. as required by the OS version.

# mkfs.ext3 /dev/VolGroup00/root

Mount new root volume

# mount /dev/VolGroup00/root  /mnt

Begin copy of the data to new volume

You can use tar or you can use rsync. Either seems to work.

# rsync  --archive --sparse --one-file-system --delete-during --delete-excluded \
  --force --numeric-ids --hard-links / /mnt/

OR

# tar -cvpf - --one-file-system --acls --xattrs --selinux / | tar -C /mnt xf -


(Optional) Create new SWAP partition

You likely want to move the SWAP partition to the new VG as well, or you may have trouble further down the instructions. MAke it the same size as your existing SWAP partition. If you don't know what size your existing SWAP is, you can use the swapon command.

# # swapon -s
Filename				Type		Size	Used	Priority
/dev/sda2                              	partition	4194300	155704	-1
# lvcreate -L 4G -n swap VolGroup00
# mkswap /dev/VolGroup00/swap

Copy the /dev partition

# cp -aux /dev /mnt/dev

Edit new fstab

Edit the new fstab to change from old partition to new partition.

# vi /mnt/etc/fstab

Be sure to fix both root and swap partitions

Switch to single user mode

This is optional, but may help.

# init 1

Repeat rsync

This should be fast, and will only copy anything that changed.

# rsync  --archive --sparse --one-file-system --delete-during --delete-excluded \
  --force --numeric-ids --hard-links / /mnt/

Mount extra directories

You will need to mount a bunch of directories to get the kernel to work.

# for i in proc dev sys dev/pts boot ; do mount -o bind /$i /mnt/$i ; done

Now chroot to the new volume

You will need to do this to get the kernel to install locally.

# chroot /mnt

Update Kernel

Depending on your version of OS, you will be able to run one of the following. You may want to have the RPM for the kernel already donwloaded to root's directory.

# update-grub

OR

# mkinitrd -v /boot/initrd-`uname -r`.img `uname -r`

OR

# rpm -i --replacepkgs kernel-2.6.18-486.el5.rpm

If you have any errors or messages, fix them before you reboot.

Unmount and reboot

At this point, you should be able to unmount and reboot the system.

# exit
#  i in proc sys dev/pts dev boot var /; do umount /mnt/$i ; done
# shutdown -r now

Boot into single user mode

It is not critical to boot into single user mode, as this should be able to be done live, but it is safer to do in single user mode.

Delete old partitions

Using fdisk, remove your old root and swap partitions.

Add new physical partitions

Using fdisk, again, add a new primary partition of type 8e. This should replace the partitions you just removed.

After this is complete, you may need to reboot or run partprobe to refresh the partition table.

# partprobe

Create new physical volume =

Using pvcreate, like before.

# pvcreate /dev/sda2

Extend Volume Group

# vgextend VolGroup00 /dev/sda2

Move volumes to new PV

# pvmove /dev/sda4

Remove PV from VG

# vgreduce VolGroup00 /dev/sda4

Delete old PV

# pvremove /dev/sda4

Use fdisk to remove and resize partitions

Delete both LVM partitions, and create a new partitions, type 8e, that starts in the same place as /dev/sda2. You want this to be larger than the old /dev/sda2, as you are extending the partitions.

when finished, you can run partprobe or reboot to refresh the partitin table.

Extend the pv

Using pvresize, extend the PV size.

# pvresize /dev/sda2
# pvdisplay /dev/sda2

Extend the LV

You don't need to change the VG, but you will need to change the LV using lvresize. This can be done while the system is live.

# lvresize -l +100%FREE /dev/VolGroup00/root

Extend the file system

Extend the file system using resize2fs.

# resize2fs /dev/VolGroup00/root