Copier le MBR

Lorsque vous activez le RAID 1 sur une machine seules les partitions sont dupliqués. Mais en cas de crash et reboot, il vous faudra également le MBR pour pouvoir booter.

Copie physique du MBR

# Avec /dev/sda le disque bootable
# et /dev/sdc le disque (copié par raid) pas encore bootable
dd if=/dev/sda of=/dv/sdc bs=512 count=1

Création du MBR via GRUB

Expliqué sur le wiki Gentoo

*invoquer le shell grub : 
	grub>
	* Dans le shell de grub exécuter: 
 
	#Ecrire le MBR de /dev/sda
	root (hd0,0)
	setup (hd0)
	#Ecrire le MBR de /dev/sdb
	device (hd0) /dev/sdb
	root (hd0,0)
	setup (hd0)

Re synchroniser un disque

Un disque désynchronisés

Admettons, les disques suivants :

/dev/sda partionné avec :
       sda1
       sda2
       sda3 swap
/dev/sdb partionné avec :
       sdb1
       sdb2
       sdb3 swap
Le RAID 1 : 
md0 = sda1 + sdb1 = /
md1 = sda2 + sdb2 = /boot

Si le disque dur sda est tombé puis remplacé et partitionné, ou simplement désynchronisé  :

raidhotadd /dev/md0 /dev/sda1 # pour régénérer la partition /dev/sda1 depuis le miroir /dev/md0
raidhotadd /dev/md1 /dev/sda2 # pour régénérer la partition /dev/sda2 depuis le miroir /dev/md1

Si le disque dur sdb est tombé puis remplacé et partitionné, ou simplement désynchronisé  :

raidhotadd /dev/md0 /dev/sdb1 # pour régénérer la partition /dev/sdb1 depuis le miroir /dev/md0
raidhotadd /dev/md1 /dev/sdb2 # pour régénérer la partition /dev/sdb2 depuis le miroir /dev/md1

Un disque RAID défectueux et remplacé

Même chose que précédement, à l'exception qu'il faut d'abord recréer toutes les partitions avec fdisk sur le nouveau disque. Pour recréer sdb1 selon sa copie (sba1) définie par md0 :

mdadm /dev/md0 -a /dev/sdb1
#mdadm: hot added /dev/sdb1
mdadm /dev/md1 -a /dev/sdb2
#mdadm: hot added /dev/sdb2

Pour connaitre l'état de synchronisation du RAID vous pouvez executer :

cat /proc/mdstat
Personalities : [raid1]
md1 : active raid1 sdb2[1] sda2[0]
      15679360 blocks [2/2] [UU]
 
md0 : active raid1 sdd1[1] sdc1[0]
      71673856 blocks [2/2] [UU]
[UU] = Synchronisé 
[_U] ou [U_] veut dire que l'un des 2 disques n'est pas synchronisé

ou

lsraid -a /dev/md0
[dev   9,   0] /dev/md0         C36FB2A0.D4B5C960.8A4478D3.C625BC94 online
[dev   8,  33] /dev/sdc1        C36FB2A0.D4B5C960.8A4478D3.C625BC94 good
[dev   8,  49] /dev/sdd1        C36FB2A0.D4B5C960.8A4478D3.C625BC94 good

Créer une partition SWAP en RAID

Sur SUSE 10.0, je n'ai pas réussi à créer un RAID 1 sur la partition SWAP, en fait si, mais cela provoquait un kernel panic au reboot :

md: raid1 personality registered as nr 3
waiting for device /dev/md2 to appear : ok
no record for 'md2' in database
Attempting manual resume
Kernel panic - not syncing : I/O error reading memory image

En me documentant quelque peu, il est déconseillé de créer un SWAP en RAID, il vaut mieux créer 2 partitions "standards" (une sur chaque disque : /dev/hdax et /dev/hdcx) et les ajouter toutes les deux en partitions de swap pour obtenir :

/dev/hdax swap swap defaults 0 0
/dev/hdcx swap swap defaults 0 0

Les bon liens à propos du RAID

  From: Michael Martucci <m.martucci@gmx.net>
  To  : <linuxsa@linuxsa.org.au>
  Date: Tue, 29 Jul 2003 14:44:46 +0930
  URL : http://www.linuxsa.org.au/mailing-list/2003-07/1270.html

GRUB + RAID howto

 In the Software RAID howto it is mentioned that it is not known how
to set up GRUB to boot off RAID. Here is how I did it:
**Follow at your own risk. If you break something it's your fault.**
==================================================================
Configuration:
 - /dev/hda (Pri. Master) 60 GB Seagate HDD (blank)
 - /dev/hdc (Sec. Master) 60 GB Seagate HDD (blank)
 - /dev/hdd (Sec. Slave) CDROM Drive 

Setup Goals:
 - /boot as /dev/md0: RAID1 of /dev/hda1 & /dev/hdc1 for redundancy
 - /     as /dev/md1: RAID1 of /dev/hda2 & /dev/hdc2 for redundancy
 - swap*2 with equal priority: /dev/hda3 & /dev/hdc3 for more speed
 - GRUB installed in boot records of /dev/hda and /dev/hdc so either
    drive can fail but system still boot.

Tools:
 - mdadm (http://www.cse.unsw.edu.au/~neilb/source/mdadm/)
   (I used 1.2.0, but notice that as of 20030729 1.3.0 is available)

1. Boot up off rescue/installation CD/disk/HDD/whatever with mdadm
tools installed.

2. Partitioning of hard drives:
(I won't show you how to do this. See: # man fdisk ; man sfdisk )
But here's how stuff was arranged:
--
# sfdisk -l /dev/hda

Disk /dev/hda: 7297 cylinders, 255 heads, 63 sectors/track
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting
from 0

  Device Boot Start   End  #cyls   #blocks  Id System
/dev/hda1  *      0+   16     17-   136521  fd Linux raid autodetect
/dev/hda2        17  7219   7203  57858097+ fd Linux raid autodetect
/dev/hda3      7220  7296     77    618502+ 82 Linux swap
/dev/hda4         0     -      0         0   0 Empty
--
To make /dev/hdc the same:
--
# sfdisk -d /dev/hda | sfdisk /dev/hdc
--
/dev/hd[ac]1 for /dev/md0 for /boot
/dev/hd[ac]2 for /dev/md1 for /
/dev/hd[ac]3 for 2*swap
It is important to make md-to-be partitions with ID 0xFD, not 0x83.

3. Set up md devices: (both are RAID1 [mirrors])
--
# mdadm --create /dev/md0 --level=1 \
    --raid-devices=2 /dev/hda1 /dev/hdc1
# mdadm --create /dev/md1 --level=1 \
    --raid-devices=2 /dev/hda2 /dev/hdc2
--

4. Make filesystems:
--
# mke2fs /dev/md0
# mkreiserfs /dev/md1
# mkswap /dev/hda3
# mkswap /dev/hdc3
--

5. Install Your distribution:
 Simply treat /dev/md0 and /dev/md1 as the partitions to install on,
and install the way your normally do. Eg, for Gentoo:
--
# mkdir newinst
# mount -t reiserfs /dev/md1 ./newinst
# cd newinst
# mkdir boot
# mount -t ext2 /dev/md0 ./boot
# tar -xvjpf ../stage1-x86-1.4_rc2.tbz2
# mount -o bind /proc ./proc
# chroot ./
...
--
Here're the relevant entries /etc/fstab for the newly created
partitions:
--
/dev/md0      /boot        ext2       noauto,noatime          1 1
/dev/md1      /        reiserfs       noatime                 1 1
/dev/hda3     none         swap       sw,pri=1                0 0
/dev/hdc3     none         swap       sw,pri=1                0 0
--
The "pri=1" for each of the swap partitions makes them the same
priority so the kernel does striping and that speeds up vm. Of
course, this means that if a disk dies then the system may crash,
needing a reboot. Perhaps it would be wiser to make hd[ac]3 a RAID1
array too, and just use /dev/md2 as swap.

6. Setting up GRUB: (assuming you've already installed it)
--
# grub
grub> root (hd0,0)
 Filesystem type is ext2fs, partition type 0xfd

grub> setup (hd0) 
 Checking if "/boot/grub/stage1" exists... yes
 Checking if "/boot/grub/stage2" exists... yes
 Checking if "/boot/grub/e2fs_stage1_5" exists... yes
 Running "embed /boot/grub/e2fs_stage1_5 (hd0)"...  16 sectors are
embedded.
succeeded
 Running "install /boot/grub/stage1 (hd0) (hd0)1+16 p
(hd0,0)/boot/grub/stage2 /boot/grub/grub.conf"... succeeded
Done.

grub> root (hd1,0)
 Filesystem type is ext2fs, partition type 0xfd

grub> setup (hd1) 
 Checking if "/boot/grub/stage1" exists... yes
 Checking if "/boot/grub/stage2" exists... yes
 Checking if "/boot/grub/e2fs_stage1_5" exists... yes
 Running "embed /boot/grub/e2fs_stage1_5 (hd1)"...  16 sectors are
embedded.
succeeded
 Running "install /boot/grub/stage1 (hd1) (hd1)1+16 p
(hd1,0)/boot/grub/stage2 /boot/grub/grub.conf"... succeeded
Done.

grub> quit
--
Here is how /boot/grub/grub.conf is: (/dev/md0 mounted as /boot)
(Assuming kernel is installed as /boot/bzImage, and RAID1 support
compiled into the kernel).
--
# Boot automatically after 30 secs.
timeout 30

# By default, boot the first entry.
default 0

# Fallback to the second entry.
fallback 1

# For booting with disc 0 kernel
title  GNU/Linux (hd0,0)
kernel (hd0,0)/bzImage root=/dev/md1

# For booting with disc 1 kernel, if (hd0,0)/bzImage is unreadable
title  GNU/Linux (hd1,0)
kernel (hd1,0)/bzImage root=/dev/md1
--

Now you should be able to reboot your system and play!
==================================================================

Sauvegarder son MBR

Je cherche donc un moyen de sauver le MBR à partir de winXP ou d'un CD gnu/linux bootable, et un  utilitaire sous debian/XP qui permette de restaurer ce meme MBR.
Extrait de la doc de partimage :
$ dd if=/dev/hda of=backup-hda.mbr count=1 bs=512

It will produce a very small, but very important file: 512 bytes of
data. Now, we will save entries of the extended partitions:
$ sfdisk -d /dev/hda > backup-hda.sf

Pour la restauration :
$ dd if=backup-hda.mbr of=/dev/hda
$ sfdisk /dev/hda < backup-hda.sf