#!/bin/bash function dd-test { if ! test -f dd-sdrnd-2GB then echo "dd if=/dev/urandom bs=4096 count=488192 | pv -s 1907m > dd-sdrnd-2GB" echo "" dd if=/dev/urandom bs=4096 count=488192 | pv -s 1907m > dd-sdrnd-2GB echo "xzcat chainloader-101MB_13.04.img.xz |dd bs=4096 conv=notrunc of=dd-sdrnd-2GB" echo "" xzcat chainloader-101MB_13.04.img.xz |dd bs=4096 conv=notrunc of=dd-sdrnd-2GB sync fi #echo 'stop after making dd-sdrnd-2GB' #exit umount "$1"? echo "" >> result.txt echo "write random data via dd (2GB)" | tee -a >> result.txt echo "pv dd-sdrnd-2GB | dd bs=4096 count=488192 2>> result.txt > $1" pv dd-sdrnd-2GB | dd bs=4096 count=488192 2>> result.txt > "$1" dd if=/dev/zero bs=4096 count=488192 | pv > /dev/null 2> /dev/null echo "" >> result.txt echo "read the same data via dd (2GB)" | tee -a >> result.txt echo "pv -s 1907m $1 | dd bs=4096 count=488192 2>> result.txt > dd-sdx-2GB.img" pv -s 1907m "$1" | dd bs=4096 count=488192 2>> result.txt > dd-sdx-2GB.img dd if=/dev/zero bs=4096 count=488192 | pv > /dev/null 2> /dev/null } LC_ALL=C LANG=C # usage if [ "$(whoami)" != "root" ] || [ $# -lt 1 ] || [ "${1:0:7}" != "/dev/sd" ] then echo "Usage:" echo "testing write/read speed of USB drives" echo " sudo $0 " echo "example: sudo $0 /dev/sdd # finished with the tarred system installed" echo "example: sudo $0 /dev/sdd tar # finished with the tarred system installed" echo "example: sudo $0 /dev/sdd dd # finished with the dd-cloned system installed" exit fi ltest0=$(grep " / " /etc/mtab|cut -d ' ' -f 1) if [ "$ltest0" == "/cow" ] then ltest1=$(grep " /isodevice" /etc/mtab|cut -c 5-8) livedrive=$ltest1 elif [ "${ltest0/\/dev\/[^f]d}" != "$test0" ] then livedrive=$(grep " / " /etc/mtab|cut -c 5-8) else livedrive="not_found" fi #echo ltest0=$ltest0 #echo ltest1=$ltest1 #echo livedrive=$livedrive for i in /dev/[^f]d?; do parted -ls|grep -B1 "$i"|tr '\n' '\t';echo "";done echo Live drive: /dev$livedrive if [ "$1" == "/dev$livedrive" ] then echo "$1 is the live drive, so I quit" exit fi tmp=${1/\/dev\/} #echo $tmp #find /sys/devices -name "*sd?"|grep usb|sed s#.*/## isusb=$(find /sys/devices -name "$tmp"|grep usb|sed s#.*/##) #echo $isusb if [ "$isusb" == "$tmp" ] then echo "$1 is a USB device, so I am prepared to continue" else echo "$1 is not a USB device, so I quit" exit fi echo "-----------------------------------------------------------------" sudo fdisk -lu "$1" echo "-----------------------------------------------------------------" sudo parted -l|grep --colour -B1 -A15 "$1" read -p "Is this the device you want to test? (y/N) " ans if [ "$ans" != "y" ] then echo "You did not answer yes, so I quit" fi # start by writing the header for the test result echo "-----------------------------------------------------------------" >> result.txt sudo parted -l|grep -B1 "$1"|grep -v "$1" >> result.txt # select if dd-test is performed before or after tar-test if [ "$2" == "" ] || [ "$2" == "tar" ] then dd-test $1 fi # make file system and install operating system from compressed tarfile echo "" >> result.txt echo "write small files from 'ball.tar.xz'" | tee -a >> result.txt # swap off and unmount all partitions swapoff "${1}*" 2>/dev/null umount "${1}"? # make partition echo fdisk -u "$1" echo "o n p 1 $((1024*1024/512)) p w q "|fdisk -u "$1" sync partprobe -s "$device" sync # make file system mkfs.ext4 "${1}1" sync # mount what will be the root partition mount "${1}1" /mnt if [ $? != 0 ] then echo "mount of what should become the root partition failed: ${1}1" exit fi # copy the files startdir="$(pwd)" cd /mnt df . starttime=$(date +"%s") echo 'pv "$startdir"/ball.tar.xz | tar -xJ' | tee -a >> "$startdir"/result.txt pv "$startdir"/ball.tar.xz | tar -xJ echo "the tarball $tarball is expanded, syncing the drive ..." | tee -a >> "$startdir"/result.txt sync endtime=$(date +"%s") usedtime=$(($endtime - $starttime)) echo "Time used by tar and sync: $usedtime s" | tee -a >> "$startdir"/result.txt usedsize=$(sudo du -b -s /mnt | cut -f 1) usedsize=$(($usedsize/1000)) echo "Size written by tar: $usedsize kB (1kB=1000B)" | tee -a >> "$startdir"/result.txt ratebytar=$(($usedsize / $usedtime)) echo "Write speed when tar writes small files: $ratebytar kB/s" | tee -a >> "$startdir"/result.txt cd "$startdir" # match the UUID to fstab uid=$(grep ' / ' /mnt/etc/fstab|grep ^UUID|cut -d= -f2|cut -d ' ' -f1) echo $uid tune2fs -U "$uid" "${1}1" /bin/echo -n "Bootloader: " grub-install --boot-directory=/mnt/boot "$1" sync umount "${1}1" /bin/echo -n "File checking with " e2fsck -f "${1}1" # finish echo "-----------------------------------------------------------------------" echo "The installation to ${1}1 has finished" # select if dd-test is performed before or after tar-test if [ "$2" == "dd" ] then dd-test "$1" fi