You are not logged in.
This is my work. Anyone can use it.
sget-pendrive() {
n_found=0
for drive in /dev/sd? ; do
if [[ -b $drive ]] ; then
letter=${drive:7:1}
udev=`udevadm info -q path -n $drive`
udev=`udevadm info -a -p $udev `
#search KERNEL=="sdL"
search=`echo KERNEL==\"sd$letter\"`
udev_info=`echo "$udev" | grep $search | tr -d ' ' `
[[ $udev_info != "KERNEL==\"sd$letter\"" ]] && continue
udev_info=`echo "$udev" | grep 'SUBSYSTEM=="block"' | tr -d ' ' `
[[ $udev_info != "SUBSYSTEM==\"block\"" ]] && continue
udev_info=`echo "$udev" | grep 'ATTR{events}=="media_change"' | tr -d ' ' `
[[ $udev_info != "ATTR{events}==\"media_change\"" ]] && continue
udev_info=`echo "$udev" | grep -E 'ATTR{size}=="[0-9]{1,}"' | tr -d ' ' `
[[ $udev_info != *"ATTR{size}=="* ]] && continue
value=` echo "$udev_info" | cut -d "\"" -f2 | cut -d "\"" -f1 `
[[ $value -lt 1 ]] && continue
echo -e "Pendrive \e[1;39;41m$drive\e[0m (size $(($value *512/1000/1000/1000 ))GBi) detected."
#echo -e "Pendrive \e[1;39;41m/dev/sd$drive\e[0m (size $(($value *512/1024/1024/1024 ))Gbyte) detected."
n_found=$(( $n_found + 1 ))
fi
done
[[ $n_found -lt 1 ]] && echo "No pendrive found" || echo "$n_found pendrives total found."
}
Last edited by simona70 (2018-10-17 19:52:25)
Offline
Please edit your post to use code tags.
Rather than looping through every letter in the alphabet and check for the presence of a device node, why not just do the following:
for drive in /dev/sd?; do
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Please edit your post to use code tags.
Rather than looping through every letter in the alphabet and check for the presence of a device node, why not just do the following:
for drive in /dev/sd?; do
Thanks :-)
Offline
Until you get >26 drives attached and it gets assigned /dev/sdaa ;)
Last edited by Morganamilo (2018-10-17 20:03:52)
Offline
I usually just eyeball the output of lsblk to see which drives I have attached, but this could be a handy alias for when I'm about to write an image - thanks
What about drives that don't appear as /dev/sd*? There are external SSD drives that appear as /dev/nvme* when attached via USB 3.1 Type-C if you have the appropriate chipset. Come to think about it /dev/mmc* would fall into this category as well.
Edit - How about walking through...
lsblk -dloNAME | tail -n +2
...instead?
Last edited by Slithery (2018-10-17 21:09:56)
Offline
Why the tail?
lsblk -ndlo NAME
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Some minor suggestions for improvements.
udev=`udevadm info -q path -n $drive`
udev=`udevadm info -a -p $udev `
Do you mean to immediately overwrite the udev variable with the second line?
search=`echo KERNEL==\"sd$letter\"`
Unnecessary use of echo. Just double-quote "KERNEL=\"sd$letter\"" instead.
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
Why the tail?
lsblk -ndlo NAME
Obviously because I missed the -n option when reading the lsblk man page.
I assumed there would be such an option but I just couldn't see it, I think I'm going to have to put off the inevitable and succumb to glasses
Offline
Wouldn't this do the same thing? I can't test here as I don't have multiple devices (and no removable drives):
#!/bin/bash
sget-pendrive() {
for drive in $(lsblk -ndlo NAME); do
udevadm info -a -p "$(udevadm info -q path -n $drive)" | awk -F\" '
/SUBSYSTEM="block"/ { match++; }
/ATTR(events)=="media_change"/ { match++; }
/ATTR(size)=="[0-9][0-9]*"/ { value=$2; if (value > 0) match++; }
END { if (match > 2) printf "Pendrive \e[1;39;41m'$drive'\e[0m (size %0.1fGBi) detected.", value; }'
done
}
I dropped the test for KERNEL==sd$letter as that's a given, and this version doesn't (currently) count the number of devices found. But otherwise this should do the same thing with *far* fewer subshells and processes.
Last edited by Trilby (2018-10-18 05:01:25)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
I usually just eyeball the output of lsblk to see which drives I have attached, but this could be a handy alias for when I'm about to write an image - thanks
What about drives that don't appear as /dev/sd*? There are external SSD drives that appear as /dev/nvme* when attached via USB 3.1 Type-C if you have the appropriate chipset. Come to think about it /dev/mmc* would fall into this category as well.
Edit - How about walking through...
lsblk -dloNAME | tail -n +2
...instead?
oh yes... thanks :-)
Offline
Thanks for hints :-) I have written it to have exact /dev/... to use before dd if= of= for write image. A faster way. This final code say also size.
Interesting the:
sget-pendrive() {
for drive in $(lsblk -ndlo NAME); do
udevadm info -a -p "$(udevadm info -q path -n $drive)" | awk -F\" '
/SUBSYSTEM="block"/ { match++; }
/ATTR(events)=="media_change"/ { match++; }
/ATTR(size)=="[0-9][0-9]*"/ { value=$2; if (value > 0) match++; }
END { if (match > 2) printf "Pendrive \e[1;39;41m'$drive'\e[0m (size %0.1fGBi) detected.", value; }'
done
}
Anymore for now this is final for now.
sget-pendrive() {
n_found=0
for drive in /dev/sd?? /dev/nvme* ; do
if [[ -b $drive ]] ; then
letter=${drive:7:1}
udev=`udevadm info -q path -n $drive`
udev=`udevadm info -a -p $udev `
search=`echo KERNEL==\"sd$letter\"`
udev_info=`echo "$udev" | grep $search | tr -d ' ' `
[[ $udev_info != "KERNEL==\"sd$letter\"" ]] && continue #not correct device
udev_info=`echo "$udev" | grep 'SUBSYSTEM=="block"' | tr -d ' ' `
[[ $udev_info != "SUBSYSTEM==\"block\"" ]] && continue #not correct device
udev_info=`echo "$udev" | grep 'ATTR{events}=="media_change"' | tr -d ' ' `
[[ $udev_info != "ATTR{events}==\"media_change\"" ]] && continue #not correct device
udev_info=`echo "$udev" | grep -E 'ATTR{size}=="[0-9]{1,}"' | tr -d ' ' `
[[ $udev_info != *"ATTR{size}=="* ]] && continue #not correct device
value=` echo "$udev_info" | cut -d "\"" -f2 | cut -d "\"" -f1 `
[[ $value -lt 1 ]] && continue #not correct device
echo -e "Pendrive \e[1;39;41m$drive\e[0m (size $(($value *512/1000/1000/1000 ))GBi) detected."
n_found=$(( $n_found + 1 ))
fi
done
[[ $n_found -lt 1 ]] && echo "No pendrive found" || echo "$n_found pendrives total found."
}
Last edited by simona70 (2018-10-20 09:39:36)
Offline
I did also this small scripts to mount images (virtualbox, kvm, ecc.)
#future to add???: iso and crypto file partitions
srun-mount-img() {
if [[ $1 == "-h" || $1 == "--help" ]] ; then
echo "Sintax: srun-mount-img [--help|-h] file-name[.ext|.format] [format] [partition mumber inside volume 1-n]"
echo "Allowed formats: $allow_formats"
echo "Dont' forget to use 'srun-umount-img file-name.ext' to undo when finished."
return 0
fi
if [[ ! -d "$HOME/mount/image" ]] ; then
echo "Please create $HOME/mount/image with command 'mkdir -p $HOME/mount/image'. Exit."
return 1
fi
if [[ ! -r /bin/qemu-nbd ]] ; then
echo "Missing qemu-nbd command! Exit"
return 1
fi
image="$1"
if [[ $image == "" ]] ; then
echo "Image missing. Please insert it as first paramater. Exit."
echo "Sintax: srun-mount-img [--help|-h] file-name[.txt|.format] [format] [partition mumber inside volume 1-n]"
return 1
else
image="$1"
fi
if [ ! -f "$image" ]; then
echo "File image $image not found!! Exit."
return 1
fi
ext="${image##*.}"
#add iso
#sudo mount -o loop path/to/iso/file/YOUR_ISO_FILE.ISO /media/iso
#add crypto
allow_formats="raw gcow2 vdi blkdebug blklogwrites blkreplay blkverify bochs cloop copy-on-read dmg file ftp ftps gluster host_cdrom host_device http https iscsi iser luks nbd nfs null-aio null-co nvme parallels qcow qcow2 qed quorum raw rbd replication sheepdog ssh throttle vdi vhdx vmdk vpc vvfat"
format=$2
if [[ $format == "" ]] ; then
if [[ $ext != "" ]] ; then
format=$ext
else
echo "Format missing. Please insert it as second paramater. Exit."
return 1
fi
fi
if [[ ! $allow_formats =~ $format ]] ; then
[[ $format == "iso" ]] && echo "ISO: future implementation." || echo "Format $format unknown. Sorry. Exit."
return 1
fi
if [[ -d "$HOME/mount/image/$image" ]] ; then
echo "Directory $HOME/mount/image/$image already exist!! Exit."
return 1
fi
[[ $3 == [0-9]* ]] && n_part=$3 || n_part=2
mkdir -p "$HOME/mount/image/$image"
if [ $? -ne 0 ]; then
echo "Can't create directory $HOME/mount/image/$image! Exit."
return 1
fi
sudo modprobe nbd max_part=63
if [ $? -ne 0 ]; then
echo "Can't execute command 'modprobe nbd max_part=63'! Exit."
return 1
fi
#-P, --partition=num Only expose partition num use?
sudo qemu-nbd -c /dev/nbd0 --format=$format "$image"
if [ $? -ne 0 ]; then
echo "Can't execute command 'qemu-nbd -c /dev/nbd0 --format=$format \"$image\"'! Exit."
return 1
fi
sudo mount /dev/nbd0p$n_part "$HOME/mount/image/$image"
if [ $? -ne 0 ]; then
echo "Can't execute command 'mount /dev/nbd0p$n_part $HOME/mount/image/$image'! Exit."
return 1
fi
cd "$HOME/mount/image/$image"
if [ $? -ne 0 ]; then
echo "Can't execute command 'cd $HOME/mount/image/$image'! Exit."
return 1
fi
echo "Dont' forget to use 'srun-umount-img file-name.ext' to undo when finished."
return 0
}
srun-umount-img() {
if [[ ! -r /bin/qemu-nbd ]] ; then
echo "Missing qemu-nbd command! Exit"
return 1
fi
image=$1
if [[ $image == "" ]] ; then
echo "Parameter image missing. Please insert it as first paramater. Exit."
return 1
fi
if [[ ! -d "$HOME/mount/image/$image" ]] ; then
echo "None to do. Directory $HOME/mount/image/$image do not exist!! Exit."
return 1
fi
if [[ $PWD == "$HOME/mount/image/$image" ]] ; then
cd "$HOME/mount/image"
if [ $? -ne 0 ]; then
echo "Can't execute command 'cd ..'! Exit."
return 1
fi
fi
sudo umount "$HOME/mount/image/$image"
if [ $? -ne 0 ]; then
echo "Can't execute command 'umount $HOME/mount/image/$image'! Exit."
return 1
fi
sudo qemu-nbd -d /dev/nbd0 >/dev/null
if [ $? -ne 0 ]; then
echo "Can't execute command 'qemu-nbd -d /dev/nbd0'! Exit."
return 1
fi
rmdir "$HOME/mount/image/$image"
if [ $? -ne 0 ]; then
echo "Can't execute command 'rmdir ~/mount/image/$image'! Exit."
return 1
fi
return 0
}
Last edited by simona70 (2018-10-20 09:51:23)
Offline