Advertisement
Guest User

setup-raid.sh

a guest
Oct 7th, 2019
357
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash -ex
  2.  
  3. # ViciBox v.9.0 RAID1 conversion script
  4.  
  5. # WARNING! This script is experimental and may result in data loss!
  6. # WARNING! Do not run on a live system without backups!
  7. # WARNING! Script assumes disk layout matches ViciBox v.9.0. MD installer and will not work with other systems.
  8.  
  9. # Usage: ./setup-raid.sh /dev/sda /dev/sdb
  10. DISK1="${1:-/dev/sda}"
  11. DISK2="${2:-/dev/sdb}"
  12.  
  13. # TODO: Detect these automatically?
  14. EFI_PART="2"
  15. BOOT_PART="3"
  16. ROOT_PART="4"
  17. SWAP_PART="5"
  18. BOOT_ARRAY="/dev/md3"
  19. ROOT_ARRAY="/dev/md4"
  20.  
  21.  
  22.  
  23. function main {
  24.   if [ efibootmgr 2>/dev/null = 0 ]; then
  25.     echo "System was booted in EFI mode, which is not supported (yet)"
  26.     exit 1
  27.   fi
  28.  
  29.   checkDiskParams
  30.   removeEfi
  31.   copyPartitionLayout
  32.   setupPartitions
  33.   waitForSync
  34.   updateGrub
  35.  
  36.   echo "RAID1 setup complete, you need to reboot. Good luck!"
  37. }
  38.  
  39.  
  40. function checkDiskParams {
  41.   if [ "$DISK1" = "" -o "$DISK2" = "" -o "$DISK1" = "$DISK2" ]; then
  42.     echo "Usage: $0 /dev/disk1 /dev/disk2"
  43.     exit 1
  44.   fi
  45.  
  46.   local response
  47.  
  48.   read -r -p "This script will DESTROY ALL DATA on $DISK2. Are you sure? [y/N] " response
  49.   if [[ ! "${response,,}" =~ ^(yes|y)$ ]]; then
  50.     exit 1
  51.   fi
  52. }
  53.  
  54.  
  55. function removeEfi {
  56.   if [ ! "$(mount | grep ' on \/boot\/efi ' --count)" = 0 ]; then
  57.     umount /boot/efi
  58.     commentLine /etc/fstab ' /boot/efi '
  59.   fi
  60. }
  61.  
  62. function copyPartitionLayout {
  63.   sgdisk "$DISK2" --zap-all
  64.   sgdisk "$DISK1" --replicate="$DISK2"
  65.   sgdisk "$DISK2" --randomize-guids
  66. }
  67.  
  68. function setupPartitions {
  69.   # SWAP ----------------------------------------
  70.   if [ ! "$(swapon --show=name --noheadings | wc -l)" = "0" ]; then
  71.     mkswap "${DISK2}${SWAP_PART}"
  72.     commentLine /etc/fstab ' swap swap '
  73.     echo "UUID=$(uuidFor "${DISK1}${SWAP_PART}") swap swap defaults 0 0" >> /etc/fstab
  74.     echo "UUID=$(uuidFor "${DISK2}${SWAP_PART}") swap swap defaults 0 0" >> /etc/fstab
  75.   fi
  76.  
  77.   # ROOT ----------------------------------------
  78.   mdadm --manage "$ROOT_ARRAY" --add "${DISK2}${ROOT_PART}"
  79.  
  80.   # BOOT ----------------------------------------
  81.   mdadm --zero-superblock "${DISK2}${BOOT_PART}"
  82.   mdadm --create "$BOOT_ARRAY" --level=1 --raid-devices=2 --metadata=1.2 "${DISK2}${BOOT_PART}" missing
  83.   mkfs.ext4 "$BOOT_ARRAY"
  84.   mkdir /tmp/boot
  85.   mount "$BOOT_ARRAY" /tmp/boot
  86.   rsync --archive /boot/ /tmp/boot
  87.   umount /tmp/boot
  88.  
  89.   # WARNING, from here onwards system wont boot if script does not run to completion
  90.   umount /boot
  91.   mdadm --manage "$BOOT_ARRAY" --add "${DISK1}${BOOT_PART}"
  92.   mdadm --examine --scan > /etc/mdadm.conf
  93.  
  94.   commentLine /etc/fstab ' /boot '
  95.   echo "UUID=$(uuidFor "$BOOT_ARRAY") /boot ext4 defaults 0 0" >> /etc/fstab
  96.   mount /boot
  97.  
  98. }
  99.  
  100. function waitForSync {
  101.   local arrays="$(cat /proc/mdstat | grep 'blocks' --count)"
  102.  
  103.   while [ ! "$(cat /proc/mdstat | grep '\[UU\]' --count)" = "$arrays" ]; do
  104.     echo "RAID arrays not synced yet, current status:"
  105.     cat /proc/mdstat
  106.     sleep 30
  107.   done
  108. }
  109.  
  110. function updateGrub {
  111.   echo 'GRUB_DISTRIBUTOR="ViciBox v.9.0"' >> /etc/default/grub
  112.  
  113.   # Needed to get grub to boot from a RAID1 drive
  114.   echo 'GRUB_PRELOAD_MODULES="diskfilter mdraid1x"' >> /etc/default/grub
  115.  
  116.   # RAID arrays must be synced before continuing, otherwise grub raises errors
  117.   grub2-mkconfig -o /boot/grub2/grub.cfg
  118.   grub2-install "$DISK1"
  119.   grub2-install "$DISK2"
  120. }
  121.  
  122. function commentLine {
  123.   local file="$1"
  124.   local search="$(echo "$2" | sed -e 's/[]\/$*.^[]/\\&/g')"   # escape sed search keyword https://stackoverflow.com/a/2705678  
  125.  
  126.   sed -i "s/^.*${search}/#&/g" "$file"
  127. }
  128.  
  129. function uuidFor {
  130.   blkid -s UUID -o value "$1"
  131. }
  132.  
  133.  
  134. main
Advertisement
RAW Paste Data Copied
Advertisement