2011-11-27

Boot disk transplant

Bare hands, MacGyver-style, boot disk transplant.

I've used a network attached linux machine as a temporary storage, but a local USB disk will do as fine.
The new disk should be as least as big as the old one.

First take note of the original disk configuration.
You will need the partition table:

# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        1305    10377990   8e  Linux LVM

The volume group configuration (note the PE and the LE sizes)

# vgdisplay -v
    Finding all volume groups
  /dev/hda: open failed: No medium found
    Finding volume group "VolGroup00"
  --- Volume group ---
  VG Name               VolGroup00
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               9.88 GB
  PE Size               32.00 MB
  Total PE              316
  Alloc PE / Size       316 / 9.88 GB
  Free  PE / Size       0 / 0
  VG UUID               CJqD4r-kRQM-Cj9e-XwOr-6dn8-TkPH-MfTTyZ

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                d4iToX-8m0c-jf9s-sMMN-VDpO-brE8-4kWhf2
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                7.91 GB
  Current LE             253
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                kEzo7d-AHuv-xiPD-3Qbo-v8yY-6w1D-DFMIwN
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.97 GB
  Current LE             63
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

  --- Physical volumes ---
  PV Name               /dev/sda2
  PV UUID               7bPvQd-H8yE-v00N-asPT-WQ92-6oCS-bBh6nj
  PV Status             allocatable
  Total PE / Free PE    316 / 0

And the fstab:

# cat /mnt/sysimage/etc/fstab
/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/VolGroup00/LogVol01 swap                    swap    defaults        0 0

you will have to rebuild the logical volumes that belong to the transplanting disk

Boot the machine in rescue mode to do a full backup

boot: linux rescue

Start the network interface and do not mount any filesystem.
Backup the filesystems on /dev/sda and send them to the second machine via network.

#gzip -1 </dev/VolGroup00/LogVol00 | ssh user@secondmachine "cat >root.gz"
#gzip -1 </dev/sda1 | ssh user@secondmachine "cat >boot.gz"

Turn off the first machine, remove the old hd and put the new one.
Reboot again into linux rescue, with networking and without searching for existing installations.

Use fdisk to rebuild the same layout, with identical partitions, cylinder boundaries and partition Id (8e for LVM)
Now rebuild the LVM

#lvm pvcreate /dev/sda2

Note the PE size:

#lvm vgcreate -s 32M VolGroup00 /dev/sda2

and the LV size

#lvm lvcreate -l 253 -n LogVol00 VolGroup00
#lvm lvcreate -l 63 -n LogVol01 VolGroup00

Rebuild the swap partition

#mkswap /dev/VolGroup00/LogVol01

and write back the previous backups:

#ssh user@secondmachine "cat boot.gz" | gzip -d > /dev/sda1
#ssh user@secondmachine "cat root.gz" | gzip -d > /dev/VolGroup00/LogVol00

Reboot once again in rescue mode

#chroot /mnt/sysimage 
#grub-install /dev/sda

And finally reboot with your new disk.
Note that if the new disk is bigger than the old one, you will have some unpartitioned space left.
You can partition it with fdisk, pvcreate, and add it to LVM.

No comments:

Post a Comment