Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash -ex
- # ViciBox v.9.0 RAID1 conversion script
- # WARNING! This script is experimental and may result in data loss!
- # WARNING! Do not run on a live system without backups!
- # WARNING! Script assumes disk layout matches ViciBox v.9.0. MD installer and will not work with other systems.
- # Usage: ./setup-raid.sh /dev/sda /dev/sdb
- DISK1="${1:-/dev/sda}"
- DISK2="${2:-/dev/sdb}"
- # TODO: Detect these automatically?
- EFI_PART="2"
- BOOT_PART="3"
- ROOT_PART="4"
- SWAP_PART="5"
- BOOT_ARRAY="/dev/md3"
- ROOT_ARRAY="/dev/md4"
- function main {
- if [ efibootmgr 2>/dev/null = 0 ]; then
- echo "System was booted in EFI mode, which is not supported (yet)"
- exit 1
- fi
- checkDiskParams
- removeEfi
- copyPartitionLayout
- setupPartitions
- waitForSync
- updateGrub
- echo "RAID1 setup complete, you need to reboot. Good luck!"
- }
- function checkDiskParams {
- if [ "$DISK1" = "" -o "$DISK2" = "" -o "$DISK1" = "$DISK2" ]; then
- echo "Usage: $0 /dev/disk1 /dev/disk2"
- exit 1
- fi
- local response
- read -r -p "This script will DESTROY ALL DATA on $DISK2. Are you sure? [y/N] " response
- if [[ ! "${response,,}" =~ ^(yes|y)$ ]]; then
- exit 1
- fi
- }
- function removeEfi {
- if [ ! "$(mount | grep ' on \/boot\/efi ' --count)" = 0 ]; then
- umount /boot/efi
- commentLine /etc/fstab ' /boot/efi '
- fi
- }
- function copyPartitionLayout {
- sgdisk "$DISK2" --zap-all
- sgdisk "$DISK1" --replicate="$DISK2"
- sgdisk "$DISK2" --randomize-guids
- }
- function setupPartitions {
- # SWAP ----------------------------------------
- if [ ! "$(swapon --show=name --noheadings | wc -l)" = "0" ]; then
- mkswap "${DISK2}${SWAP_PART}"
- commentLine /etc/fstab ' swap swap '
- echo "UUID=$(uuidFor "${DISK1}${SWAP_PART}") swap swap defaults 0 0" >> /etc/fstab
- echo "UUID=$(uuidFor "${DISK2}${SWAP_PART}") swap swap defaults 0 0" >> /etc/fstab
- fi
- # ROOT ----------------------------------------
- mdadm --manage "$ROOT_ARRAY" --add "${DISK2}${ROOT_PART}"
- # BOOT ----------------------------------------
- mdadm --zero-superblock "${DISK2}${BOOT_PART}"
- mdadm --create "$BOOT_ARRAY" --level=1 --raid-devices=2 --metadata=1.2 "${DISK2}${BOOT_PART}" missing
- mkfs.ext4 "$BOOT_ARRAY"
- mkdir /tmp/boot
- mount "$BOOT_ARRAY" /tmp/boot
- rsync --archive /boot/ /tmp/boot
- umount /tmp/boot
- # WARNING, from here onwards system wont boot if script does not run to completion
- umount /boot
- mdadm --manage "$BOOT_ARRAY" --add "${DISK1}${BOOT_PART}"
- mdadm --examine --scan > /etc/mdadm.conf
- commentLine /etc/fstab ' /boot '
- echo "UUID=$(uuidFor "$BOOT_ARRAY") /boot ext4 defaults 0 0" >> /etc/fstab
- mount /boot
- }
- function waitForSync {
- local arrays="$(cat /proc/mdstat | grep 'blocks' --count)"
- while [ ! "$(cat /proc/mdstat | grep '\[UU\]' --count)" = "$arrays" ]; do
- echo "RAID arrays not synced yet, current status:"
- cat /proc/mdstat
- sleep 30
- done
- }
- function updateGrub {
- echo 'GRUB_DISTRIBUTOR="ViciBox v.9.0"' >> /etc/default/grub
- # Needed to get grub to boot from a RAID1 drive
- echo 'GRUB_PRELOAD_MODULES="diskfilter mdraid1x"' >> /etc/default/grub
- # RAID arrays must be synced before continuing, otherwise grub raises errors
- grub2-mkconfig -o /boot/grub2/grub.cfg
- grub2-install "$DISK1"
- grub2-install "$DISK2"
- }
- function commentLine {
- local file="$1"
- local search="$(echo "$2" | sed -e 's/[]\/$*.^[]/\\&/g')" # escape sed search keyword https://stackoverflow.com/a/2705678
- sed -i "s/^.*${search}/#&/g" "$file"
- }
- function uuidFor {
- blkid -s UUID -o value "$1"
- }
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement