From 4a818bb2d544855f2aa125249819469983a648ca Mon Sep 17 00:00:00 2001 From: BrainwreckedTech <3752022+BrainwreckedTech@users.noreply.github.com> Date: Wed, 21 Aug 2019 17:28:17 -0400 Subject: [PATCH] First sync. --- README.md | 27 +++++++++- ckalign | 32 ++++++++++++ cto | 11 +++++ ddi | 10 ++++ drvspeed | 45 +++++++++++++++++ filldev | 22 +++++++++ lctopen | 23 +++++++++ makeraid | 29 +++++++++++ mkcrypt | 76 ++++++++++++++++++++++++++++ nhdtest | 49 +++++++++++++++++++ partcopy | 38 ++++++++++++++ partvis | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 505 insertions(+), 1 deletion(-) create mode 100755 ckalign create mode 100755 cto create mode 100755 ddi create mode 100755 drvspeed create mode 100755 filldev create mode 100755 lctopen create mode 100755 makeraid create mode 100755 mkcrypt create mode 100755 nhdtest create mode 100755 partcopy create mode 100755 partvis diff --git a/README.md b/README.md index 64208a4..a5f4566 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,28 @@ # disks +Scripts for disks -Scripts for disks \ No newline at end of file +**PLEASE NOTE:** These scripts should be considered experimental. +Test on non-production or backed-up data first. + +- ckalign + > Check if a given sector number is aligned on a 4k boundary +- ctoa + > CrypTab Open All: Opens all devices listed in /etc/cryptab +- drvspeed + > Reports speeds of all devices using hdparm +- ddi + > Dump Disk Info: Dumps UUID and Label to .uuid and .label files in root of device +- filldev + > Fills a hard drive with 0's or with random data +- lctopen + > LUKS CrypTab Open: Opens a device in /etc/cryptab given a unique identifier) +- makeraid + > Make a RAID device +- mkcrypt + > Make a crypt device +- nhdtest + > Tests new hard drives +- partcopy + > Copies the contents of one partition to another (takes care of mounting and unmounting) +- partvis + > Displays device partitions on the command line diff --git a/ckalign b/ckalign new file mode 100755 index 0000000..5972c28 --- /dev/null +++ b/ckalign @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +if [ ${1} == "-e" -o ${1} == "--end" ]; then + BOUNDADJ=1 +elif [ ${1} == "-s" -o ${1} == "--start" ]; then + BOUNDADJ=0 +else + echo "Neither '--start' nor '--end' were specified." + exit 1 +fi + +BOUNDARY=$(( ${2} + BOUNDADJ )) + +ALIGN_4K=$(( BOUNDARY / 8 )) +SECTS_4K=$(( ALIGN_4K * 8 )) +((SECTS_4K-=${BOUNDADJ})) + +ALIGN_1M=$(( BOUNDARY / 2048 )) +SECTS_1M=$(( ALIGN_1M * 2048 )) +((SECTS_1M-=${BOUNDADJ})) + +if [ ${2} -eq ${SECTS_4K} ]; then + echo "Boundary is 4K aligned." +else + echo "Boundary is not 4K aligned. Proper alignment would be ${SECTS_4K} or $(( SECTS_4K + 8 ))." +fi + +if [ ${2} -eq ${SECTS_1M} ]; then + echo "Boundary ${2} is 1M aligned." +else + echo "Boundary is not 1M aligned. Proper alignment would be ${SECTS_1M} or $(( SECTS_1M + 2048 ))." +fi diff --git a/cto b/cto new file mode 100755 index 0000000..f8ce1e2 --- /dev/null +++ b/cto @@ -0,0 +1,11 @@ +#!/usr/bin/env sh + +# CryptTab Open + +while read -r NAME DEV KEY OPTS; do + if [ -f "${KEY}" ]; then + cryptsetup open "${DEV}" "${NAME}" --key-file "${KEY}" + else + cryptsetup open "${DEV}" "${NAME}" + fi +done <<< $(grep -v '^#' /etc/crypttab | sed '/^[[:space:]]*$/d' | sed 's/[[:space:]]\{1,\}/\t/g' | sed 's|UUID=|/dev/disk/by-uuid/|g') diff --git a/ddi b/ddi new file mode 100755 index 0000000..dd942c9 --- /dev/null +++ b/ddi @@ -0,0 +1,10 @@ +# DDI - Dump Disk Information +# +# Dumps the UUID and label of a filesystem to its / (root) directory. +# Parses df output to find current mounts and then uses lsblk to get info. + +for dev in `df -h | grep ^/dev | cut -d\ -f1 | cut -d/ -f3`; do + loc=`cat /proc/mounts | grep $dev | cut -d\ -f2` + lsblk -fn -o UUID /dev/$dev > $loc/.uuid + lsblk -fn -o LABEL /dev/$dev > $loc/.label +done diff --git a/drvspeed b/drvspeed new file mode 100755 index 0000000..be22684 --- /dev/null +++ b/drvspeed @@ -0,0 +1,45 @@ +#!/bin/sh + +DEVICES="$(lsblk -l -x NAME -o NAME | tail -n +2 | cut -d\ -f1 | grep -v '[0-9]' | tr '\n' ' ') $(find /dev -maxdepth 1 -type b -name 'md*'|sort -V|rev|cut -d/ -f1|rev) $(find /dev -maxdepth 1 -type b -name 'dm-*'|sort -V|rev|cut -d/ -f1|rev)" + +case ${1} in + t) + + sudo echo -n '╔══════╤' + for DEVICE in $DEVICES; do + printf "%6s╤" "══════" + done + + echo -ne '\b╗\n║DEVICE│' + for DEVICE in $DEVICES; do + printf "%6s│" "${DEVICE}" + done + + echo -ne '\b║\n╟──────┼' + for DEVICE in $DEVICES; do + printf "%6s┼" "──────" + done + + echo -ne '\b╢\n║MB/sec│' + for DEVICE in $DEVICES; do + printf "%6s│" "$(sudo hdparm -t --direct /dev/${DEVICE} 2> /dev/null | grep 'MB/sec' | cut -d= -f2 | sed 's/\s\s*//g;s/MB\/sec//g')" + done + + echo -ne '\b║\n╚══════╧' + for DEVICE in $DEVICES; do + printf "%6s╧" "══════" + done + + echo -ne '\b╝\n' + ;; + *) + + sudo echo -n "DISK SPEED (MB/s): " + for DEVICE in $DEVICES; do + echo -n "${DEVICE}=" + echo -n "$(sudo hdparm -t --direct /dev/${DEVICE} 2> /dev/null | grep 'MB/sec' | cut -d= -f2 | sed 's/\s\s*//g;s/MB\/sec//g')," + done + echo -e '\b \b' + + ;; +esac diff --git a/filldev b/filldev new file mode 100755 index 0000000..b2bb803 --- /dev/null +++ b/filldev @@ -0,0 +1,22 @@ +#!/bin/bash + +sudo echo "garbage" > /dev/null +while [ $? -ne 0 ]; do !!; done + +SEC=falase + +for ARG in "$@"; do + case "${ARG}" in + "--secure") SEC=true ;; + *) [[ -b "${ARG}" ]] && DVC+=("${ARG}") || ( echo "${ARG} is not a block device"; exit 1 ) ;; + esac +done + +echo -n "Filling ${DVC[@]} with " +${SEC} && echo "random data" || echo "zeros" + +for DEV in "${DVC[@]}"; do + echo "${DEV}:" + ${SEC} && openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt /dev/null +while [ ${?} != 0 ]; do + sudo echo 'test' > /dev/null +done + +COUNT=$(sudo grep -c "${1}" /etc/crypttab) + +[[ ${COUNT} -eq 0 ]] && echo "Identifier '${1}' not found." && exit 1 +[[ ${COUNT} -gt 1 ]] && echo "Identifier '${1}' is not unique." && exit 1 + +CTLINE=$(sudo grep "${1}" /etc/crypttab) + +echo "Identifier '${1}' found in the following line:" +echo "${CTLINE}" + +MPNAME=$(sudo grep "${1}" /etc/crypttab | sed 's/#//g;s/ */ /g;s/ /\t/g' | awk '{ print $1 }') +DEVICE=$(sudo grep "${1}" /etc/crypttab | sed 's/#//g;s/ */ /g;s/ /\t/g;s/UUID=/\/dev\/disk\/by-uuid\//g' | awk '{ print $2 }') +KEYFIL=$(sudo grep "${1}" /etc/crypttab | sed 's/#//g;s/ */ /g;s/ /\t/g' | awk '{ print $3 }') + +set -x +sudo cryptsetup open "${DEVICE}" "${MPNAME}" --key-file "${KEYFIL}" diff --git a/makeraid b/makeraid new file mode 100755 index 0000000..2d9f261 --- /dev/null +++ b/makeraid @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +help() { + echo "USAGE: makeraid [level] [device] [device] ... [ [--spares] [device] ... ]" +} + +[[ :0:1:4:5:6:10: == *:${1}:* ]] && RL=${1} || { help; exit 1; } +shift + +until [ "${1}" = "--spares" ]; do + [ -b "${1}" ] && RD+=("${1}") || { help; exit 2; } + shift + [ ${#} = 0 ] && break +done + +if [ "${1}" == "--spares" ]; then + shift + for DV in ${@}; do + SD+=("${DV}") + done +fi + +[ "${#SD[@]}" -gt "0" ] && SO=( "-x" "${#SD[@]}" ) + +for MD in {0..127}; do + [ ! -b "/dev/md${MD}" ] && ND=${MD} && break +done + +mdadm -Cve 1.2 /dev/md${ND} -l "${RL}" -c 32 -n ${#RD[@]} ${RD[@]} ${SO[@]} ${SD[@]} diff --git a/mkcrypt b/mkcrypt new file mode 100755 index 0000000..6b19470 --- /dev/null +++ b/mkcrypt @@ -0,0 +1,76 @@ +#!/usr/bin/env sh + +CIPHER="aes-xts-plain64" +KEYSIZ='512' +HSHTYP='sha512' +ITTIME='5000' +USERAN='random' +DEVICE="${1}" + +# WE NEED ROOT PRIVILEGES + +if [ $EUID -ne 0 ]; then + echo "This script must be run as root." + exit 1 +fi + +# CONFIRM THE DEVICE + +echo "You are about to format the following device with LUKS:" +echo "" + +\lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINT,LABEL,UUID "${DEVICE}" +echo "" + +echo -n "Are you sure [y/N]? " +read CONFRM +echo "" + +if [ "${CONFRM,,}" = "y" ]; then + + echo -n "Fill ${DEVICE} with random data [Y/n]? " + read DEVFIL + echo "" + + if [ "${DEVFIL,,}" = "n" ]; then + echo "Not filling ${DEVICE} with random data." + else + echo "Filling ${DEVICE} with random data..." + echo "" + openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt