Skip to content

Instantly share code, notes, and snippets.

@somian
Last active December 27, 2015 20:49
Show Gist options
  • Save somian/7387025 to your computer and use it in GitHub Desktop.
Save somian/7387025 to your computer and use it in GitHub Desktop.
#!/bin/bash
declare -x TOPDIR_STORAGE=${HOME:-"/home/retread"}/.config/virt/virtualdisks
declare CLOO=$1 # e.g. "SANDISK"
declare -a DISK_GRP=(\
$(awk --sandbox -F : '/disk/{print $1,$3}' /etc/group)\
)
declare DISK_GID=${DISK_GRP[1]}
declare +x DEV_idPATH
function rattatt
{
string=$1
whold=$(find /dev/disk/by-id -iname usb-\*$string\* -printf '%f\n' |sed -n '/0:0$/{p}')
DEV_idPATH="/dev/disk/by-id/$whold"
printf '%s' "$DEV_idPATH"
}
discovery=$(rattatt $CLOO)
declare -a DEV_Attr
mapfile -t DEV_Attr < <(for YOW in $discovery* ; do \
printf '\n\n%s\n%s' "$YOW" "$(/sbin/blkid -o export -pu filesystem "$YOW")" ; done)
if [[ ${#DEV_Attr[*]} -gt 1 ]]
then # we got some data and the device is of interest
# script debugging output (to filedescriptor 2)
printf >&2 '%u elements → ' "${#DEV_Attr[@]}"
# Oh, you want me to explain? ;-) ...ok, not easy stuff. The 0th ele of the array
# is null. We need it removed. To do so, we use a bash-ish "slice" of the array
# that uses the 1st to Nth arrays as the new content.
printf >&2 'ELEMENT «%s» ' "${DEV_Attr[@]:1:2}"
printf >&2 '\n\n'
# /end debugging output/
# Oh, you want me to explain? again? Not easy ... <sigh>. It's some ugly perl
# that treats each paragraph as a record and parses for parameters.
printf '%s\n' "${DEV_Attr[@]:1:${#DEV_Attr[*]}}" |
perl -n000 -Mstrict -wle '
BEGIN { use vars qw($ostr1 $ostr2 $bomb) }
my $fline;
my $use_as_id;
my @ltok = grep { $_ } split qq[\n] => $_;
$bomb = $ltok[0];
$bomb =~s{[ ].*}{};
die qq[No disk dev path] unless $bomb;
$bomb =~ m{^\S+ /usb- ([^-]+)\S*}x and ($fline = $1) or ($fline = qq[]);
if (grep /\bPART_ENTRY/ , @ltok) {
my ($fstype, $ustr);
DRUM:
{
for my $chp (reverse @ltok[1 .. $#ltok]) {
$chp=~ /\bTYPE\b=(\w+)/ and $fstype = $1;
$chp=~ /\bLABEL=([[:print:]]+)/ and $ustr = $1;
unless ($ustr) { $chp=~ /\bUUID=([[:print:]]+)/ and $ustr = $1; }
$ustr && last;
}
warn "string for ID not found" unless $ustr;
}
$use_as_id = "$fstype-$ustr-part" . ($fline ? "of-$fline" : "NAUGHTY") . ".vmdk";
} else {
$use_as_id = "${fline}-disk.vmdk";
}
$ostr2 = "-filename " . $ENV{"TOPDIR_STORAGE"} . "/$use_as_id";
$ostr1 = "-rawdisk $bomb";
print "VBoxManage internalcommands createrawvmdk \\\n$ostr1 \\\n$ostr2\n";
'
else
printf >&2 'blkid did not get us any data about %s, aborting.\n' "${discovery}"
fi
### vim: set ft=sh et sw=4 :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment