Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.59 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # https://kamaok.org.ua/?p=182
  4. # http://blog.ispsystem.info/2016/11/centos-68-raid1-mdadm.html
  5. #
  6. ########################################
  7. #
  8.  
  9. EXIT(){
  10.     [[ -n  "${2}" ]] && echo "${2}"
  11.     exit "${1}"
  12. }
  13.  
  14. MSG(){
  15.     echo "___________________________________________________"
  16.     echo
  17.     echo "  $*"
  18.     echo
  19. }
  20.  
  21. EDITOR="mcedit"
  22.  
  23. SRC="/dev/sda"
  24. TRGT="/dev/sdb"
  25. B="1"  # /boot
  26. R="2"  # / (root FS)
  27.  
  28. RDEV=$(grep ^/dev/ /etc/mtab | while read -a i ; do
  29.     [[ "${i[1]}" == "/" ]] && echo "${i[0]}" | sed "s/\//\\\\\//g"
  30. done)
  31.  
  32. [[ -n "${RDEV}" ]] || EXIT 1 "Error: root device not found"
  33. [[ -b "${TRGT}" ]] || EXIT 1 "Error: ${TRGT} not found"
  34. [[ -n $(grep "^${SRC}" /etc/mtab) ]] || EXIT 1 "Error: root drive not ${SRC}"
  35.  
  36. ########################################
  37. #
  38.  
  39. sfdisk -d "${SRC}" | sfdisk --force "${TRGT}"
  40.  
  41. modprobe linear
  42. modprobe raid0
  43. modprobe raid1
  44.  
  45. ####
  46.  
  47. MSG "Linux raid autodetect fd"
  48.  
  49. fdisk "${TRGT}"
  50.  
  51. MSG "Clear superblocks"
  52.  
  53. mdadm --zero-superblock "${TRGT}${B}"
  54. mdadm --zero-superblock "${TRGT}${R}"
  55.  
  56. MSG "Create /dev/md${B} and /dev/md${R}"
  57.  
  58. mdadm --create /dev/md"${B}" --metadata=0.90  --level=1 --raid-disks=2 missing "${TRGT}${B}"
  59. mdadm --create /dev/md"${R}" --metadata=0.90  --level=1 --raid-disks=2 missing "${TRGT}${R}"
  60.  
  61. mdadm --detail --scan > /etc/mdadm.conf
  62.  
  63. MSG "Mkfs /dev/md${B} and /dev/md${R}"
  64.  
  65. mkfs.ext4 /dev/md"${B}"
  66. mkfs.ext4 /dev/md"${R}"
  67.  
  68. #### / (root FS)
  69.  
  70. MSG "Mount RootFS"
  71.  
  72. mkdir /raid
  73. mount /dev/md"${R}" /raid/ || EXIT 1 "Mount /dev/md${R} failed"
  74.  
  75. MSG "Copy  RootFS"
  76.  
  77. cd    /
  78. find  . -depth -xdev | grep -v '^\./tmp/' | cpio -pmd /raid/
  79.  
  80. blkid  /dev/md? >> /raid/etc/fstab
  81. "${EDITOR}" /raid/etc/fstab
  82.  
  83. touch  /raid/.autorelabel
  84.  
  85. #### /boot
  86.  
  87. MSG "Mount boot"
  88.  
  89. mount /dev/md"${B}" /raid/boot/
  90.  
  91. MSG "Copy boot"
  92.  
  93. cd    /boot/
  94. find  . -depth | cpio -pmd /raid/boot/
  95.  
  96. sed -i "s/"${RDEV}"/\/dev\/md"${R}"/g" /raid/boot/grub/grub.conf
  97.  
  98. # blkid  /dev/md? >> /raid/boot/grub/grub.conf
  99.  
  100. # remove rd_NO_DM rd_NO_MD
  101. # remove VolGroup-lv_root and other
  102. # add rd_MD_UUID=b9baa472:faeb3a7c:6e31c5b6:310ac827 ?
  103. "${EDITOR}" /raid/boot/grub/grub.conf
  104.  
  105. # (hd0) /dev/sda
  106. # (hd1) /dev/sdb
  107. "${EDITOR}" /raid/boot/grub/device.map
  108.  
  109. ####
  110.  
  111. mount -o bind /dev  /raid/dev
  112. mount -o bind /sys  /raid/sys
  113. mount -o bind /proc /raid/proc
  114.  
  115. ####
  116.  
  117. MSG "Create initramfs"
  118.  
  119. mv /raid/boot/initramfs-$(uname -r).img /raid/boot/initramfs-$(uname -r).img.old
  120. dracut --nolvmconf --mdadmconf --force /raid/boot/initramfs-$(uname -r).img $(uname -r)
  121.  
  122. MSG "Setup grub into chroot and reboot system
  123.  
  124. type:
  125. chroot /raid/"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement