http://smallken.no-ip.org/blog/2008/02/16/linux/533.html  轉貼

 

一般系要做RAID1時在灌系統時就會規劃好,但是如果機器已經是在跑的呢?

安裝設定RAID重要的套件,如果沒有安裝過,要重開機過才能繼續設定RAID
# yum install mkinitrd mdadm
# reboot

首先要準備兩顆大小一樣的兩顆硬碟,先在第一顆硬碟 /dev/sda 上安裝好系統,第二顆硬碟 /dev/sdb 則還是完全還是空白的硬碟

第一顆硬碟 /dev/sda 的partition分割資訊如下
# fdisk -l /dev/sda
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0×0002c3a7

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          25      200781   83  Linux
/dev/sda2              26          90      522112+  82  Linux swap / Solaris
/dev/sda3              91        1044     7663005   83  Linux

把 /dev/sda 的分割資訊完整地 COPY 到 /dev/sdb
# sfdisk -d /dev/sda | sfdisk /dev/sdb
Successfully wrote the new partition table...

現在第二顆硬碟 /dev/sdb 的partition 分割資訊與第一顆硬碟 /dev/sda 已經一模一樣了

 

# fdisk -l /dev/sdb
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0×000de9c4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1          25      200781   83  Linux
/dev/sdb2              26          90      522112+  82  Linux swap / Solaris
/dev/sdb3              91        1044     7663005   83  Linux

# fdisk /dev/sdb

Command (m for help): m ← 列出功能清單
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): t
Partition number (1-4): 1
Hex code (type L to list codes): L
0  Empty           1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot
.
.
.
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fd  Linux raid auto
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fe  LANstep
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid ff  BBT
1c  Hidden W95 FAT3 75  PC/IX
Hex code (type L to list codes): fd ← 變更成為 Linux raid autodetect 屬性Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes):fd ← 變更成為 Linux raid autodetect 屬性
Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes):  fd ← 變更成為 Linux raid autodetect 屬性
Command (m for help): w ← 儲存

產生 /dev/md0,/dev/md1,/dev/md2 這三個RAID1的虛擬硬碟
# mdadm --create /dev/md0 --level=1 --raid-disks=2 missing /dev/sdb1
# mdadm --create /dev/md1 --level=1 --raid-disks=2 missing /dev/sdb2
# mdadm --create /dev/md2 --level=1 --raid-disks=2 missing /dev/sdb3

確認一下RAID都有被建立起來
# cat /proc/mdstat
Personalities : [raid1]
md2 : active raid1 sdb3[1]
      7662912 blocks [2/1] [_U]
md1 : active raid1 sdb2[1]
      522048 blocks [2/1] [_U]
md0 : active raid1 sdb1[1]
      200704 blocks [2/1] [_U]
unused devices: <none>

格式化/dev/md0, /dev/md1, /dev/md2
# mkfs.ext3 /dev/md0
# mkswap /dev/md1
# mkfs.ext3 /dev/md2

產生設定檔內容直接存成 /etc/mdadm.conf
# mdadm --examine --scan > /etc/mdadm.conf

接著我們要測試一下,/dev/md0跟/dev/md2到底可不可以mount起來?
# mkdir /mnt/md0
# mkdir /mnt/md2
# mount /dev/md0 /mnt/md0
# mount /dev/md2 /mnt/md2

查看所有被mount起來的資訊
# mount
.
.
.
/dev/md0 on /mnt/md0 type ext3 (rw)
/dev/md2 on /mnt/md2 type ext3 (rw)

修改 /etc/fstab
# vi /etc/fstab
/dev/md2                /                       ext3    defaults        1 1
/dev/md0                /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/md1                 swap

# vi /etc/mtab
/dev/md2  / ext3 rw 0 0
proc /proc proc rw 0 0
sysfs /sys sysfs rw 0 0
devpts /dev/pts devpts rw,gid=5,mode=620 0 0
/dev/md0  /boot ext3 rw 0 0
tmpfs /dev/shm tmpfs rw 0 0
none /proc/sys/fs/binfmt_misc binfmt_misc rw 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw 0 0

# vi /boot/grub/menu.lst
default=0
fallback=1
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Fedora (2.6.23.1-42.fc8)
        root (hd1,0)
        kernel /vmlinuz-2.6.23.1-42.fc8 ro root=LABEL=/ rhgb quiet
        initrd /initrd-2.6.23.1-42.fc8.img
title Fedora (2.6.23.1-42.fc8)
        root (hd0,0)
        kernel /vmlinuz-2.6.23.1-42.fc8 ro root=LABEL=/ rhgb quiet
        initrd /initrd-2.6.23.1-42.fc8.img

備份initrd
# mv /boot/initrd-`uname -r`.img /boot/initrd-`uname -r`.img_orig

重新產生 initrd
# mkinitrd /boot/initrd-`uname -r`.img `uname -r`

硬碟資料對拷
# cp -dpRx / /mnt/md2
# cd /boot
# cp -dpRx . /mnt/md0

# grub
grub> root (hd0,0)
Filesystem type is ext2fs, partition type 0×83
grub> setup (hd0)
grub> root (hd1,0)
Filesystem type is ext2fs, partition type 0xfd
grub> setup (hd1)
grub> quit

# reboo

重新開機,選用第二個開機選單

成功開機後,確認一下環境
# df -h
檔案系統              容量  已用 可用 已用% 掛載點
/dev/md2              7.1G  2.5G  4.3G  37% /
/dev/md0              190M   15M  166M   9% /boot
tmpfs                 125M     0  125M   0% /dev/shm
# cat /proc/mdstat
md0 : active raid1 sdb1[1]
      200704 blocks [2/1] [_U]

md1 : active raid1 sdb2[1]
      522048 blocks [2/1] [_U]

md2 : active raid1 sdb3[1]
      7662912 blocks [2/1] [_U]

unused devices: <none>

比照之前 /dev/sdb 的動作,修改成 Linux raid autodetect
# fdisk /dev/sda
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid autodetect)

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): fd
Changed system type of partition 2 to fd (Linux raid autodetect)

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): fd
Changed system type of partition 3 to fd (Linux raid autodetect)

Command (m for help): w
The partition table has been altered!

再依依將3個partition加入RAID中
# mdadm --add /dev/md0 /dev/sda1
# mdadm --add /dev/md1 /dev/sda2
# mdadm --add /dev/md2 /dev/sda3

# cat /proc/mdstat

# mdadm --examine --scan > /etc/mdadm.conf

# vi /boot/grub/menu.lst
default=0
fallback=1
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Fedora (2.6.23.1-42.fc8)
        root (hd0,0)
        kernel /vmlinuz-2.6.23.1-42.fc8 ro root=LABEL=/ rhgb quiet
        initrd /initrd-2.6.23.1-42.fc8.img
title Fedora (2.6.23.1-42.fc8)
        root (hd1,0)
        kernel /vmlinuz-2.6.23.1-42.fc8 ro root=LABEL=/ rhgb quiet
        initrd /initrd-2.6.23.1-42.fc8.img

備份 initrd
|# mv /boot/initrd-`uname -r`.img /boot/initrd-`uname -r`.img_orig2

重新產生 inirtd
# mkinitrd /boot/initrd-`uname -r`.img `uname -r`

# reboot

測試

手動讓硬碟失效
# mdadm --manage /dev/md0 --fail /dev/sdb1
移除加入RAID
# mdadm --manage /dev/md0 --remove /dev/sdb1

加入RAID
# mdadm -a /dev/md0 /dev/sdb1

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 安德森 的頭像
    安德森

    安德森技術備忘板

    安德森 發表在 痞客邦 留言(0) 人氣()