迁移linux到不同的硬盘

背景和问题:

这两天用 Virtualbox 搭建 Oracle 11G R2 RAC 环境,由于刚开始划分的虚拟硬盘太小,安装完 GRID 后空间就只剩下不到 1G ,这肯定是不够用的,所以必须增加磁盘容量。

首先考虑通过 Virtualbox 扩展虚拟硬盘,后来发现 Virtualbox 虚拟硬盘的大小不能改变,此路不通。

然后考虑新增硬盘,扩展 VG 后,再扩展对应的 LV ,由于虚拟机需要在我的机器上配好后,需要虚拟硬盘拷到其他机器上的虚拟机上面运行操作系统,如果增加硬盘的话会带来很多不便,所以此法不予考虑。

最后考虑新建一更大的虚拟硬盘,然后把操作系统整体备份然后恢复到新建的虚拟硬盘上去。这个看起来是多么好的一个解决方案却让我付出了两天的辛苦。这中间遇到了 N 多的问题,但不管怎样最后还是实现了自己的想法,下面简单介绍一下,希望对大家有所帮助。

 

关键步骤:

Ø         新增两块硬盘,一块作为我们新的大容量的操作系统盘 (/dev/hdd) ,一块用来临时保存操作系统的备份数据 (/dev/hdb)

Ø         备份操作系统到备份盘。

Ø         恢复操作系统到新的大容量系统盘。

Ø         重新生成 initrd 文件,为新的大容量系统盘安装 GRUB

 

说明:

因为在同一个操作系统下,不能存在名字相同的卷组,所以新建的卷组名字为 VolGroup01 ,旧的操作系统卷组为 VolGroup00 ,这也是恢复到不同名字逻辑卷的主要原因。

 

操作系统的硬盘分二个分区,一个用来做 BOOT 分区,保存 /boot 文件系统,一个用来作为主卷组 ( 例如 VolGroup00) PV ;这个主卷组划分 2 个逻辑卷,一个用来做交换 (swap), 一个用来做根 (/) 文件系统。

 

详细步骤:

 

1)      为虚拟机新增两块硬盘,启动操作系统

2)      查看硬盘

[root@localhost ~]# fdisk -l

 

Disk /dev/hda: 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/hda1   *           1          13      104391   83  Linux

/dev/hda2              14        1305    10377990   8e  Linux LVM

 

Disk /dev/hdb: 21.4 GB, 21474836480 bytes

16 heads, 63 sectors/track, 41610 cylinders

Units = cylinders of 1008 * 512 = 516096 bytes

 

Disk /dev/hdb doesn't contain a valid partition table

 

Disk /dev/hdd: 21.4 GB, 21474836480 bytes

16 heads, 63 sectors/track, 41610 cylinders

Units = cylinders of 1008 * 512 = 516096 bytes

 

Disk /dev/hdd doesn't contain a valid partition table

 

 

 

HDA 为原有的操作系统盘

HDB BAK

HDC 为光驱

HDD RAC-LARGE

 

 

3)       为新硬盘分区

 

[root@localhost ~]# fdisk /dev/hdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel. Changes will remain in memory only,

until you decide to write them. After that, of course, the previous

content won't be recoverable.

 

 

The number of cylinders for this disk is set to 41610.

There is nothing wrong with that, but this is larger than 1024,

and could in certain setups cause problems with:

1) software that runs at boot time (e.g., old versions of LILO)

2) booting and partitioning software from other OSs

   (e.g., DOS FDISK, OS/2 FDISK)

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

 

Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 1

First cylinder (1-41610, default 1):

Using default value 1

Last cylinder or +size or +sizeM or +sizeK (1-41610, default 41610):

Using default value 41610

 

Command (m for help): w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

[root@localhost ~]# fdisk /dev/hdd

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel. Changes will remain in memory only,

until you decide to write them. After that, of course, the previous

content won't be recoverable.

 

 

The number of cylinders for this disk is set to 41610.

There is nothing wrong with that, but this is larger than 1024,

and could in certain setups cause problems with:

1) software that runs at boot time (e.g., old versions of LILO)

2) booting and partitioning software from other OSs

   (e.g., DOS FDISK, OS/2 FDISK)

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

 

Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 1

First cylinder (1-41610, default 1):

Using default value 1

Last cylinder or +size or +sizeM or +sizeK (1-41610, default 41610): +100M   

 

Command (m for help): n

Command action

    e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 2

First cylinder (196-41610, default 196):

Using default value 196

Last cylinder or +size or +sizeM or +sizeK (196-41610, default 41610):

Using default value 41610

 

Command (m for help): w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

 

4)      为备份硬盘创建文件系统

[root@localhost ~]# mkfs.ext3 /dev/hdb1

mke2fs 1.39 (29-May-2006)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

2621440 inodes, 5242852 blocks

262142 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=0

160 block groups

32768 blocks per group, 32768 fragments per group

16384 inodes per group

Superblock backups stored on blocks:

        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,

        4096000

 

Writing inode tables: done                           

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done

 

This filesystem will be automatically checked every 26 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

 

5)      创建新的逻辑卷

[root@localhost ~]# pvscan

  PV /dev/hda2   VG VolGroup00   lvm2 [9.88 GB / 0    free]

  Total: 1 [9.88 GB] / in use: 1 [9.88 GB] / in no VG: 0 [0   ]

[root@localhost ~]# pvcreate /dev/hdd2

  Physical volume "/dev/hdd2" successfully created

[root@localhost ~]# pvscan

  PV /dev/hda2   VG VolGroup00   lvm2 [9.88 GB / 0    free]

  PV /dev/hdd2                   lvm2 [19.91 GB]

  Total: 2 [29.78 GB] / in use: 1 [9.88 GB] / in no VG: 1 [19.91 GB]

 

[root@localhost ~]# vgcreate  VolGroup01 /dev/hdd2

  Volume group "VolGroup01" successfully created

[root@localhost ~]# vgdisplay

  --- Volume group ---

  VG Name               VolGroup01

  System ID            

  Format                lvm2

  Metadata Areas        1

  Metadata Sequence No  1

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                0

  Open LV               0

  Max PV                0

  Cur PV                1

  Act PV                1

  VG Size               19.90 GB

  PE Size               4.00 MB

  Total PE              5095

  Alloc PE / Size       0 / 0  

  Free  PE / Size       5095 / 19.90 GB

  VG UUID               M25Xcj-Dvep-saMc-MMRE-rpnQ-YH5F-7R4zdo

  

  --- 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               evR4SI-sgGS-uXL2-U54G-T1qu-vKdJ-BP9sdI

 

[root@localhost ~]# vgdisplay

  --- Volume group ---

  VG Name               VolGroup01

  System ID            

  Format                lvm2

  Metadata Areas        1

  Metadata Sequence No  2

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                1

  Open LV               0

  Max PV                0

  Cur PV                1

  Act PV                1

  VG Size                19.90 GB

  PE Size               4.00 MB

  Total PE              5095

  Alloc PE / Size       4608 / 18.00 GB

  Free  PE / Size       487 / 1.90 GB

  VG UUID               M25Xcj-Dvep-saMc-MMRE-rpnQ-YH5F-7R4zdo

  

  --- 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               evR4SI-sgGS-uXL2-U54G-T1qu-vKdJ-BP9sdI

  

[root@localhost ~]# lvcreate -n LogVol01 -l 487  VolGroup01

  Logical volume "LogVol01" created

[root@localhost ~]# vgdisplay

  --- Volume group ---

  VG Name               VolGroup01

  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               0

  Max PV                0

  Cur PV                1

  Act PV                1

  VG Size               19.90 GB

  PE Size               4.00 MB

  Total PE              5095

  Alloc PE / Size       5095 / 19.90 GB

  Free  PE / Size       0 / 0  

  VG UUID                M25Xcj-Dvep-saMc-MMRE-rpnQ-YH5F-7R4zdo

  

  --- 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               evR4SI-sgGS-uXL2-U54G-T1qu-vKdJ-BP9sdI

 

 

[root@localhost ~]# mkfs.ext3 /dev/mapper/VolGroup01-LogVol00

mke2fs 1.39 (29-May-2006)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

2359296 inodes, 4718592 blocks

235929 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=0

144 block groups

32768 blocks per group, 32768 fragments per group

16384 inodes per group

Superblock backups stored on blocks:

        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,

        4096000

 

Writing inode tables: done                           

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done

 

This filesystem will be automatically checked every 21 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

 

 

 

6)      备份 / 文件系统

[root@localhost ~]# mount /dev/hdb1 /media/

[root@localhost /]# dump -0f /media/rootfs-20100610.bak /

  DUMP: Date of this level 0 dump: Thu Jun 10 05:13:00 2010

  DUMP: Dumping /dev/mapper/VolGroup00-LogVol00 (/) to /media/rootfs-20100610.bak

  DUMP: Label: none

  DUMP: Writing 10 Kilobyte records

  DUMP: mapping (Pass I) [regular files]

  DUMP: mapping (Pass II) [directories]

  DUMP: estimated 2506553 blocks.

  DUMP: Volume 1 started with block 1 at: Thu Jun 10 05:13:08 2010

  DUMP: dumping (Pass III) [directories]

  DUMP: dumping (Pass IV) [regular files]

  DUMP: 29.49% done at 2463 kB/s, finished in 0:11

  DUMP: 84.27% done at 3520 kB/s, finished in 0:01

  DUMP: Closing /media/rootfs-20100610.bak

  DUMP: Volume 1 completed at: Thu Jun 10 05:26:38 2010

  DUMP: Volume 1 3017980 blocks (2947.25MB)

  DUMP: Volume 1 took 0:13:30

  DUMP: Volume 1 transfer rate: 3725 kB/s

  DUMP: 3017980 blocks (2947.25MB) on 1 volume(s)

  DUMP: finished in 809 seconds, throughput 3730 kBytes/sec

  DUMP: Date of this level 0 dump: Thu Jun 10 05:13:00 2010

  DUMP: Date this dump completed:  Thu Jun 10 05:26:38 2010

  DUMP: Average transfer rate: 3725 kB/s

  DUMP: DUMP IS DONE

7)      备份 /boot 文件系统

[root@localhost /]# dump -0f /media/bootfs-20100610.bak /boot/

  DUMP: Date of this level 0 dump: Thu Jun 10 05:29:56 2010

  DUMP: Dumping /dev/hda1 (/boot) to /media/bootfs-20100610.bak

  DUMP: Label: /boot

  DUMP: Writing 10 Kilobyte records

  DUMP: mapping (Pass I) [regular files]

  DUMP: mapping (Pass II) [directories]

  DUMP: estimated 6047 blocks.

  DUMP: Volume 1 started with block 1 at: Thu Jun 10 05:29:56 2010

  DUMP: dumping (Pass III) [directories]

  DUMP: dumping (Pass IV) [regular files]

  DUMP: Closing /media/bootfs-20100610.bak

  DUMP: Volume 1 completed at: Thu Jun 10 05:29:57 2010

  DUMP: Volume 1 6100 blocks (5.96MB)

  DUMP: Volume 1 took 0:00:01

  DUMP: Volume 1 transfer rate: 6100 kB/s

  DUMP: 6100 blocks (5.96MB) on 1 volume(s)

  DUMP: finished in 1 seconds, throughput 6100 kBytes/sec

  DUMP: Date of this level 0 dump: Thu Jun 10 05:29:56 2010

  DUMP: Date this dump completed:  Thu Jun 10 05:29:57 2010

  DUMP: Average transfer rate: 6100 kB/s

  DUMP: DUMP IS DONE

 

 

8)      恢复 / 文件系统到逻辑卷 VolGroup01-LogVol00

[root@localhost /]# mount /dev/mapper/VolGroup01-LogVol00 /mnt

[root@localhost /]# cd /mnt

  [root@localhost mnt]# restore -rf /media/rootfs-20100610.bak

restore: ./lost+found: File exists

 

9)      恢复 /boot 文件系统到分区 /dev/hdd1

[root@localhost /]# mkfs.ext3  /dev/hdd1

mke2fs 1.39 (29-May-2006)

Filesystem label=

OS type: Linux

Block size=1024 (log=0)

Fragment size=1024 (log=0)

24576 inodes, 98248 blocks

4912 blocks (5.00%) reserved for the super user

First data block=1

Maximum filesystem blocks=67371008

12 block groups

8192 blocks per group, 8192 fragments per group

2048 inodes per group

Superblock backups stored on blocks:

        8193, 24577, 40961, 57345, 73729

 

Writing inode tables: done                           

Creating journal (4096 blocks): done

Writing superblocks and filesystem accounting information: done

 

This filesystem will be automatically checked every 28 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@localhost /]# mount /dev/hdd1 /mnt

[root@localhost /]# cd /mnt

[root@localhost mnt]# restore -rf /media/bootfs-20100610.bak

restore: ./lost+found: File exists

 

 

10) GRUB 安装

[root@localhost /]# grub-install --root-directory=/mnt /dev/hdd

/dev/hdd does not have any corresponding BIOS drive.

[root@localhost /]# cat /mnt/boot/grub/device.map

# this device map was generated by anaconda

(hd0)     /dev/hda

[root@localhost /]# vi /mnt/boot/grub/device.map

# this device map was generated by anaconda

(hd0)     /dev/hdd

[root@localhost /]# grub-install --root-directory=/mnt /dev/hdd

Installation finished. No error reported.

This is the contents of the device map /mnt/boot/grub/device.map.

Check if this is correct or not. If any of the lines is incorrect,

fix it and re-run the script `grub-install'.

 

# this device map was generated by anaconda

(hd0)     /dev/hdd

 

标记、 /dev/hdd1 为可启动

[root@localhost /]# fdisk /dev/hdd

 

The number of cylinders for this disk is set to 41610.

There is nothing wrong with that, but this is larger than 1024,

and could in certain setups cause problems with:

1) software that runs at boot time (e.g., old versions of LILO)

2) booting and partitioning software from other OSs

   (e.g., DOS FDISK, OS/2 FDISK)

 

Command (m for help): p

 

Disk /dev/hdd: 21.4 GB, 21474836480 bytes

16 heads, 63 sectors/track, 41610 cylinders

Units = cylinders of 1008 * 512 = 516096 bytes

 

   Device Boot      Start         End      Blocks   Id  System

/dev/hdd1               1         195       98248+  83  Linux

/dev/hdd2             196       41610    20873160   83  Linux

 

Command (m for help): ?

?: unknown command

Command action

   a   toggle a bootable flag

   b   edit bsd disklabel

   c   toggle the dos compatibility flag

   d   delete a partition

   l   list known partition types

   m   print this menu

   n   add a new partition

   o   create a new empty DOS partition table

   p   print the partition table

   q   quit without saving changes

   s   create a new empty Sun disklabel

   t   change a partition's system id

   u   change display/entry units

   v   verify the partition table

   w   write table to disk and exit

   x   extra functionality (experts only)

 

Command (m for help): a

Partition number (1-4): 1

 

Command (m for help): w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

 

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.

The kernel still uses the old table.

The new table will be used at the next reboot.

Syncing disks.

 

11) 修改 initrd.img /boot/grub/grub.conf,/etc/fstab 文件,反应新的卷组名

[root@localhost ~]# mount /dev/VolGroup01/LogVol00 /mnt

[root@localhost ~]# cd /tmp

[root@localhost tmp]# mkdir workArea

[root@localhost tmp]# cp /mnt/boot/initrd-2.6.18-53.el5.img  /tmp/workArea/initrd-2.6.18-53.el5.img.gz

[root@localhost tmp]# cd workArea/

[root@localhost workArea]# gunzip initrd-2.6.18-53.el5.img.gz ;cpio -idmv < initrd-2.6.18-53.el5.img

bin

bin/nash

bin/kpartx

bin/modprobe

bin/insmod

bin/dmraid

bin/lvm

sys

etc

etc/lvm

etc/lvm/lvm.conf

init

lib

lib/sd_mod.ko

lib/uhci-hcd.ko

lib/dm-snapshot.ko

lib/firmware

lib/dm-zero.ko

lib/ext3.ko

lib/scsi_mod.ko

lib/ohci-hcd.ko

lib/dm-mod.ko

lib/libata.ko

lib/dm-mirror.ko

lib/jbd.ko

lib/ehci-hcd.ko

lib/ata_piix.ko

proc

sysroot

dev

dev/systty

dev/null

dev/rtc

dev/tty4

dev/tty

dev/tty2

dev/console

dev/mapper

dev/tty1

dev/tty3

dev/zero

dev/tty8

dev/tty5

dev/tty9

dev/ttyS3

dev/ram

dev/ttyS2

dev/tty7

dev/tty0

dev/ttyS0

dev/ttyS1

dev/tty11

dev/ram0

dev/tty10

dev/tty12

dev/ram1

dev/tty6

dev/ptmx

sbin

13415 blocks

修改 ini 文件中的 VolGroup00 VolGroup01

 

[root@localhost workArea]# rm initrd-2.6.18-53.el5.img

rm: remove regular file `initrd-2.6.18-53.el5.img'? y

[root@localhost workArea]# find .|cpio -c -o > ../initrd-2.6.18-53.el5.img

13415 blocks

[root@localhost workArea]# cd ..

[root@localhost tmp]# gzip initrd-2.6.18-53.el5.img

[root@localhost tmp]# cp initrd-2.6.18-53.el5.img.gz /mnt/boot/initrd-2.6.18-53.el5.img

cp: overwrite `/mnt/boot/initrd-2.6.18-53.el5.img'? y

 

 

12) 关闭虚拟机,设置 rac1-large.vdi 为第一 IDE 控制器主通道上的设备,重启虚拟机即可实现引导。至此完成操作系统到不同名字的逻辑卷的恢复。

下面可以看到系统根文件系统是在新的逻辑卷上

[root@localhost ~]# mount

/dev/mapper/VolGroup01-LogVol00 on / type ext3 (rw)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw)

devpts on /dev/pts type devpts (rw,gid=5,mode=620)

/dev/hdc1 on /boot type ext3 (rw)

tmpfs on /dev/shm type tmpfs (rw)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

 

EMAIL: [email protected]

有什么疑问欢迎咨询。

 

你可能感兴趣的:(迁移linux到不同的硬盘)