Перевод рабочей Linux Debian Squeeze на RAID1

Случилось так что винт начал сыпаться потихоньку, поэтому нужно было срочно заменить его на другой.
Заодно сделать RAID1 из двух винтов чтобы больше такое не повторилось. На старом винте не было никакого рейда.

1.Для начала устанавливаем необходимые утилиты

aptitude install initramfs-tools mdadm

2.Проверяем как разбит диск

# fdisk -l
 
Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00080c99
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1       58338   468596736   83  Linux
/dev/sda2           58338       60802    19786753    5  Extended
/dev/sda5           58338       60802    19786752   82  Linux swap / Solaris

В итоге нам нужно заменить этот винт на другой, добавить еще один и сделать из них RAID1 (зеркалирование)

3.Подцепляем новый винт и размечаем его

# cfdisk /dev/sdb
 
# fdisk -l /dev/sdb
 
Disk /dev/sdb: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1       13130   105466693+  fd  Linux raid autodetect
/dev/sdb2           13131       14593    11751547+  fd  Linux raid autodetect

/dev/sdb1 - /(корень)
/dev/sdb2 - swap

4. Очищаем суперблоки от старых рейдов

mdadm --zero-superblock /dev/sdb1
mdadm --zero-superblock /dev/sdb2

5. Проверим доступны ли массивы

# cat /proc/mdstat
Personalities : 
unused devices: <none>

Похоже что нет.

6. Подгружаем модуль

# modprobe raid1

7. Проверяем еще раз.

# cat /proc/mdstat                 
Personalities : [raid1] 
unused devices: <none>

8. Создаем массивы

# mdadm --create /dev/md0 --level=1 --raid-disks=2 missing /dev/sdb1
mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.  If you plan to
    store '/boot' on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    --metadata=0.90
Continue creating array? (y/n) y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
 
# mdadm --create /dev/md1 --level=1 --raid-disks=2 missing /dev/sdb2
mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.  If you plan to
    store '/boot' on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    --metadata=0.90
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
 
# cat /proc/mdstat 
Personalities : [raid1] 
md1 : active raid1 sdb2[1]
      11750451 blocks super 1.2 [2/1] [_U]
 
md0 : active raid1 sdb1[1]
      105465597 blocks super 1.2 [2/1] [_U]

Всё получилось, создали 2 массива в каждом по 2 диска, один из которых missing.
Затем мы подсунем туда диски sda.
md0 - /(корень)
md1 - swap

9. Теперь нужно отформатировать наши рейды

# mkfs.ext4 /dev/md0
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6594560 inodes, 26366399 blocks
1318319 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
805 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872
 
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
 
# mkswap /dev/md1
mkswap: /dev/md1: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 11750444 KiB
no label, UUID=6693ef14-d27b-493a-945c-e78333ec97b7

10. Добавляем массивы в конфиг mdadm.conf

cp /etc/mdadm/mdadm.conf /etc/mdadm/mdadm.conf_orig
mdadm --examine --scan >> /etc/mdadm/mdadm.conf

11. Проверяем, что получилось

# cat /etc/mdadm/mdadm.conf
# mdadm.conf
#
# Please refer to mdadm.conf(5) for information about this file.
#
# by default, scan all partitions (/proc/partitions) for MD superblocks.
# alternatively, specify devices to scan, using wildcards if desired.
DEVICE partitions
# auto-create devices with Debian standard permissions
CREATE owner=root group=disk mode=0660 auto=yes
# automatically tag new arrays as belonging to the local system
HOMEHOST <system>
# instruct the monitoring daemon where to send mail alerts
MAILADDR root
# definitions of existing MD arrays
# This file was auto-generated on Tue, 01 Jan 2002 06:47:08 +0300
# by mkconf 3.1.4-1+8efb9d1+squeeze1
ARRAY /dev/md/0 metadata=1.2 UUID=4dc57958:4d53372f:f7b6e186:393acc1a name=main:0
ARRAY /dev/md/1 metadata=1.2 UUID=c3f5f216:9b385662:ecfe3a5d:b13e926c name=main:1

12. Создаем каталог в /mnt и монтируем туда наш будущий корень

# mkdir /mnt/root
# mount /dev/md0 /mnt/root/

13. Копируем туда наш текущий корень

# cp -dpRx / /mnt/root/

14. Редактируем /etc/fstab
было так:

# cat /etc/fstab 
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
# / was on /dev/sda1 during installation
UUID=4c907b69-9134-472c-9a18-6bdb0ec0bf3a /               ext3    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=c69d54f0-37ac-4d40-ad02-8ae4fee12081 none            swap    sw              0       0

стало так:
# cat /etc/fstab 
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
# / was on /dev/sda1 during installation
/dev/md0       /       ext4    errors=remount-ro       0       1
# swap was on /dev/sda5 during installation
/dev/md1       none    swap    sw                      0       0

15. Монтируем оставшиеся каталоги и устанавливаем grub

# mount --bind /dev /mnt/root/dev/
# mount --bind /sys/ /mnt/root/sys/
# mount --bind /proc /mnt/root/proc/
# chroot /mnt/root/
# grub-install /dev/sdb
/usr/sbin/grub-probe: error: no such disk.
Auto-detection of a filesystem of /dev/md0 failed.
Please report this together with the output of "/usr/sbin/grub-probe --device-map=/boot/grub/device.map --target=fs -v /boot/grub" to <bug-grub@gnu.org>

Ловим глюк с установкой grub.

16. Пытаемся полечить

# /usr/sbin/grub-probe --device-map=/boot/grub/device.map --target=fs -v /boot/grub
/usr/sbin/grub-probe: info: the size of hd0 is 976773168.
/usr/sbin/grub-probe: info: the size of hd0 is 976773168.
/usr/sbin/grub-probe: info: the size of hd0 is 976773168.
/usr/sbin/grub-probe: info: the size of hd0 is 976773168.
/usr/sbin/grub-probe: info: the size of hd0 is 976773168.
/usr/sbin/grub-probe: info: the size of hd0 is 976773168.
/usr/sbin/grub-probe: info: the size of hd0 is 976773168.
/usr/sbin/grub-probe: info: the size of hd0 is 976773168.
/usr/sbin/grub-probe: info: changing current directory to /dev.
/usr/sbin/grub-probe: info: opening md/0.
/usr/sbin/grub-probe: error: no such disk.
 
# update-grub2
Generating grub.cfg ...
/usr/sbin/grub-probe: error: no such disk.
 
# cat /boot/grub/device.map 
(hd0)   /dev/disk/by-id/ata-WDC_WD5003ABYX-01WERA0_WD-WMAYP1971306
 
# mv /boot/grub/device.map /boot/grub/device.map.old
 
# grub-mkdevicemap
 
# cat /boot/grub/device.map
(hd0)   /dev/disk/by-id/ata-WDC_WD5003ABYX-01WERA0_WD-WMAYP1971306
(hd1)   /dev/disk/by-id/ata-OCZ-TRION150_26MB31A6K1AU
 
# update-grub2
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-2.6.32-5-amd64
Found initrd image: /boot/initrd.img-2.6.32-5-amd64
done
 
# grub-install /dev/sdb
Installation finished. No error reported.
 
# update-grub2         
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-2.6.32-5-amd64
Found initrd image: /boot/initrd.img-2.6.32-5-amd64

Вобщем алилуджя, вылечили!

17. В файле /etc/default/grub обязательно нужно раскомментировать строку

GRUB_TERMINAL=console

18. И снова

# update-grub2         
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-2.6.32-5-amd64
Found initrd image: /boot/initrd.img-2.6.32-5-amd64

19. Проверяем

# cat /boot/grub/grub.cfg
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#
 
### BEGIN /etc/grub.d/00_header ###
if [ -s $prefix/grubenv ]; then
  load_env
fi
set default="0"
if [ "${prev_saved_entry}" ]; then
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
fi
 
function savedefault {
  if [ -z "${boot_once}" ]; then
    saved_entry="${chosen}"
    save_env saved_entry
  fi
}
 
function load_video {
  insmod vbe
  insmod vga
  insmod video_bochs
  insmod video_cirrus
}
 
terminal_input console
terminal_output console
set timeout=5
### END /etc/grub.d/00_header ###
 
### BEGIN /etc/grub.d/05_debian_theme ###
set menu_color_normal=cyan/blue
set menu_color_highlight=white/blue
### END /etc/grub.d/05_debian_theme ###
 
### BEGIN /etc/grub.d/10_linux ###
menuentry 'Debian GNU/Linux, with Linux 2.6.32-5-amd64' --class debian --class gnu-linux --class gnu --class os {
        insmod raid
        insmod mdraid
        insmod part_msdos
        insmod ext2
        set root='(md/0)'
        search --no-floppy --fs-uuid --set aaf9fd10-1ca0-4480-ac0b-e19f1a9ca0d6
        echo    'Loading Linux 2.6.32-5-amd64 ...'
        linux   /boot/vmlinuz-2.6.32-5-amd64 root=/dev/md0 ro  quiet
        echo    'Loading initial ramdisk ...'
        initrd  /boot/initrd.img-2.6.32-5-amd64
}
menuentry 'Debian GNU/Linux, with Linux 2.6.32-5-amd64 (recovery mode)' --class debian --class gnu-linux --class gnu --class os {
        insmod raid
        insmod mdraid
        insmod part_msdos
        insmod ext2
        set root='(md/0)'
        search --no-floppy --fs-uuid --set aaf9fd10-1ca0-4480-ac0b-e19f1a9ca0d6
        echo    'Loading Linux 2.6.32-5-amd64 ...'
        linux   /boot/vmlinuz-2.6.32-5-amd64 root=/dev/md0 ro single 
        echo    'Loading initial ramdisk ...'
        initrd  /boot/initrd.img-2.6.32-5-amd64
}
### END /etc/grub.d/10_linux ###
 
### BEGIN /etc/grub.d/20_linux_xen ###

затем можно выйти из chroot и перезагрузиться, отцепив старый винт и подцепив второй новый.
не забыв загрузиться с того на который мы скопировали систему.

20. Если мы успешно загрузились то продолжаем

# cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] 
md0 : active raid1 sda1[1]
      105465597 blocks super 1.2 [2/1] [_U]
 
md1 : active (auto-read-only) raid1 sda2[1]
      11750451 blocks super 1.2 [2/1] [_U]
 
unused devices: <none>

21. Смотрим UUID

# blkid 
/dev/md0: UUID="aaf9fd10-1ca0-4480-ac0b-e19f1a9ca0d6" TYPE="ext4" 
/dev/md1: UUID="6693ef14-d27b-493a-945c-e78333ec97b7" TYPE="swap" 
/dev/sda1: UUID="4dc57958-4d53-372f-f7b6-e186393acc1a" LABEL="main:0" TYPE="linux_raid_member" 
/dev/sda2: UUID="c3f5f216-9b38-5662-ecfe-3a5db13e926c" LABEL="main:1" TYPE="linux_raid_member" 
 
# ls -l /dev/disk/by-uuid/
итого 0
lrwxrwxrwx 1 root root 9 Июн  3 19:46 6693ef14-d27b-493a-945c-e78333ec97b7 -> ../../md1
lrwxrwxrwx 1 root root 9 Июн  3 19:46 aaf9fd10-1ca0-4480-ac0b-e19f1a9ca0d6 -> ../../md0

22.

# update-grub2 
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-2.6.32-5-amd64
Found initrd image: /boot/initrd.img-2.6.32-5-amd64
done

23. Клонируем разметку разделов с sda на sdb

# sfdisk -d /dev/sda | sfdisk --force /dev/sdb 
Checking that no-one is using this disk right now ...
OK
 
Disk /dev/sdb: 14593 cylinders, 255 heads, 63 sectors/track
 
sfdisk: ERROR: sector 0 does not have an msdos signature
 /dev/sdb: unrecognized partition table type
Old situation:
No partitions found
New situation:
Units = sectors of 512 bytes, counting from 0
 
   Device Boot    Start       End   #sectors  Id  System
/dev/sdb1   *        63 210933449  210933387  fd  Linux raid autodetect
/dev/sdb2     210933450 234436544   23503095  fd  Linux raid autodetect
/dev/sdb3             0         -          0   0  Empty
/dev/sdb4             0         -          0   0  Empty
Successfully wrote the new partition table
 
Re-reading the partition table ...
 
If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
to zero the first 512 bytes:  dd if=/dev/zero of=/dev/foo7 bs=512 count=1
(See fdisk(8).)

24. Смотрим, что получилось

# fdisk -l /dev/sdb
 
Disk /dev/sdb: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1       13130   105466693+  fd  Linux raid autodetect
/dev/sdb2           13131       14593    11751547+  fd  Linux raid autodetect

25. заглянем в /proc/mdstat

# cat /proc/mdstat 
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] 
md0 : active raid1 sda1[1]
      105465597 blocks super 1.2 [2/1] [_U]
 
md1 : active (auto-read-only) raid1 sda2[1]
      11750451 blocks super 1.2 [2/1] [_U]
 
unused devices: <none>

26. Добавляем в массивы остальные партиции

# mdadm --add /dev/md0 /dev/sdb1
mdadm: added /dev/sdb1
# mdadm --add /dev/md1 /dev/sdb2
mdadm: added /dev/sdb2

27. Ждем когда добавятся

# cat /proc/mdstat 
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] 
md0 : active raid1 sdb1[2] sda1[1]
      105465597 blocks super 1.2 [2/1] [_U]
      [============>........]  recovery = 63.3% (66848448/105465597) finish=12.5min speed=51132K/sec
 
md1 : active raid1 sdb2[2] sda2[1]
      11750451 blocks super 1.2 [2/1] [_U]
        resync=DELAYED
 
# cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] 
md0 : active raid1 sdb1[2] sda1[1]
      105465597 blocks super 1.2 [2/2] [UU]
 
md1 : active raid1 sdb2[2] sda2[1]
      11750451 blocks super 1.2 [2/2] [UU]
 
unused devices: <none>

28. Устанавливаем GRUB на второй винт и для профилактики на первый )

# grub-install /dev/sdb
Installation finished. No error reported.
# grub-install /dev/sda
Installation finished. No error reported.

Вроде всё. А ну еще надо подправить fstab прописать туда UUID.