These are steps I recorded for moving from a large LVM managed disk with a CentOS 5.5 install to a smaller disk (obviously the used space on the large disk was less than the size of the smaller disk). I'm sure there are better ways to do this, but this method was successful. Some steps may be specific to our situation, tweak as necessary.
Reqs:
Steps:
Connect the new disk
Boot with the CD, at the prompt type "linux rescue" to get into rescue mode. It will ask whether you want to continue or skip the mount, you should continue (it will mount to /mnt/sysimage
). Do not format the new disk if it asks.
Check previous geometry with fdisk -l
. You'll likely have /dev/sda1
and /dev/sda2
in a regular LVM configuration. sda1
will be your /boot partition which exists outside of the LVM. Its size should be 1-13 with the rest of the disk being dedicated to the LVM.
.
# fdisk -l
...
/dev/sda1 * 1 13 ... 83 Linux
/dev/sda2 14 ... 8e Linux LVM
# fdisk /dev/sdb
>Command...:
n
>Command action
>e extended
>p primary partition (1-4)
p
>Partition number (1-4):
1
>First cylinder ...:
<default>
>Last cylinder ...:
13 (value from /dev/sda1, the original /boot)
>Command...:
n
>Command action
>e extended
>p primary partition (1-4)
p
>Partition number (1-4):
2
>First cylinder ...:
<default>
>Last cylinder ...:
<default (end of disk)>
>Command...:
t
>Partition...:
1
>Hex code...:
83
>Command...:
t
>Partition...:
2
>Hex code...:
8e
>Command...:
a
>Partition...:
1
>Command...:
w
- Create file system for /boot on /dev/sdb1
.
# mkfs.ext3 /dev/sdb1
- Setup new Physical Volume, Volume Group, and Logical Volumes and their filesystems on /dev/sdb2. Replace
?G
with the size you want. LogVol00 should be LVM partition size minus your required swap volume size, LogVol01 should be your swap size.
.
# pvcreate /dev/sdb2
# vgcreate VolGroup01 /dev/sdb2
# lvcreate --name LogVol00 --size ?G VolGroup01
# lvcreate --name LogVol01 --size ?G VolGroup01
# mkfs.ext3 /dev/VolGroup01/LogVol00
# mkswap /dev/VolGroup01/LogVol01
- Mount the new disk and copy contents from the old disk to it with
cp -ax
. Avoid copying /dev
, /proc
, /sys
, /boot
, /lost+found
and /mnt
.
# mkdir /mnt/newdisk
# mount /dev/VolGroup01/LogVol00 /mnt/newdisk
# cd /mnt/sysimage
# for i in $(ls -1 | grep -v '\(dev\|proc\|sys\|mnt\|boot\|lost\)'); do echo $i; cp -ax /mnt/sysimage/$i /mnt/newdisk; done
# cd /mnt/newdisk
# mkdir {dev,proc,sys,mnt,boot}
- Mount the new
/boot
and copy contents from the old disk to it, then unmount it
.
# mkdir /mnt/{boot,newboot}
# mount /dev/sda1 /mnt/boot
# mount /dev/sdb1 /mnt/newboot
# cp -ax /mnt/boot/* /mnt/newboot
# umount /mnt/newboot
- Install grub to the new disk
.
# mount -o bind /dev /mnt/newdisk/dev
# mount /dev/sdb1 /mnt/newdisk/boot
# chroot /mnt/newdisk
# grub
> root (hd1,0)
> setup (hd1)
> quit
- Fix your
/boot/grub/grub.conf
.
# vi /boot/grub/grub.conf
:%s/VolGroup00/VolGroup01/g
:wq
.
# cd /boot
# for i in $(ls -1 initrd* | grep -v bak); do mv $i{,-bak}; ver=$(echo $i | sed 's/initrd-//;s/\.img//;'); mkinitrd /boot/$i $ver; done
.
# exit
#
.
# vi /mnt/newdisk/etc/fstab
:%s/VolGroup00/VolGroup01/g
:wq
.
# e2label /dev/sda1 /boot
- Remove the CD, and you should be able to boot into the resized disk at this point.