Skip to content

Instantly share code, notes, and snippets.

@dklassen
Forked from dalehamel/gist:5528d054d3ad4035f06a
Last active August 29, 2015 14:11
Show Gist options
  • Save dklassen/370292fb35a6723e42e4 to your computer and use it in GitHub Desktop.
Save dklassen/370292fb35a6723e42e4 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -x # verbosity
# This script is meant to be run from inside alchemy linux, our PXE rescue system.
# To boot into alchemy, connect to a serial console with ipmitool sol activate, and set the bootdev with 'chassis bootdev pxe'
# The default user:pass is alchemy:transmute
# This script covers swapping out the OS drive of a hadoop node, and is very coupled to this task. The individual sections however may be reused.
# This script should never be run without being watched, and should never be run all at once. Run each section on its own. Ye be warned.
# Here we'll detect the newly inserted drive, and create it as a single drive raid0. Yes, megaraid is stupid.
slot=`sudo /opt/megacli/megacli -PDList -aAll | grep Unconfigured -B 17 | head -n1 | cut -d ' ' -f 3`
sudo /opt/megacli/megacli -CfgLdAdd -r0 [252:$slot] -a0
# Now we'll create the partition table, and create a full disk partition. Blank lines are intentional, as they pass the default response
NEWDEVICE=`lsblk | grep 893 | awk '{print $1}'` #893 is a magic number, the size of a 1TB crucial M500 hard drive.
echo $NEWDEVICE
fdisk /dev/$NEWDEVICE <<EOF
o
n
w
EOF
# Now we'll create the LVM physical, volume, and logical groups
pvcreate /dev/${NEWDEVICE}1
vgcreate lvssd /dev/${NEWDEVICE}1
lvcreate -L 40G -nroot lvssd
lvcreate -L 100G -nswap lvssd
lvcreate -L 50G -nu lvssd
lvcreate -L 700G -ntmp lvssd
# And format them
mkfs.ext4 /dev/mapper/lvssd-root
mkfs.ext4 /dev/mapper/lvssd-u
mkfs.ext4 -i 4096 /dev/mapper/lvssd-tmp # the -i 4096 here says to use inodes that are 1/4 the default size, so we get more of them for tmp
mkswap /dev/mapper/lvssd-swap
# Now we'll mount the drives we made
mkdir /mnt/new /mnt/u /mnt/tmp /mnt/old /mnt/boot
mount /dev/mapper/lvssd-tmp /mnt/tmp
mount /dev/mapper/lvssd-u /mnt/u
mount /dev/mapper/lvssd-root /mnt/new
mount /dev/mapper/lvroot-root /mnt/old
mount /dev/sda1 /mnt/boot
chmod -R 1777 /mnt/tmp # set everyone and sticky bit
# And copy some data we care about, and remove what we don't
mv /mnt/old/u/* /mnt/u
rm -rf /mnt/old/tmp # cleared on startup anyways, so let's not copy it
cp -r /mnt/boot/* /mnt/old/boot # keep the kernel or else we're fucked
#tarpipes ftw
cd /mnt/old
# NOTE: make sure that the old root isn't bigger than the new root First!!!
# Here's the magic source - a tarpipe to copy everything, preserving the permissions of all files. There are other ways, but I like this best.
tar -cpf - . | sudo tar -C /mnt/new -xpf -
# Manually fix /etc/fstab to remove crappy entries, then append this:
#/dev/mapper/vgssd-root / ext4 errors=remount-ro,discard 0 1
#/dev/mapper/vgssd-swap none swap sw 0 0
#/dev/mapper/vgssd-tmp /tmp ext4 nobarrier,discard,noatime,nodiratime 1 2
#/dev/mapper/vgssd-u /u ext4 discard 1 2
# Now we chroot into the new root system
mount --rbind /dev /mnt/new/dev
mount --rbind /sys /mnt/new/sys
mount -t proc none /mnt/new/proc
# And make sure we haven't confused the fuck out of grub
chroot /mnt/new grub-install /dev/${NEWDEVICE}
chroot /mnt/new update-grub2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment