isaaclw
10/24/2014 - 1:24 AM

Block Device

Block Device

#!/bin/bash
dev1=$1
remote=$(echo $2 | cut -d ':' -f 1)
dev2=$(echo $2 | cut -d ':' -f 2)

ssh -i ~/.ssh/id_rsa $remote "
  perl -'MDigest::MD5 md5' -ne 'BEGIN{\$/=\1024};print md5(\$_)' $dev2 | lzop -c" |
    lzop -dc | perl -'MDigest::MD5 md5' -ne 'BEGIN{$/=\1024};$b=md5($_);
        read STDIN,$a,16;if ($a eq $b) {print "s"} else {print "c" . $_}' $dev1 | lzop -c |
ssh -i ~/.ssh/id_rsa $remote "lzop -dc |
  perl -ne 'BEGIN{\$/=\1} if (\$_ eq\"s\") {\$s++} else {if (\$s) {
          seek STDOUT,\$s*1024,1; \$s=0}; read ARGV,\$buf,1024; print \$buf}' 1<> $dev2"
# first list devices that are connected:
ls /dev/sd*
# then list devices listed in /sys/block:
ls /sys/block/sd*  # I expect they agree, but unsure atm

# find the 'hostid' of the existing devices
for i in $(ls /dev/); do if [[ "$i" =~ "sd" ]]; then (echo -n "$i => "; readlink /sys/block/$i | cut -d '/' -f 5; echo "") | grep "host"; fi; done
#!/bin/bash
if [ -z "$1" ] || [ -z "$2" ] || [ ! -b "$1" ]; then
    echo "Missing a block device, and a size (in that order)";
    echo "   example: $0 /dev/data/apps 3"
    echo "   Ensure your size is bigger than the filesystem"
    echo "   Or type 'optimal' To calculate the size"
    exit 1;
fi
size="$2"
if [[ "$size" = *"G" ]]; then
    size="${size::-1}"
elif [ "$size" == "optimal" ]; then
    # pass
    :
elif ! [[ $size =~ ^[0-9]+$ ]]; then
    echo "Size: $size is not an integer" >&2
    exit 1
fi

blockdev="$1";

sudo mount "$blockdev" 2> /dev/null
current="$(echo "$(df "$blockdev" --output=size | sed 1d)/(1024*1024)" | bc)"
if [ "$2" == "optimal" ]; then
    size="$(echo "($(df "$blockdev" --output=used | sed 1d) * 1.33333)/(1024*1024)" | bc)"
fi

df -h "$blockdev"
echo -n "Resizing to ${size}G [N/y] "
read -N 1 REPLY
echo
if test "$REPLY" = "y" -o "$REPLY" = "Y"; then
    if [ "$current" -eq 0 ] || [ "$current" -gt "$size" ]; then
        # shrinking
        sudo umount "$blockdev" || exit 1
        while mount | grep "$blockdev" > /dev/null; do
            sudo umount "$blockdev"
            sleep 1; echo "Trying again"
        done
        sudo e2fsck -f "$blockdev" && \
            sudo resize2fs -M "$blockdev" && \
            sudo lvresize -f -L "${size}G" "$blockdev" && \
            sudo resize2fs "$blockdev" && \
            sudo mount "$blockdev"
    else
        # expanding
        sudo lvresize -L "${size}G" "$blockdev" && sudo resize2fs "$blockdev"
    fi
else
    echo "Cancelled by user"
fi
sudo lvs --segments -o +pe_ranges
#!/bin/bash
# This was to create a quick snapshot of the database, make the changes I
# wanted, and then revert the database to exactly the way it was before I
# snapshotted it.

LV=postgresql
VG=pogo-vg
MTPT=/var/lib/postgresql
PROC=postgres
VERSION=11

LVM_DEV="/dev/$VG/$LV"
LVM_SNAP="$LVM_DEV-snap"

if [ "$(whoami)" != "root" ]; then
    echo "Run as root!"
    exit 1
fi
un_mount () {
    mount | grep "$MTPT" | cut -d' ' -f 3 | sort -r | while read dir; do
        while findmnt "$dir" > /dev/null; do
            echo "found $dir to unmount"
            umount "$dir" || { echo "relevant processes: "; lsof +D "$dir" 2>/dev/null; }
            sleep 2
        done
    done
}

start_snapshot() {
    un_mount
    lvcreate --snapshot --size 50G --name $LV-snap "$LVM_DEV"
    mount "$LVM_SNAP" "$MTPT" 2>/dev/null
}
stop_snapshot() {
    un_mount
    /sbin/lvremove --force "$LVM_SNAP"
    mount "$LVM_DEV" "$MTPT" 2>/dev/null
}

startproc() {
    pg_ctlcluster $VERSION main start # start only known active
    /etc/init.d/pgpool2 restart
}
stopproc() {
    /etc/init.d/pgpool2 stop
    /etc/init.d/postgresql stop  # stop all instances of postgres
    sleep 2
}

case $1 in
    start)
        if [ -b "$LVM_SNAP" ]; then
            echo "Already set up"
            exit 1
        fi
        stopproc
        start_snapshot
        systemctl start postgresql
        startproc
        ;;
    stop)
        if ! [ -b "$LVM_SNAP" ]; then
            echo "None Exists"
            exit 1
        fi
        stopproc
        stop_snapshot
        startproc
        ;;
    restart)
        stopproc
        stop_snapshot
        start_snapshot
        startproc
        ;;
    *)
        echo "start|stop|restart";
        ;;
esac;
#!/bin/bash
# copied from David
# oneliner scan: for i in $(ls /dev/); do if [[ "$i" =~ "sd" ]]; then (echo -n "$i => "; readlink /sys/block/$i | cut -d '/' -f 5; echo "") | grep "host"; fi; done
# oneliner reset: echo '- - -' > /sys/class/scsi_host/host3/scan

[ -b "$1" -o -b /dev/"$1" ] || { echo "Error: Invalid device '$1'"; exit 1; }

device=$(basename $(echo "$1" |sed -e 's/[0-9]$//g'))
host=$(ls -l /sys/block/$device/subsystem/ |grep "${device}$" |grep -oE 'host[0-9]+')

read -p "Rescan device or bus?: (D/b) " yn
case $yn in
  d|D)
    # Rescan the specific device
    echo 1 > /sys/block/$device/device/rescan
    ;;

  b|B)
    # Rescan the bus
    echo '- - -' > /sys/class/scsi_host/$host/scan
    ;;

  *) echo "Invalid option. Quitting"; exit 1;;
esac