Заміна дисків та розширення файлової системи в software RAID1 зі збереженням інформації
Стаття допоможе замінити диски з розширенням файлової системи (у випадку, якщо ФС для розширення знаходиться в кінці фізичного диска).
Вихідні дані:
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/md1 94G 60G 30G 67% /
/dev/md0 248M 33M 203M 14% /boot
# cat /proc/mdstat
Personalities : [raid1]
md1 : active raid1 sda3[0] sdb3[1]
100114816 blocks super 1.2 [2/2] [UU]
md0 : active raid1 sdb1[1] sda1[0]
262080 blocks [2/2] [UU]
# fdisk -l /dev/sda /dev/sdb
Disk /dev/sda: 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 / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x0003846e
Device Boot Start End Blocks Id System
/dev/sda1 1 33 262144 fd Linux raid autodetect
Partition 1 does not end on cylinder boundary.
/dev/sda2 33 2122 16777216 82 Linux swap / Solaris
/dev/sda3 2122 14594 100180440 fd Linux raid autodetect
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 / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000894bc
Device Boot Start End Blocks Id System
/dev/sdb1 1 33 262144 fd Linux raid autodetect
Partition 1 does not end on cylinder boundary.
/dev/sdb2 33 2122 16777216 83 Linux
/dev/sdb3 2122 14594 100180440 fd Linux raid autodetect
1) Встановлення boot-даних
Перш за все, перевіряємо завантажувальні сектори, щоб вони були на обох дисках.
1.1) GRUB1
Centos 5-6.
# grub
Probing devices to guess BIOS drives. This may take a long time.
GNU GRUB version 0.97 (640K lower / 3072K upper memory)
[ Minimal BASH-like line editing is supported. For the first word, TAB
lists possible command completions. Anywhere else TAB lists the possible
completions of a device/filename.]
grub> find /grub/stage1
find /grub/stage1
(hd0,0)
(hd1,0)
grub> root (hd0,0)
root (hd0,0)
Filesystem type is ext2fs, partition type 0xfd
grub> setup (hd0)
setup (hd0)
Checking if "/boot/grub/stage1" exists... no
Checking if "/grub/stage1" exists... yes
Checking if "/grub/stage2" exists... yes
Checking if "/grub/e2fs_stage1_5" exists... yes
Running "embed /grub/e2fs_stage1_5 (hd0)"... 27 sectors are embedded.
succeeded
Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub>
grub>
grub> device (hd0) /dev/sdb
device (hd0) /dev/sdb
grub> root (hd0,0)
root (hd0,0)
Filesystem type is ext2fs, partition type 0xfd
grub> setup (hd0)
setup (hd0)
Checking if "/boot/grub/stage1" exists... no
Checking if "/grub/stage1" exists... yes
Checking if "/grub/stage2" exists... yes
Checking if "/grub/e2fs_stage1_5" exists... yes
Running "embed /grub/e2fs_stage1_5 (hd0)"... 27 sectors are embedded.
succeeded
Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub> quit
quit
По другій частині поясню: диск, з якого система завантажилася в цей момент, завжди буде hd0, неважливо, який він за рахунком фізично. В першій частині ми на ньому обновили boot-сектор.
Щоб обновити на другому фізичному диску, грабу потрібно явно вказати, щоб він його вважав першим(grub> device (hd0) /dev/sdb). І потом ставимо як і на перший.
1.2) GRUB2
Ubuntu/Debain. (CentOS7 поки не тестував).
Тут ще легше, достатньо запустити
# dpkg-reconfigure grub-pc
і вибрати обидва фізичні диски: /dev/sda та /dev/sdb.
Далі він сам.
2) Заміна першого диска
Перевіряємо, що сервер завантажується з одного диска, а замінюємо інший.
Виходить після вимкнення та заміни
# fdisk -l /dev/sda /dev/sdb
Disk /dev/sda: 240.1 GB, 240057409536 bytes
255 heads, 63 sectors/track, 29185 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x00000000
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 / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000894bc
Device Boot Start End Blocks Id System
/dev/sdb1 1 33 262144 fd Linux raid autodetect
Partition 1 does not end on cylinder boundary.
/dev/sdb2 33 2122 16777216 83 Linux
/dev/sdb3 2122 14594 100180440 fd Linux raid autodetect
# cat /proc/mdstat
Personalities : [raid1]
md1 : active raid1 sdb3[1]
100114816 blocks super 1.2 [2/1] [_U]
md0 : active (auto-read-only) raid1 sdb1[1]
262080 blocks [2/1] [_U]
3) Розбивка диска.
Переносимо розмітку робочого диска на новий. УВАЖНО перевіряємо, звідки й куди копіюємо!
З пункту 2 видно, що треба переносити розділи з /dev/sdb на /dev/sda.
# sfdisk -d /dev/sdb | sfdisk --force /dev/sda
Checking that no-one is using this disk right now ...
OK
Disk /dev/sda: 29185 cylinders, 255 heads, 63 sectors/track
/dev/sda: 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/sda1 2048 526335 524288 fd Linux raid autodetect
/dev/sda2 526336 34080767 33554432 83 Linux
/dev/sda3 34080768 234441647 200360880 fd Linux raid autodetect
/dev/sda4 0 - 0 0 Empty
Warning: partition 1 does not end at a cylinder boundary
Successfully wrote the new partition table
Re-reading the partition table ...
# fdisk -l /dev/sda /dev/sdb
Disk /dev/sda: 240.1 GB, 240057409536 bytes
255 heads, 63 sectors/track, 29185 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sda1 1 33 262144 fd Linux raid autodetect
Partition 1 does not end on cylinder boundary.
/dev/sda2 33 2122 16777216 83 Linux
/dev/sda3 2122 14594 100180440 fd Linux raid autodetect
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 / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000894bc
Device Boot Start End Blocks Id System
/dev/sdb1 1 33 262144 fd Linux raid autodetect
Partition 1 does not end on cylinder boundary.
/dev/sdb2 33 2122 16777216 83 Linux
/dev/sdb3 2122 14594 100180440 fd Linux raid autodetect
4) Зміна розділів, відновлення рейду
Тепер повинні збільшити останній розділ перед підключенням знову до масиву:
# fdisk /dev/sda
Command (m for help): p
Disk /dev/sda: 240.1 GB, 240057409536 bytes
255 heads, 63 sectors/track, 29185 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sda1 1 33 262144 fd Linux raid autodetect
Partition 1 does not end on cylinder boundary.
/dev/sda2 33 2122 16777216 83 Linux
/dev/sda3 2122 14594 100180440 fd Linux raid autodetect
Command (m for help): d
Partition number (1-4): 3
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (2122-29185, default 2122):
Using default value 2122
Last cylinder, +cylinders or +size{K,M,G} (2122-29185, default 29185):
Using default value 29185
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): p
Disk /dev/sda: 240.1 GB, 240057409536 bytes
255 heads, 63 sectors/track, 29185 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sda1 1 33 262144 fd Linux raid autodetect
Partition 1 does not end on cylinder boundary.
/dev/sda2 33 2122 16777216 83 Linux
/dev/sda3 2122 29185 217388128+ fd Linux raid autodetect
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
Тобто, ми видаляємо розділ і повторно створюємо з тим самим типом і номером, тією самою початковою точкою, і новою, що знаходиться далі, і тим же самим ідентифікатором ФС.
PS. Так можна розширювати ФС і без рейду без втрати даних.
Додаємо нові розділи в рейд і чекаємо закінчення відновлення рейдів.
# mdadm --manage /dev/md0 --add /dev/sda1
mdadm: added /dev/sda1
# mdadm --manage /dev/md1 --add /dev/sda3
mdadm: added /dev/sda3
# watch -n 10 cat /proc/mdstat
Every 10.0s: cat /proc/mdstat Wed Dec 2 15:38:43 2015
Personalities : [raid1]
md1 : active raid1 sda3[2] sdb3[1]
100114816 blocks super 1.2 [2/1] [_U]
[=>...................] recovery = 6.0% (6098816/100114816) finish=12.2min speed=128354K/sec
md0 : active raid1 sda1[0] sdb1[1]
262080 blocks [2/2] [UU]
unused devices:
Після цього встановлюємо граб на новий диск (пункт 1), та перезавантажуємося з нього.
Якщо система успішно завантажилася, переходимо до наступного пункту.
5) Заміна другого диска.
Виконуємо пункти 2 та 3, а потім 1, аналогічно, як для попереднього диска.
4й уже не потрібен, тому що розділ перенесено уже в розширеному варіанті.
# cat /proc/mdstat
Personalities : [raid1]
md1 : active raid1 sda3[2]
100114816 blocks super 1.2 [2/1] [U_]
md0 : active (auto-read-only) raid1 sda1[0]
262080 blocks [2/1] [U_]
unused devices:
# fdisk -l /dev/sda /dev/sdb
Disk /dev/sda: 240.1 GB, 240057409536 bytes
255 heads, 63 sectors/track, 29185 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sda1 1 33 262144 fd Linux raid autodetect
Partition 1 does not end on cylinder boundary.
/dev/sda2 33 2122 16777216 83 Linux
/dev/sda3 2122 29185 217388128+ fd Linux raid autodetect
Disk /dev/sdb: 240.1 GB, 240057409536 bytes
255 heads, 63 sectors/track, 29185 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x00000000
Тобто, /dev/sda ==> /dev/sdb
# sfdisk -d /dev/sda | sfdisk --force /dev/sdb
...
# mdadm --manage /dev/md0 --add /dev/sdb1
mdadm: added /dev/sdb1
# mdadm --manage /dev/md1 --add /dev/sdb3
mdadm: added /dev/sdb3
# watch -n 10 cat /proc/mdstat
...
Чекаємо завершення відновлення. Потім встановлюємо grub(пункт 1).
6) Розширюємо рейд-масив та саму ФС.
Розширюємо рейд-масив, знову чекаємо ресинк
# mdadm --grow /dev/md1 --size=max
mdadm: component size of /dev/md1 has been set to 217322592K
unfreeze
# watch -n 10 cat /proc/mdstat
...
Та розширюємо саму файлову систему.
# resize2fs /dev/md1
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/md1 is mounted on /; on-line resizing required
old desc_blocks = 6, new_desc_blocks = 13
Performing an on-line resize of /dev/md1 to 54330648 (4k) blocks.
...
The filesystem on /dev/md1 is now 54330648 blocks long.
Все готово.
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/md1 204G 60G 134G 31% /
/dev/md0 248M 33M 203M 14% /boot