(源自網絡)Linux Software RAID 1 實作
- 詳細內容
- 分類: Linux
- 發佈: 2008-12-10, 週三 09:24
- 作者 Super User
- 點擊數: 23188
Linux Software RAID 1 實作
於單顆硬碟的系統加上另一顆硬碟, 做成 Software RAID 1
狀況測試:
指令熱插拔
摘除一顆硬碟
掛回摘除的硬碟
摘除其中一顆硬碟, 以另一顆全新硬碟取代
取消 Software RAID 1, 回到最原始的單顆硬碟環境
環境
於單顆硬碟的系統加上另一顆硬碟, 做成 Software RAID 1
狀況測試:
指令熱插拔
摘除一顆硬碟
掛回摘除的硬碟
摘除其中一顆硬碟, 以另一顆全新硬碟取代
取消 Software RAID 1, 回到最原始的單顆硬碟環境
環境
Fedora Core 6 (on VMware Server)
/etc/sysconfig/selinux → SELINUX=disabled
fdisk -l
Disk /dev/sda: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
/etc/sysconfig/selinux → SELINUX=disabled
fdisk -l
Disk /dev/sda: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 456 3662788+ 83 Linux
/dev/sda2 457 521 522112+ 82 Linux swap / Solaris
/dev/sda1 * 1 456 3662788+ 83 Linux
/dev/sda2 457 521 522112+ 82 Linux swap / Solaris
Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
(第二顆硬碟是空的, 尚未分割磁區)操作步驟
#安裝所需工具
yum -y install mkinitrd mdadm
yum -y install mkinitrd mdadm
#將第一顆硬碟的磁碟分割表複製給第二顆硬碟
sfdisk -d /dev/sda | sfdisk /dev/sdb
sfdisk -d /dev/sda | sfdisk /dev/sdb
fdisk -l
Disk /dev/sda: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 456 3662788+ 83 Linux
/dev/sda2 457 521 522112+ 82 Linux swap / Solaris
/dev/sda1 * 1 456 3662788+ 83 Linux
/dev/sda2 457 521 522112+ 82 Linux swap / Solaris
Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 * 1 456 3662788+ 83 Linux
/dev/sdb2 457 521 522112+ 82 Linux swap / Solarisfdisk /dev/sdb
/dev/sdb1 * 1 456 3662788+ 83 Linux
/dev/sdb2 457 521 522112+ 82 Linux swap / Solarisfdisk /dev/sdb
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)
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)
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): w
#清除 RAID superblock
mdadm --zero-superblock /dev/sdb1
mdadm --zero-superblock /dev/sdb2
mdadm --zero-superblock /dev/sdb1
mdadm --zero-superblock /dev/sdb2
#Fedora Core 6 預設只有 /dev/md0, 但我們有兩個 partition, 所以手動再建一個 md1
mknod /dev/md1 b 9 1
mknod /dev/md1 b 9 1
#建立 RAID 1
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/md0 --level=1 --raid-disks=2 missing /dev/sdb1
mdadm --create /dev/md1 --level=1 --raid-disks=2 missing /dev/sdb2
#查看 RAID 狀態: RAID 1 已經建立, 目前只有 /dev/sdb 掛入 ARRAY
cat /proc/mdstat
cat /proc/mdstat
Personalities : [raid1]
md1 : active raid1 sdb2[1]
522048 blocks [2/1] [_U]
md0 : active raid1 sdb1[1]
3662720 blocks [2/1] [_U]
unused devices: <none>#產生 RAID 設定檔
cp /usr/share/doc/mdadm-2.5.4/mdadm.conf-example /etc/mdadm.conf
mdadm --examine --scan >> /etc/mdadm.conf
md1 : active raid1 sdb2[1]
522048 blocks [2/1] [_U]
md0 : active raid1 sdb1[1]
3662720 blocks [2/1] [_U]
unused devices: <none>#產生 RAID 設定檔
cp /usr/share/doc/mdadm-2.5.4/mdadm.conf-example /etc/mdadm.conf
mdadm --examine --scan >> /etc/mdadm.conf
#格式化第二顆硬碟的磁區, 並將第二顆硬碟的 root ( / ) 掛載到 /mnt/md0
mkfs.ext3 /dev/md0
mkswap /dev/md1
mkdir /mnt/md0
mount /dev/md0 /mnt/md0
mkfs.ext3 /dev/md0
mkswap /dev/md1
mkdir /mnt/md0
mount /dev/md0 /mnt/md0
#修改相關設定, 準備以第二顆硬碟 (/dev/md0) 開機
vi /etc/fstab
/dev/md0 / ext3 defaults 1 1
/dev/md1 swap swap defaults 0 0
vi /etc/fstab
/dev/md0 / ext3 defaults 1 1
/dev/md1 swap swap defaults 0 0
vi /boot/grub/menu.lst
default=0
timeout=5
title Fedora Core (2.6.18-1.2798.fc6)
root (hd1,0)
kernel /boot/vmlinuz-2.6.18-1.2798.fc6 ro root=/dev/md0
initrd /boot/initrd-2.6.18-1.2798.fc6-raid.img
default=0
timeout=5
title Fedora Core (2.6.18-1.2798.fc6)
root (hd1,0)
kernel /boot/vmlinuz-2.6.18-1.2798.fc6 ro root=/dev/md0
initrd /boot/initrd-2.6.18-1.2798.fc6-raid.img
#將 Software RAID Module 載入 initrd.img
mkinitrd --preload raid1 /boot/initrd-`uname -r`-raid.img `uname -r`
mkinitrd --preload raid1 /boot/initrd-`uname -r`-raid.img `uname -r`
#將第一顆硬碟裡的資料全數複製到第二顆硬碟
cp -ax / /mnt/md0
cp -ax / /mnt/md0
#將 GRUB 寫入第二顆硬碟
grub
grub>root (hd1,0)
grub>setup (hd1)
grub>quit
grub
grub>root (hd1,0)
grub>setup (hd1)
grub>quit
reboot
#將第一顆硬碟的 Partition Type 改為 Linux RAID
fdisk /dev/sda
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)
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)
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): w
#把第一顆硬碟的磁區加入 RAID
mdadm --add /dev/md1 /dev/sda2
mdadm --add /dev/md0 /dev/sda1
mdadm --add /dev/md1 /dev/sda2
mdadm --add /dev/md0 /dev/sda1
#監看 RAID Recovery 的進度
watch cat /proc/mdstat
watch cat /proc/mdstat
Personalities : [raid1]
md1 : active raid1 sda2[0] sdb2[1]
522048 blocks [2/2] [UU]
md0 : active raid1 sda1[2] sdb1[1]
3662720 blocks [2/1] [_U]
[=>...................] recovery = 9.7% (358400/3662720) finish=4.4min speed=12360K/sec
unused devices: <none>
完成後↓
md1 : active raid1 sda2[0] sdb2[1]
522048 blocks [2/2] [UU]
md0 : active raid1 sda1[2] sdb1[1]
3662720 blocks [2/1] [_U]
[=>...................] recovery = 9.7% (358400/3662720) finish=4.4min speed=12360K/sec
unused devices: <none>
完成後↓
Personalities : [raid1]
md1 : active raid1 sda2[0] sdb2[1]
522048 blocks [2/2] [UU]
md1 : active raid1 sda2[0] sdb2[1]
522048 blocks [2/2] [UU]
md0 : active raid1 sda1[0] sdb1[1]
3662720 blocks [2/2] [UU]
3662720 blocks [2/2] [UU]
unused devices: <none>
#修改 GRUB root device
#其實完成 RAID 1 後, 不管是 root (hd0,0) 還是 root (hd1,0) 都能開機, 但改成 hd0 較好, 因為若其中有一顆掛點, 只剩一顆硬碟開機的時候, 那顆硬碟就是 hd0 啦!
vi /boot/grub/menu.lst
#其實完成 RAID 1 後, 不管是 root (hd0,0) 還是 root (hd1,0) 都能開機, 但改成 hd0 較好, 因為若其中有一顆掛點, 只剩一顆硬碟開機的時候, 那顆硬碟就是 hd0 啦!
vi /boot/grub/menu.lst
default=0
timeout=5
title Fedora Core (2.6.18-1.2798.fc6)
root (hd0,0)
kernel /boot/vmlinuz-2.6.18-1.2798.fc6 ro root=/dev/md0
initrd /boot/initrd-2.6.18-1.2798.fc6-raid.img
timeout=5
title Fedora Core (2.6.18-1.2798.fc6)
root (hd0,0)
kernel /boot/vmlinuz-2.6.18-1.2798.fc6 ro root=/dev/md0
initrd /boot/initrd-2.6.18-1.2798.fc6-raid.img
狀況測試
指令熱插拔:
mdadm --manage /dev/md0 --fail /dev/sdb1 --remove /dev/sdb1
mdadm --add /dev/md0 /dev/sdb1
watch cat /proc/mdstat 觀察 recovery 進度
摘除其中一顆硬碟, 開機後一樣可以正常運作
掛回剛剛摘除的硬碟, 開機後查看 /proc/mdstat, 把缺少的那顆再掛載進來:
例如: mdadm --add /dev/md0 /dev/sda1
掛載後, cat /proc/mdstat 可看到系統正在進行 recovery
摘除其中一顆硬碟, 以另一顆全新硬碟取代 (新硬碟當第二顆), 開機後執行:
sfdisk -d /dev/sda | sfdisk /dev/sdb
mdadm --add /dev/md1 /dev/sdb2
mdadm --add /dev/md0 /dev/sdb1
watch cat /proc/mdstat 靜候 recovery 作業完成
mdadm --manage /dev/md0 --fail /dev/sdb1 --remove /dev/sdb1
mdadm --add /dev/md0 /dev/sdb1
watch cat /proc/mdstat 觀察 recovery 進度
摘除其中一顆硬碟, 開機後一樣可以正常運作
掛回剛剛摘除的硬碟, 開機後查看 /proc/mdstat, 把缺少的那顆再掛載進來:
例如: mdadm --add /dev/md0 /dev/sda1
掛載後, cat /proc/mdstat 可看到系統正在進行 recovery
摘除其中一顆硬碟, 以另一顆全新硬碟取代 (新硬碟當第二顆), 開機後執行:
sfdisk -d /dev/sda | sfdisk /dev/sdb
mdadm --add /dev/md1 /dev/sdb2
mdadm --add /dev/md0 /dev/sdb1
watch cat /proc/mdstat 靜候 recovery 作業完成
附錄: 取消 RAID 1, 回到最原始的單顆硬碟環境
#設定以第二顆硬碟開機
vi /boot/grub/menu.lst
default=0
timeout=5
title Fedora Core (2.6.18-1.2798.fc6)
root (hd1,0)
kernel /boot/vmlinuz-2.6.18-1.2798.fc6 ro root=/dev/sdb1
initrd /boot/initrd-2.6.18-1.2798.fc6.img (不是 -raid 那個)
vi /boot/grub/menu.lst
default=0
timeout=5
title Fedora Core (2.6.18-1.2798.fc6)
root (hd1,0)
kernel /boot/vmlinuz-2.6.18-1.2798.fc6 ro root=/dev/sdb1
initrd /boot/initrd-2.6.18-1.2798.fc6.img (不是 -raid 那個)
vi /etc/fstab
/dev/sdb1 / ext3 defaults 1 1
/dev/sdb2 swap swap defaults 0 0
/dev/sdb1 / ext3 defaults 1 1
/dev/sdb2 swap swap defaults 0 0
#刪除 Software RAID 設定檔
rm /etc/mdadm.conf
rm /etc/mdadm.conf
#所有的 partition 都改回 83 (linux)、82 (swap)
fdisk /dev/sda && fdisk /dev/sdb
fdisk /dev/sda && fdisk /dev/sdb
reboot
#清除 RAID superblock
mdadm --zero-superblock /dev/sda1
mdadm --zero-superblock /dev/sda2
mdadm --zero-superblock /dev/sda1
mdadm --zero-superblock /dev/sda2
mount /dev/sda1 /mnt
chroot /mnt
#改回以第一顆硬碟開機
vi /etc/fstab
vi /etc/fstab
/dev/sda1 / ext3 defaults 1 1
/dev/sda2 swap swap defaults 0 0
/dev/sda2 swap swap defaults 0 0
vi /boot/grub/menu.lst
default=0
timeout=5
title Fedora Core (2.6.18-1.2798.fc6)
root (hd0,0)
kernel /boot/vmlinuz-2.6.18-1.2798.fc6 ro root=/dev/sda1
initrd /boot/initrd-2.6.18-1.2798.fc6.img
exit
timeout=5
title Fedora Core (2.6.18-1.2798.fc6)
root (hd0,0)
kernel /boot/vmlinuz-2.6.18-1.2798.fc6 ro root=/dev/sda1
initrd /boot/initrd-2.6.18-1.2798.fc6.img
exit