Showing posts with label clone. Show all posts
Showing posts with label clone. Show all posts

How to use hardware RAID10 to clone Linux between 2 Dell R720 servers


 Jephe Wu - http://linuxtechres.blogspot.com

Environment: 2 Dell R720 (db01 and db02), need to clone db01 to db02 and wipe off everything on db02 hard disks which was used for other purpose.
db01 has 6 SAS 300G 15k rpm hard disk and configured as RAID10.
IP addresses for 2 servers: db1: 192.168.0.1 db2: 192.168.0.2

Objective: to clone server db01 to db02 and change ip address.



Steps:

1. Cloning hard disk

While db01 is running, remove hard disk ID 1,3,5 , insert any 3 hard disks from db02 while db02 is power off, then it will automatically start rebuilding raid10, you can observe it from Dell Openmanage page https://192.168.0.1:1311 for db01.

Put hard disk ID 1,3,5 from db01 into slot 0,2,4 on db05 which is the top row of disk slots, keep slot 1,3,4 empty first, then power on db02.

On boot up screen, it will say all original disk has been removed, press F to import foreign config.
You have to press it quickly otherwise, it will continue and fail to bootup Linux due to no boot up device found.

Press F so it can continue to boot Linux OS which is same as the one on db01

2. Configure network on db02 after OS boot up

As the hard disk comes from db01, after OS boots up, everything is same as db01, you need to change the following:

a. hostname. 
run commands below:
# hostname hmspzdb05
# vi /etc/sysconfig/network to change hostname to hmspzdb05
# service network restart


b. ip address
# cd /etc/sysconfig/network-scripts
# dmesg | grep eth  # to get all network NICs mac address and network card module name (igb)

[root@db01 ~]# dmesg | grep eth
igb 0000:01:00.0: eth0: (PCIe:5.0Gb/s:Width x4) bc:20:5b:ed:95:28
igb 0000:01:00.0: eth0: PBA No: G10565-011
igb 0000:01:00.1: eth1: (PCIe:5.0Gb/s:Width x4) bc:20:5b:ed:95:29
igb 0000:01:00.1: eth1: PBA No: G10565-011
igb 0000:01:00.2: eth2: (PCIe:5.0Gb/s:Width x4) bc:20:5b:ed:95:2a
igb 0000:01:00.2: eth2: PBA No: G10565-011
igb 0000:01:00.3: eth3: (PCIe:5.0Gb/s:Width x4) bc:20:5b:ed:95:2b
igb 0000:01:00.3: eth3: PBA No: G10565-011
igb 0000:44:00.0: eth4: (PCIe:5.0Gb/s:Width x4) a0:26:9f:01:c8:74
igb 0000:44:00.0: eth4: PBA No: G13158-000
igb 0000:44:00.1: eth5: (PCIe:5.0Gb/s:Width x4) a0:26:9f:01:c8:75
igb 0000:44:00.1: eth5: PBA No: G13158-000
igb 0000:44:00.2: eth6: (PCIe:5.0Gb/s:Width x4) a0:26:9f:01:c8:76
igb 0000:44:00.2: eth6: PBA No: G13158-000
igb 0000:44:00.3: eth7: (PCIe:5.0Gb/s:Width x4) a0:26:9f:01:c8:77
igb 0000:44:00.3: eth7: PBA No: G13158-000

# vi ifcfg-em1/2/3/4 and vi ifcfg-p2p1/2/3/4 (if any) to modify mac address and ip address line

c. service restart network to take affect without reboot

Note: you don't have to reboot to make networking work.
# rmmod igb
# modprobe igb
Note: it will update /etc/udev/rules.d/70-persistent-net.rules
# service network restart

3.  rebuild raid 10 on db02

You can now insert the remaining 3 hard disks into slot ID 1,3,5 on db02.

It won't rebuild RAID10 automatically by itself after insert.

Now check Open manage page at https://192.168.0.2:1311 (new ip address for db02 after change)

all the hard disks in slot ID 1,3,5 shows 'foreign' disks.

You need to clear 'foreign' status by doing the following:  refer to http://en.community.dell.com/support-forums/servers/f/906/t/19299553.aspx


You will need to clear the foreign configuration, reconfigure the drive as hotspare and the rebuild will start.

To clear foreign configuration:  select the controller on OpenManage, go to the "Information/Configuration" tab, on "Controller Tasks" select "Foreign Configiration Operations", click "Execute".  On the next page, click "Clear"


After clearing 'foreign', the status becomes 'ready', it's still not rebuilding RAID10.

To reconfigure as hot spare: go to the "Physical Disk" view, on the "Available Task" for that drive select "Assign Global Hot Spare". then it will start rebuilding RAID10.

4. reboot db01 and db02 to confirm it's okay.

Clone Oracle virtual machine to physical server

Jephe Wu - http://linuxtechres.blogspot.com

Environment: Oracle virtual machine image file .img (shutdown VM to make image), use hda as hard disk only, no any scsi driver in initrd image file. Oracle Enterprise Linux 4.5, grub-0.95-3.8.0.1 rpm which is the latest in OL4.9
Objective: Clone it to a dell PowerEdge 1950 (megaraid scsi driver)


Steps:

1.  boot from RIP CD(Recovery Is Possible) non-X version, 64bit kernel

login as root without password

vi /etc/hosts.deny to comment out the last line to allow remote ssh

vi /etc/ssh/sshd_config to allow direct root login

dhcpcd eth0 or use ifconfig to manually set up ip address

/usr/sbin/sshd

passwd root

2. transfer System.img which is Oracle VM image file over somewhere on the network which you can mount it

# file System.img

# mkdir iso

# mount System.img iso -o offset=$((512*63))

=========some examples for using offset to mount Oracle VM system image.

[root@oravmdr01 xen]# file dr2.img
dr2.img: x86 boot sector; partition 1: ID=0x83, active, starthead 0, startsector 2048, 208896 sectors; partition 2: ID=0x83, starthead 0, startsector 210944, 24954880 sectors, code offset 0x48

noet: first partition is /boot, the second one is /.

[root@oravmdr01 xen]# mount dr2.img -o offset=$((512*2048)) /tmp/a
[root@oravmdr01 xen]# ls /tmp/a
config-2.6.18-308.20.1.0.1.el5xen  grub                                   initrd-2.6.39-300.17.2.el5uek.img      symvers-2.6.32-300.21.1.el5uek.gz      System.map-2.6.32-300.21.1.el5uek   vmlinuz-2.6.32-300.21.1.el5uek  xen-syms-2.6.18-308.20.1.0.1.el5
config-2.6.18-308.4.1.0.1.el5xen   initrd-2.6.18-308.20.1.0.1.el5xen.img  lost+found                             symvers-2.6.39-300.17.2.el5uek.gz      System.map-2.6.39-300.17.2.el5uek   vmlinuz-2.6.39-300.17.2.el5uek  xen-syms-2.6.18-308.4.1.0.1.el5
config-2.6.32-300.21.1.el5uek      initrd-2.6.18-308.4.1.0.1.el5xen.img   symvers-2.6.18-308.20.1.0.1.el5xen.gz  System.map-2.6.18-308.20.1.0.1.el5xen  vmlinuz-2.6.18-308.20.1.0.1.el5xen  xen.gz-2.6.18-308.20.1.0.1.el5
config-2.6.39-300.17.2.el5uek      initrd-2.6.32-300.21.1.el5uek.img      symvers-2.6.18-308.4.1.0.1.el5xen.gz   System.map-2.6.18-308.4.1.0.1.el5xen   vmlinuz-2.6.18-308.4.1.0.1.el5xen   xen.gz-2.6.18-308.4.1.0.1.el5
[root@oravmdr01 xen]# umount /tmp/a

[root@oravmdr01 xen]# mount dr2.img -o offset=$((512*210944)) /tmp/a
[root@oravmdr01 xen]# ls /tmp/a
bin  boot  dev  etc  home  lib  lib64  lost+found  media  misc  mnt  net  opt  proc  root  sbin  selinux  srv  stage  sys  tftpboot  tmp  u01  usr  var
============================


3. transfer file system over physical machine

make partition for the physical server hard disk - sda(2x72G raid1)
fdisk /dev/sda
cd /mnt
mkdir sda
mount /dev/sda1 sda
cd sda
ssh remoteserver 'cd /path/to/iso; tar cvpf - . | tar xvpf -'


4. generate mkinitrd and modify some configuration

initrd:

vi /etc/modprobe.conf

alias scsi_hostadapter megaraid_sas
alias scsi_hostadapter1 ata_piix


cd /boot

mkinitrd -f /boot/initrd.img.new 2.6.9xxxxsmp (check folder name under /lib/modules)

vi /boot/grub/grub.conf to modify accordingly to use above generated initrd.img.new

configurations:

cd /mnt/sda
chroot .
modify /etc/sysconfig/network-scritps/ifcfg-eth0 and ifcfg-eth1 if necessary
modify /etc/sysconfig/network
modify /etc/fstab
modify /etc/grub.conf
cp /etc/blkid /etc/blkid.bak
rm -f /etc/blkid/*
cp /etc/mtab /etc/mtab.bak
vi /etc/mtab (to correct the / and /boot line to have the correct partition name)
vi /boot/grub/device.map to something like '(hd0)  /dev/sda'
cd /dev
./MAKEDEV sda


After that, create root, sys and proc directory for cloned virtual server.

cd /mnt/sda
mkdir proc sys root


5. generate MBR

(install it again everytime you modify file /etc/grub.conf)

# grub-install hd0

Or after reboot, at grub prompt:\

root (hd0,0)  -> hd0 means the first hard disk, 0 means /boot directory is sitting on the first partition, if /dev/sda3 is /boot partition, this should be 'root (hd0,2)'

find /grub/stage1
find /grub/stage2
setup (hd0)

reboot

Note: if you cannot use grub-install to install MBR for grub boot loader, you can use RIP CD to boot GRUB from cdrom or usb option to boot the OS on the first hard disk.

then you can fix it from actual os itself.

------
/etc/grub.conf
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Oracle Linux Server (2.6.32-100.26.2.el5uek)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-100.26.2.el5 ro root=/dev/sda3
    initrd /initrd-2.6.32-100.26.2.el5.img 


note:
a. splashimage=(hd0,0)/grub/splash.xpm.gz
means /boot partition is the first partition on first hard disk hd0, it's /grub, not /boot/grub
b. kernel /vmlinuz-2.6.32-100.26.2.el5 ro root=/dev/sda3
means /vmlinuz-2.6.32-100.26.2.el5, not /boot/vmlinuz-2.6.32-100.26.2.el5 since it's already specified root (hd0,0)
c. initrd /initrd-2.6.32-100.26.2.el5.img
means /initrd-2.6.32-100.26.2.el5.img, not /boot/initrd-2.6.32-100.26.2.el5.img if /boot is a separated partition

6. case study

/boot is not separated partition, after clone, it becomes seperated one, you need to change grub configuration as follows:

previous: (no /boot partition, only / one partition)
#boot=/dev/hda
default=0
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title Oracle Linux Server (2.6.32-100.26.2.el5)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.32-100.26.2.el5 ro root=LABEL=/
        initrd /boot/initrd-2.6.32-100.26.2.el5.img

after: (/boot is at /dev/sda3)
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,2)/grub/splash.xpm.gz
hiddenmenu
title Oracle Linux Server (2.6.32-100.26.2.el5)
        root (hd0,2)
        kernel /vmlinuz-2.6.32-100.26.2.el5 ro root=/dev/sda1
        initrd /initrd-2.6.32-100.26.2.el5.img.new


7. possible solution
a. error: /boot/grub/stage1 not read correctly when I tried to install GRUB by grub-install /dev/sdc to the new drive.


Grub consults your /etc/fstab and /etc/mtab files to determine which partition/drive the '/boot' directory is actually on, so check those files are correct. (See the next section for more details about this.)

solution 1: to create a separated /boot partition after boot from RIP CD boot loader, then modify /etc/grub.conf and install grub again:

grub-install /dev/sda

or

find /boot/grub/stage1
root (hd0,0)
find /boot/grub/stage1
setup hd0

solution 2:
Problem might be that the old legacy GRUB can not handle GPT partition tables. You can simply fix this with the tool gptsync:

gptsync /dev/sdc ( not tested)

How to use LVM snapshot to clone CentOS 5 server

Jephe Wu - http://linuxtechres.blogspot.com

Environment: 36G IDE hard disk with CentOS 5.5 default installation which has /boot as /dev/hda1 and the rest are on LVM, this is server A.
Objective: to clone to another same kind of machine by using LVM snapshot. This is server B.
Tools used: RIP(Recovery Is Possible) CD V8.9, create LVM snapshot for / partition before using tar to copy to another machine.

Concept:
1. use RIP to boot up the destination machine
2. create partitions same as the original one and make file system for all partitions
3. use external hard disk or thumb drive on original machine to extend the volume group
4. create LVM snapshot logical volume on original machine before using tar to copy the whole file system
5. use tar on destinaion machine to copy over the whole file system from original machine
6. install grub on destiniation machine. reboot


Steps:
1.  use RIP to boot up the server B, choose skip the keyboard map.
2.  setting up the environment of RIP for network use
login as root without password
passwd root
ifconfig eth0 10.0.0.2 netmask 255.255.255.0 up
/usr/sbin/sshd


3.  copy over the partition configurations from server A (10.0.0.1/24)

ssh 10.0.0.1 'sfdisk -d /dev/hda' | sfdisk [--force] /dev/hda
or
use fdisk -ul /dev/hda on server A to get the sector layout, then configure it on server B

mkfs -t ext3 /dev/hda1
mkfs -t ext3 /dev/VolGroup00/LogVol00
mkswap /dev/VolGroup00/LogVol01
e2label /boot /dev/hda1


4. create snapshot on server A for intact tar backup over ssh.
4.1 extend the current volume group first
use external hard disk or thumb drive as the extra space for snapshot, as long as the thumb drive can hold the extra changes between the time you created snapshot and the time you finish then delete snapshot, thumb drive doesn't have to be same size as hard disk.

How LVM snapshot works?
As soon as you create a snapshot, LVM creates a pool of blocks. I believe that this pool also contains a full copy of the metadata of the volume. When writes happen to the main volume, the block being overwritten is copied to this new pool and the new block is written to the main volume. This is the 'copy-on-write'. Because of this, the more data that gets changed between when a snapshot was taken and the current state of the main volume, the more space will get consumed by that snapshot pool.

4.2 create snapshot (I used a 4G thumb drive as /dev/sda1)

vgextend VolGroup00 /dev/sda1
vgdisplay -v
lvcreate -n backup -l 126 VolGroup00
mkfs -t ext3 /dev/VolGroup00/backup
mkdir /backup
mount /dev/VolGroup00/backup /backup/
vcreate -l 126 -s -n rootsnapshot /dev/VolGroup00/LogVol00


4.3 to remove snapshot later, do:
umount /backup/
lvremove /dev/VolGroup00/backup
vgreduce VolGroup00 /dev/hda1

or
pvremove /dev/sda1
vgreduce --removemissing
vgreduce --removemissing VolGroup00


Note: If the physical volume is still used you will have to migrate the data to another physical volume using pvmove.

5.  copying the whole file system to server B
on server B:
mount /dev/VolGroup00/LogVol00 /mnt/hda2
mount /dev/hda1 /mnt/hda2/boot
scp 10.0.0.1:/etc/passwd /etc/
scp 10.0.0.1:/etc/group /etc/
ssh 10.0.0.1 'cd /backup; tar cvpf - .' | tar xvpf -


6. make grub
cd /mnt/hda2
chroot .
[MAKEDEV hda]
[vgchange -a y] to activate all LVM - optional
grub-install hd0


7. References:

http://www.howtoforge.com/linux_lvm_snapshots

Cloning a physical RHEL 5 server to vmware virtual server

Jephe Wu - http://linuxtechres.blogspot.com

Objective: Cloning a physical RHEL 5 server to vmware virtual server from scratch
Environment: RHEL 5.1 (physical server HP Proliant DL360), CentOS 5.5 (vmware host server)


Steps:

1. Setup the vmware server 2.0 under CentOS 5.5
Please refer to another article at http://linuxtechres.blogspot.com/2010/09/how-to-setup-vmware-server-202-under.html

2. Clone physical RHEL 5.1 to vmware virtual server


a. create a vmware virtual environment, use sda as hard disk and boot it up from system rescue CD iso image
You can download iso file from http://www.sysresccd.org/Main_Page

b. make a tar ball from the original RHEL 5.1 physical server
login as root
cd /
tar --exclude root --exclude sys --exclude proc -cvpzf - . | ssh intermediate_server 'cat > rhel.tar.gz'



c. configure IP address, make partitions and change root password
ifconfig eth0 10.0.0.2 netmask 255.255.255.0 up
passwd root
fdisk /dev/sda (make sda1 as /boot(100M), sda2 as swap(2xRAM size), sda3 as /)
mkfs -t ext3 /dev/sda1
mkfs -t ext3 /dev/sda3
mkswap /dev/sda2


d. start to clone the whole OS

cd /mnt
mkdir sda
mount /dev/sda3 sda
cd sda
mount /dev/sda1 boot



scp  root@original_server:/etc/passwd /etc/

scp root@original_server:/etc/group /etc/

note: above scp commands to copy original server /etc/passwd and /etc/group file to the rescue CD environment so that untaring the rhel.tar.gz will keep the correct owner and permission for all files systems later on. Otherwise, you will notice /var/spool/clientmqueue directory will not be owned by smmsp, as well as the group permission for /usr/sbin/sendmail.sendmail, and so on.


logout then login again

 cd /mnt/sda
ssh root@intermediate_server 'cat rhel.tar.gz' | tar xvpzf - 


or
if on the same LAN,use
tar --exclude root --exclude sys --exclude proc -cvpf - . | ssh intermedia_server 'cd /mnt/sda; tar xpf -'

 Note: if you use RIP 12.3 non-X version, vi /etc/hosts.deny to comment out the last line which only permit localhost to ssh into RIP server, also you might need to vi /etc/ssh/sshd_config to enable root login by using password.



e. modify configurations after cloning OS

cd /mnt/sda
chroot .
modify /etc/sysconfig/network-scritps/ifcfg-eth0 and ifcfg-eth1 if necessary
modify /etc/sysconfig/network
modify /etc/fstab
modify /etc/grub.conf
cp /etc/blkid /etc/blkid.bak
rm -f /etc/blkid/*
cp /etc/mtab /etc/mtab.bak
vi /etc/mtab (to correct the / and /boot line to have the correct partition name)
vi /boot/grub/device.map to something like '(hd0)  /dev/sda'
cd /dev
./MAKEDEV sda


After that, create root, sys and proc directory for cloned virtual server.
cd /mnt/sda
mkdir proc sys root


f. generating new initrd image and install grub

modify /etc/modprobe.conf to have the correct scsi driver, for vmware using sda hard disk, change that file to the follows:

root@jephe # more /etc/modprobe.conf
alias eth0 pcnet32
#alias scsi_hostadapter mptspi
#alias scsi_hostadapter1 mptfc
#alias scsi_hostadapter2 mptsas
alias scsi_hostadapter3 mptbase
alias scsi_hostadapter4 mptscsih
alias scsi_hostadapter5 ata_piix
alias scsi_hostadapter6 usb-storage


then generate new initrd image based on the above scsi drivers (no need to use --preload options to load scsi modules)
root@jephe # mkinitrd -f /boot/initvm.img kernelnumber
error opening /sys/block: No such file or directory
error opening /sys/block: No such file or directory
Note: ignore above errors.

note: you can use commands below to check new initrd content:
mkdir /tmp/initrd
cd /tmp/initrd;gzip -dc /boot/initrd-2.6.22.1-41.fc7.img | cpio -id

Lastly, install grub: (install it again everytime you modify file /etc/grub.conf)
# grub-install hd0


Or after reboot, at grub prompt:


root (hd0,0)
setup (hd0)
reboot

g. reboot
# rm -f /etc/mtab  (already backed up as /etc/mtab.bak at section e)
# reboot

Note: if you encountered issue such as getting grub> prompt only , you need to boot into Windows partition, use command 'chainloader  (hd0,0)+1' to boot into Windows

# more /etc/grub.conf

title windows
   chainloader (hd0,0)+1

Clone HP Proliant DL360 G4p to HP Proliant DL360G6 with CentOS and LVM

Objective: clone HP Proliant DL360 G4p(S) to HP Proliant DL360G6(D)
Environment:

  • CentOS 5.4 64bit and Oracle 11g release 1 64bit were installed on HP Proliant DL360G4p(S) and HP Proliant DL360G6(D) is empty machine.
  • S has 2x160G hard disk as raid1 hardware mirroring, D has 4x300G hard disk as raid5.
  • CentOS on S has configured as LVM2.


Concepts:
1. refering to the article at http://linuxtechres.blogspot.com/2007/08/clone-linux-server-with-lvm2-partition.html
2. Since all HP Proliant storage controller uses Linux cciss driver, so we can clone them although the hard disk size is different.
3. after cloning, you can online resize the partition too since we are using LVM2

Steps:
1. since the hard disks are totally different in terms of type and size. So we cannot use sfdisk to clone partiton tables. We can manually configure partition table on D server, make the /boot as 100M and  the rest as the whole physical volume which is around 800G

bootup with RIP CD 9.3 non-X version in ILO, you can use local iso file in browser ILO, or you can refer to article http://linuxtechres.blogspot.com/2010/03/configure-virtual-media-through-vsp-and.html
boot up with 64bit kernel without keyboard map

fdisk /dev/cciss/c0d0
c0d0p1 as /boot as 100M (default type 83,Linux)
c0d0p2 as LVM partition type (8e)

2. refer to article http://linuxtechres.blogspot.com/2007/08/clone-linux-server-with-lvm2-partition.html

How to convert CentOS root file system to XFS

Jephe Wu - http://linuxtechres.blogspot.com


As you know, the CentOS is a clone of Redhat Enterprise Linux. CentOS doesn't support XFS file system by default which is the same as RHEL, but CentOS provides additional kernel which has built-in XFS support. You can download it to replace the existing kernel to easily get XFS support in kernel space. But the problem is how to make your /, /usr,/var etc partition as XFS file system since you already used ext3 during installation.

This article is to guide you to do that, the following is my testing environments:

CentOS 4.1
partitions:
/dev/hda1 -/boot (ext3)
/dev/hda2 - swap
/dev/hda3 - / (ext3)
/dev/hda5 - /usr (ext3)
/dev/hda6 -/var (ext3)
/dev/hda7 - /tmp (ext3)
/dev/hda8 - /serverdata (ext3)(for storing data later, it's empty after installation)


Our objective:
convert all partitions except for /boot from default file system ext3 to XFS. Grub doesn't support XFS so we leave /boot as ext3.

Clone and XFS convert concept:
a. download XFS-enabled kernel from CentOS and install it
b. use /serverdata partition to save all server OS partitions such as /boot, /,/usr,/var,/tmp
c. boot up server using RIP(Recovery Is Possible) CD
d. format all partitions as XFS
e. copy back all data from /serverdata
f. change /etc/fstab and /boot/initrd image file
g. done. reboot

Steps:

a. download the latest RIP CD at http://www.tux.org/pub/people/kent-robotti/looplinux/rip/
download Non-X version is enough

b. download the XFS enabled supported kernel from CentOS website and install it to the server
# rpm -ivh xfs-enabled-kernel
# vi /etc/grub.conf
to make sure it will boot with the new kernel next time
# reboot
(make sure it can boot normally with new kernel)

c. copy the whole server OS to /serverdata, make xfs file system then copy back
c.1 reboot server with RIP CD after upgrading to xfs enabled kernel, you might want to choose the second option to skip keyboard map
c.2 login as root without password
c.3 make /serverdata partition as XFS first and backup the whole OS to /serverdata
mkfs -t xfs /dev/hda8
cd /mnt
mount /dev/hda3 hd
cd hd
mount /dev/hda1 boot
mount /dev/hda5 usr
mount /dev/hda6 var
mount /dev/hda7 tmp
mount /dev/hda8 serverdata
chroot .
tar --exclude ./boot --exclude ./proc --exclude ./serverdata -cpf - .| (cd serverdata; tar xvpf -)

c.4 umount all partitions then make xfs file systems
exit (exit from chroot environement)
cd /mnt/hd
umount usr
umount var
umount tmp
umount serverdata
umount boot
cd ..
umount hd

mkfs -t xfs -L / /dev/hda3
mkfs -t xfs -L /usr /dev/hda5
mkfs -t xfs -L /var /dev/hda6
mkfs -t xfs -L /tmp /dev/hda7
note: -L to make label for partition, be sure to use the same label name in /etc/fstab.

c.5 mount them again like c.3
cd /mnt
mount /dev/hda3 hd
cd hd
mount /dev/hda1 boot
mount /dev/hda5 usr
mount /dev/hda6 var
mount /dev/hda7 tmp
mount /dev/hda8 serverdata

c.6 copy back the whole OS from backup
(cd serverdata; tar cpf - . ) | tar xvpf -

d. make necessary changes
d.1 change /etc/fstab
cd /mnt/hd
chroot .
vi /etc/fstab (to change file system for those changed from ext3 to xfs, if you didn't use -L option in above mkfs -t xfs for /, /usr,/var,/tmp partitions, you also need to change the LABEL= to the real device name like from LABEL=/usr to /dev/hda5)

d.2 change /boot/initrd image file
cd /mnt/hd
chroot .
cp /boot/initrd.img /tmp/a (use the correct name for your initrd image, I use /boot/initrd.img here)
cd /tmp

zcat a > a1

mkdir a1.dir

cd a1.dir

cpio -iv < ../a1

modify something

vi init ( change ext3 to xfs, add xfs.ko module in init as well as lib/ folder) as follows:
[root@linuxtechres ]# more init
#!/bin/nash
mount -t proc /proc /proc
setquiet
echo Mounted /proc filesystem
echo Mounting sysfs
mount -t sysfs none /sys
echo Creating /dev
mount -o mode=0755 -t tmpfs none /dev
mknod /dev/console c 5 1
mknod /dev/null c 1 3
mknod /dev/zero c 1 5
mknod /dev/hda3 b 3 3 ---> added, hda3 is the root file system device
mkdir /dev/pts
mkdir /dev/shm
echo Starting udev
/sbin/udevstart
echo -n "/sbin/hotplug" > /proc/sys/kernel/hotplug
echo "Loading jbd.ko module"
insmod /lib/jbd.ko
echo "Loading ext3.ko module"
insmod /lib/ext3.ko
echo "Loading xfs.ko module"

insmod /lib/xfs.ko ---> added - please remember to copy xfs.ko file from /lib/modules/kernel_version/kernel/fs/xfs/xfs.ko to /lib
/sbin/udevstart
echo Creating root device
mkrootdev /dev/root
umount /sys
echo Mounting root filesystem
mount -o defaults --ro -t xfs /dev/root /sysroot ---> change ext3 to xfs
mount -t tmpfs --bind /dev /sysroot/dev
echo Switching to new root
switchroot /sysroot
umount /initrd/dev
re-generate initrd.img with xfs support
find . | cpio -co > ../c
cd ..
gzip c
cp c.gz /boot/initrd.xfs.img
change /etc/grub.conf to use this new initrd.xfs.img file.
d.3 install grub
cd /mnt/hd
chroot .
grub-install /dev/hda
After that, umount all partitions, reboot the server.

Clone Linux server with LVM2 partition

Jephe Wu - http://linuxtechres.blogspot.com


You might want to clone your production Linux server to another, but the production server is having LVM2 partition which is enabled by default installation.


1. My test environment is as follows:

Source:
Dell Latitude C510
CentOS 5
IDE 30G HDD
/dev/hda1 mounted as /boot
/dev/VolGroup00/LogVol00 mounted as /
/dev/VolGroup00/LogVol01 is swap partition

Destination:
Acer TravelMate 603TER
IDE 20G HDD

2. Cloning concept
a. use RIP(Recovery Is Possible) CD to boot up Acer laptop
b. make /boot normal partition and LVM2 partition
c. create physical volume, volume group and logical volumes
d. mount logical volumes then clone all files over from the source
e. modify /boot/initrd image file if the destination VG and LV names are different from the source ones, it's recommended to use the same name so that this step can be ignored
f. modify /etc/fstab and /etc/sysconfig/network-scripts/ifcfg-ethX etc
g. done

3. Cloning steps
a. download RIP CD from http://www.tux.org/pub/people/kent-robotti/looplinux/rip/
download smaller non-X version which is about 37M since we don't need X window for cloning

b. boot up the Acer laptop using RIP cd, choose boot up option 2 to skip keyboard map

c. login as root without password

d. Set up the ip address for Acer
# ifconfig eth0 192.168.0.3 up (assuming the source server IP is 192.168.0.2 and they are on the same network)

e. Make partitions for Acer
If the both the source and destination hard disks are the same size, you can just use the following command to clone the partition table over.
# ssh 192.168.0.2 'sfdisk -d /dev/hda' | sfdisk /dev/hda

In my case, I have to do it manually since my Acer HDD is smaller. So I created /dev/hda1(type:83) and /dev/hda2 as LVM(type: 8e)

f. Create logical volumes and make file systems on Acer
# pvcreate /dev/hda2 (initialize it)
# vgcreate VolGroup00 /dev/hda2 (use the same volume group name as the source so that you don't have to modify /boot/initrd image file later)
# vgdisplay -v (to find out the total PE numbers and available space to create logical volumes)
# lvcreate -l 4617 -n LogVol00 VolGroup00 (to create / partition )
# lvcreate -l 128 -n LogVol01 VolGroup00 (for swap partition inside LVM)

note : you don't have to create the same number of extend for each logical volume, you can specify any value, this setting is not fixed somewhere in source server, so you can still be able to startup destination server later.

# mkfs -t ext3 /dev/VolGroup00/LogVol00
# mkswap /dev/VolGroup00/LogVol01
# mkfs -t ext3 /dev/hda1 (for /boot)
# e2label /dev/hda1 /boot (for labelling /boot partition, to be the same as the source which is indicated in /etc/fstab)


g. Cloning over everything from the source
cd /mnt
mkdir hd
mount /dev/VolGroup00/LogVol00 hd
cd hd
mkdir boot proc sys ( boot is a separated partition, and proc and sys are empty folder for kernel memory information)
mount /dev/hda1 boot
ssh 192.168.0.2 'cd /; tar --exclude ./proc --exclude ./sys -cpf - .'| tar xvpf -

h. Make necessary changes for Acer
cd /mnt/hd;chroot .

vi /etc/fstab(optional) (since you are using the same volume group and logical volume names, so you can ignore this step, otherwise, change the names to the new ones)

vi /etc/grub.conf (optional) (since your are using the same volume group and logical volume names, so you can ignore this step, otherwise, change the all root= line to the new logical volume path)

vi /etc/sysconfig/network-scripts/ifcfg-ethx (to change IP address and comment out hardware address line)

i. Install grub for Acer
cd /mnt/hd
chroot .
grub-install hd0
exit

change necessary configuration such as /etc/sysconfig/networking-scripts/ifcfg-eth0 mac address

j. Finishing up
cd /mnt/hd
umount boot
cd /
umount /mnt/hd
reboot

Note:

1. if you forgot to do something, you can always reboot the destination server with RIP CD again, the following are the steps:
login as root after RIP bootup
sh /etc/rc.d/rc.lvm2 start (to activate all volume groups)
(if not, use 'vgchange -a y' to activate all volume groups
cd /mnt
mount /dev/VolGroup00/LogVol00 hd
cd hd
mount /dev/hda1 boot
chroot .
now you can do whatever forgotten changes

2. If you used the different volume group and logical volume names, you need to do some additional changes:

2.1 change /boot/initrd-2.6.18-8.el5.img before rebooting destination
cd /mnt
mount /dev/VolGroup00/LogVol00 hd
cd hd
mount /dev/hda1 boot
chroot .
cp /boot/initrd-2.6.18.el5.img /boot/intrd-2.6.18-el5.img.orig -va (backup it first)
cp /boot/initrd-2.6.18-8.el5.img /tmp/a.img
cd /tmp
zcat a.img > a
mkdir a.dir
cd a.dir
cpio -iv < ../a vi init (to change VolGroup00 to vg0 and LogVol00 to lv0 and LogVol01 to lv1 provided you used the names vg0, lv0 and lv1) find . | cpio -co > ../b
cd ..
gzip b (zip file b as b.gz)
cp b.gz /boot/initrd-2.6.18-8.el5.img
sync
exit
cd /
umount /mnt/hd/boot
umount /mnt/hd
reboot

2.2 change /etc/fstab to reflect the new volume group and logical volume names
2.3 change /etc/grub.conf root= line to use the new names





Cloning a CD under Linux command line


Jephe Wu(linuxtechres.blogspot.com)

It's easy to clone a standard CD under Linux command line, the following are the steps:

1. find out the volume size (assuming your cdrom is /dev/hdc)
# isoinfo -d -i /dev/hdc
record down volume size (e.g. 53488)

2. generate md5sum from CD
# dd if=/dev/hdc bs=2048 count=53488 | md5sum

3. generating iso file from the existing CD
# dd if=/dev/hdc of=/linuxtechres.iso bs=2048 count=53488 conv=notrunc

4. generate md5sum from iso file
# md5sum /linuxtechres.iso
make sure the output is the same as the step 2

5. burn the iso file

# cdrecord dev=/dev/hdc speed=10 padsize=63s -pad -dao -v -eject /linuxtechres.iso

note:
a. always use padsize=63s -pad -dao to generate a CDR which can be used anywhere.
b. do not use the maximum speed your cd writer can support, use a bit lower speed to write.

6. verify the CD
a. put in CDR, run command 'isoinfo -d -i /dev/hdc' to find out volume size value, let's say it's 53488
b. dd if=/dev/hdc bs=2048 count=53488 | md5sum
make sure the output is the same as step 2 and 4.

That's it.

Cloning a Linux server - from IDE to SCSI

Jephe Wu http://linuxtechres.blogspot.com

If your source and destination are the same kind of hard disk, you can refer to the previous articles at cloning a linux server - scenery 2.


In case you need to clone Linux server from ide hard disk to scsi hard disk, after you did the steps that mentioned in the above link, you have to modify initrd.img to include scsi driver.

1. modify initrd.img
I assume you are using 2.6 kernel, the initrd.img format are different if you are using kernel 2.4. Kernel 2.6 for initrd.img is cpio archive after unzip while kernel 2.4 for initrd.img is loopback file system after unzip.

The following are the steps to add scsi driver to initrd.img

1. preparation
cp /boot/initrd.img /tmp/a
cd /tmp
zcat a > a1
mkdir a1.dir
cd a1.dir
cpio -iv < ../a1

or one command like this:
cd /tmp; mkdir initrd; cd initrd
gzip -dc /boot/initrd.img | cpio -id

2. modification
vi init ( add scsi driver modules in init as well as put them in lib/ folder)
after modification, it should be like this:

[root@linuxtechres jephe]# more init
#!/bin/nash
mount -t proc /proc /proc
setquiet
echo Mounted /proc filesystem
echo Mounting sysfs
mount -t sysfs none /sys
echo Creating /dev
mount -o mode=0755 -t tmpfs none /dev
mknod /dev/console c 5 1
mknod /dev/null c 1 3
mknod /dev/zero c 1 5
mknod /dev/sda3 b 8 3 ---> added(assuming /dev/sda3 is your root partition)
mkdir /dev/pts
mkdir /dev/shm
echo Starting udev
/sbin/udevstart
echo -n "/sbin/hotplug" > /proc/sys/kernel/hotplug
echo "Loading jbd.ko module"
insmod /lib/jbd.ko
echo "Loading ext3.ko module"
insmod /lib/ext3.ko

insmod /lib/scsi_mod.ko
insmod /lib/sd_mod.ko
insmod /lib/sr_mod.ko
insmmod /lib/scsi_transport_spi.ko
insmod /lib/sym53c8xx.ko
# note: added above lines for sym53c8xx scsi drivers, please remember to put all necessary files from /lib/modules/KERNEL_VERSION/kernel/drivers/scsi to the above lib folder.

/sbin/udevstart
echo Creating root device
mkrootdev /dev/root
umount /sys
echo Mounting root filesystem
mount -o defaults --ro -t ext3 /dev/root /sysroot
mount -t tmpfs --bind /dev /sysroot/dev
echo Switching to new root
switchroot /sysroot
umount /initrd/dev

3. make changes finally
find . | cpio -co > ../c
cd ..
gzip c
cp c.gz /boot/initrd.img

or

find ./ | cpio -H newc -o > initrd.cpio
gzip initrd.cpio
mv initrd.cpio.gz initrd.img


Note:
1. make cpio -co not 'cpio -o', 'c' is very important parameter
2. make backup for /boot/initrd.img first.

4. modify /etc/modprobe.conf
add the following for scsi driver
alias scsi_hostadapter sym53c8xx

Note: If you are making changes for initrd.img for kernel 2.4, the following are the steps:
cd /boot
cp initrd.img /tmp/a.img -va
cd /tmp
zcat a.img > a
mkdir a.dir
mount -o loop a a.dir
cd a.dir
modify something for file init
cd ..
umount a.dir
cat a | gzip > a.img.new



5. References
http://wiki.openvz.org/Modifying_initrd_image

Cloning Windows server - using RIP and ntfsclone

Jephe Wu http://linuxtechres.blogspot.com


RIP(Recovery Is Possible) CD can also be used for cloning Windows NTFS partition. If the source and destination servers have the same kind of hardwares, you can use the RIP and ntfsclone which comes with RIP CD to clone Windows NTFS partition.

Use ntfsclone to clone Windows server through network

on the source server(HP DL360)
1) properly shutdown your Windows. Boot from RIP CD, choice the second bootup option which loads everything in memory and skip keyboard map
2) ifconfig eth0 192.168.0.1 up (you can set any ip address, I use 192.168.0.1 here)
4) passwd root (to change root password for remote ssh login later)
5) /usr/sbin/sshd (startup ssh daemon)
6) check ntfs partition information with commands below: (optional)
ntfsinfo -m -v /dev/cciss/c0d0p1 | less
ntfsinfo -i -v /dev/cciss/c0d0p1 | less
7) you might need to use command 'ntfsfix' to fix some basic problems such as Windows didn't shutdown properly before cloning.
ntfsfix /dev/cciss/c0d0p1

on the destination server:
1) boot from RIP CD too, same as what you did on the source
2) use 'fdisk' to make new partition(the size must be equal or greater than the server) (use id 7 (hpfs/ntfs).
you can also use 'sfdisk' to clone the partition table over like below if you'd like to have the exact same partition size.
ifconfig eth0 192.168.0.2 up
ssh 192.168.0.1 'sfdisk -d /dev/cciss/c0d0' | sfdisk /dev/cciss/c0d0
3) clone over the MBR code first(excluding partition table info)
ssh 192.168.0.1 'dd if=/dev/cciss/c0d0 count=1 bs=446' | dd of=/dev/cciss/c0d0
It might doesn't bootup properly if the destination server has the different kind of hard disk, you can try step 5 then to install MBR.
4) clone the ntfs partition content over
ssh 192.168.0.1 'ntfsclone -s -o - /dev/cciss/c0d0p1' | ntfsclone -r - -O /dev/cciss/c0d0p1
reboot
5) in case it doesn't boot up. try to boot from windows CD to recovery console mode to run 'fixmbr' to install the proper MBR

Note: if it doesn't boot up, in order to find out it's due to MBR or ntfsclone issue, you can boot with the RIP CD, then choose 'Boot MBR on first hard disk' option to check if Windows can boot up properly.

note: you can also use RIP and MBRFix program to easily fix the MBR, please refer my another article at http://linuxtechres.blogspot.com/2008/09/use-rip-cd-and-mbrfix-to-easily-fix.html

Use ntfsclone to backup Windows ntfs partition
If you want to do backup for the source Windows ntfs partition, you can do the following on the source after booting up with RIP CD

a. backup partition table
sfdisk -d /dev/cciss/c0d0 | ssh remoteserver 'cat > /path/to/c0d0_sfdisk-d'
for restore, use
ssh remoteserver 'cat /path/to/c0d0_sfdisk-d' | sfdisk /dev/cciss/c0d0

b. backup MBR
dd if=/dev/cciss/c0d0 count=1 bs=512 | ssh remoteserver 'dd of=/path/to/mbr_dd'
for restore, use
ssh remoteserver 'dd if=/path/to/mbr_dd' | dd of=/dev/cciss/c0d0

c. backup ntfs partition /dev/cciss/c0d0p1
ntfsclone -s -o - /dev/cciss/c0d0p1 |gzip -c | ssh remoteserver 'cat > /path/to/c0d0p1_ntfsclone.gz'
for restore, use
ssh remoteserver 'gzip -dc
/path/to/c0d0p1_ntfsclone.gz' | ntfsclone -r - -O /dev/cciss/c0d0p1



note: If the original hard disk is bad, you might want to use the parameters below during backup.

--force --ignore-fs-check



References:
a. http://edoceo.com/exemplar/ntfsclone-transfer-windows

Cloning a Linux server - general method

Jephe Wu http://linuxtechres.blogspot.com


Last time, we talked about using dd to do Linux server cloning for the same kind of hardwares - Cloning a Linux server - scenery 1. This time, we will introduce another general method for doing Linux server cloning.

I assume the source is using the normal partition which means it's not using software raid, LVM etc. For cloning Linux server with LVM partition, please check my another article at http://linuxtechres.blogspot.com/2007/08/clone-linux-server-with-lvm2-partition.html . My test environment is as follows:

the source:
a. HP DL380 (Intel Xeon CPU)
b. Redhat Enterprise Linux 4

the destination:
a. HP DL385G2 AMD64

The following are the cloning steps:

1. boot up the destination server from RIP(Recovery Is Possible) CD
You can choose the second options in GRUB bootup menu to load everything into memory and skip keyboard map option, then you can eject CD out before doing the following if you'd like to.

2. configure the IP address for the destination server( we use 192.168.0.101 here)
ifconfig eth0 192.168.0.101 up

2. copy over the 3 files from the source(assuming server name is linuxtechres, the IP is 192.168.0.100)
scp 192.168.0.100:/etc/passwd /etc/
scp 192.168.0.100:/etc/shadow /etc/
scp 192.168.0.100:/etc/group /etc/

3. clone the partition table over using sfdisk
Assuming /dev/cciss/c0d0p1 is / partition, /dev/cciss/c0d0p2 is the /boot partition.

ssh 192.168.0.100 'sfdisk -d /dev/cciss/c0d0' | sfdisk /dev/cciss/c0d0
mkfs -t ext3 /dev/cciss/c0d0p1
e2label /dev/cciss/c0d0p1 /
mkfs -t ext3 /dev/cciss/c0d0p2
e2label /dev/cciss/c0d0p2 /boot
Note: check your source server /etc/fstab so that you know why we use e2label to label the partition / and /boot. That's the default label name by Redhat Enterprise Linux for / and /boot partitions.
cd /mnt
mount /dev/cciss/c0d0p1 hd
cd hd
mkdir boot proc sys mnt ...
mount /dev/cciss/c0d0p2 boot
note: if you have some more partitions like /usr and /home etc, make sure you make file system for all partitions and give the correct label as what the source has, similar as the above steps.

4. clone everything over now
cd /mnt/hd
ssh 192.168.0.100 'cd /; tar --exclude ./proc --exclude ./sys --exclude ./mnt -cpf - .' | tar xvpf -
note: you might want to exclude some data folder or partitions also so that you can finish the cloning process earlier, after that, you can copy the data folder/partition separately. see the step 8
cd etc
rm -f blkid.tab mtab (because they will be generated by OS each time it boots up)
vi sysconfig/network-scripts/ifcfg-eth* ( to comment out hardware address lines and change ip address line to the destination IP)

5. install GRUB (if both the source and the destination are x86 32bit)
cd /mnt/hd
chroot .; grub_install hd0
In my case, I skiped this step and use step 7 to install GRUB since the destination is AMD64 CPU

6. umount those partition that you mounted before
exit (exit from chroot environment)
cd /mnt/hd
umount boot mnt
cd /mnt
umount hd
sync;sync;sync
reboot

7. install GRUB on AMD64 servers
If you are cloning from x86_32 server to x86_64 server like AMD64, you have to
boot from the first x86_64 RedHat Enterprise Linux 4 CD to install GRUB
chroot /mnt/sysimage;grub_install hd0

8. rsync over the data partitions
after the OS boots up, you can use rsync to copy the data folder or partition over.
rsync -e ssh -P -avz source_ip_addr:/data/ /data/

That's it. Now you can bring up one more server which is exactly the same as the source.


Cloning a Linux server - use dd

Jephe Wu http://linuxtechres.blogspot.com

One of the benefits of using Linux is that you don't have to install it every time, you can ALWAYS clone it despite the destination server hardware is the same as the source ones or not. There're two situations as follows:

1) exactly same hardwares.
2) different hardwares

For each situation, there're different ways to clone the servers. Today, I'll introduce how to clone server by using command dd.

In order to use dd. Generally, the destination server will be having the same kind of server model as the source, or at least the same kind of network card and hard disk.

dd will clone everything from source to destination, sector by sector. dd doesn't care about what kind of OS, it can apply to Windows too. The advantage is that you only need to use one command, the disadvantage is that it might take a bit longer since it will copy every sector of your hard disk, even there's no data on those sectors. If the time is not important to you, and the hardware is the almost same, it might be the best choice for you to clone servers.

The following is the steps I use.

a. keep the minimum application running on the source if possible.
Shutdown those services as long as you can. The idea here is to prevent anything from writing things to the hard disk during cloning process. However, according to my experience, it works well even if you don't shutdown anything, just keep the source server running as normal while cloning.

b. use RIP(Recovery is possible) CD to boot up the destination server.
You can download RIP CD from here. The smaller non-X version is enough, around 37M. Burn it to CD, boot up the destination server.

Normally, I choose the second GRUB bootup option for RIP so that I can eject out the CD after boot up, everything will be in memory only.

c. after booting up RIP, login as root(no password is needed), running the following commands to set up IP address:
ifconfig eth0 ip_address_here up (IP address here should be the same network segment as the source)

d. run the following commands to clone:
ssh source_server_ip 'dd if=/dev/sda' | dd of=/dev/sda

note: if the hard disk is HP hardware raid, it might be /dev/cciss/c0d0. Just replace it with yours.

Now you have to wait until it finish.

Note: the dd won't give you any process report as long as the things go well. However, you can use the command

pkill -USR1 ^dd$

to get the progress report, for more info, you can refer to this excellent article at
http://www.redhatmagazine.com/2007/08/16/tips-from-an-rhce-how-can-i-make-dd-give-me-a-progress-report/
(How to make dd to give me progress report)

If you want to check remotely if it finish or not. Then you can run the following commands after setting up ip on destination server and before doing actual cloning:

passwd root (to change root password)
/usr/sbin/sshd (to start sshd daemon for remote login)

You can also boot up the source using RIP CD, then use the above commands to change root password for the source and start sshd daemon for you login from the destination server.

That's it. I'll introduce other methods for cloning servers for same kind of hardwares later.