Author Topic: [ SOLVED ] Is it a Removable Drive?  (Read 2840 times)

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
[ SOLVED ] Is it a Removable Drive?
« on: March 13, 2011, 07:53:27 PM »
If faced with an unknown system, how would you determine, at the command line, whether a particular partition was on a removable drive or on a fixed drive?

I am looking for a command that will return an answer from which I can determine if the partition is removable or fixed.

For instance if I try to mount a fixed partition using pmount, it will error telling me it is not a removable drive. (it also errors out if a removable is already mounted so that is not certain regarding whether the partition is fixed or not).

So the drives/partitions must be flagged or some reference to them stored somewhere for pmount to have knowledge of the type of partition it is.

If I could access the flags for the partitions then that should allow me to determine the type of partition I am dealing with.

Ideas welcome.  Thanks.

regards.
« Last Edit: March 23, 2011, 02:47:27 AM by Just19 »

Offline Old-Polack

  • Administrator
  • Super Villain
  • *****
  • Posts: 11688
  • ----IOFLU----
Re: Is it a Removable Drive?
« Reply #1 on: March 13, 2011, 09:40:15 PM »
If faced with an unknown system, how would you determine, at the command line, whether a particular partition was on a removable drive or on a fixed drive?

I am looking for a command that will return an answer from which I can determine if the partition is removable or fixed.

For instance if I try to mount a fixed partition using pmount, it will error telling me it is not a removable drive. (it also errors out if a removable is already mounted so that is not certain regarding whether the partition is fixed or not).

So the drives/partitions must be flagged or some reference to them stored somewhere for pmount to have knowledge of the type of partition it is.

If I could access the flags for the partitions then that should allow me to determine the type of partition I am dealing with.

Ideas welcome.  Thanks.

regards.

For most systems allowing HAL auto mounting, df should do. If it's removable, it's mounted on /media/<something>
Old-Polack

Of what use be there for joy, if not for the sharing thereof?



Lest we forget...

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
Re: Is it a Removable Drive?
« Reply #2 on: March 14, 2011, 09:34:38 AM »
If faced with an unknown system, how would you determine, at the command line, whether a particular partition was on a removable drive or on a fixed drive?

I am looking for a command that will return an answer from which I can determine if the partition is removable or fixed.

For instance if I try to mount a fixed partition using pmount, it will error telling me it is not a removable drive. (it also errors out if a removable is already mounted so that is not certain regarding whether the partition is fixed or not).

So the drives/partitions must be flagged or some reference to them stored somewhere for pmount to have knowledge of the type of partition it is.

If I could access the flags for the partitions then that should allow me to determine the type of partition I am dealing with.

Ideas welcome.  Thanks.

regards.

For most systems allowing HAL auto mounting, df should do. If it's removable, it's mounted on /media/<something>

  I am seeking a CLi command which will return 'the type' of the partition that the command specifies .....  as in, 'it is on a removable' or on a 'fixed' disk.

So I would like to see

Code: [Select]
$ <command>  /dev/sdc5
removable

$ <command>  /dev/sda2
fixed

I hope that better explains the quest.

Thanks for responding.

regards.

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
Re: Is it a Removable Drive?
« Reply #3 on: March 14, 2011, 09:47:49 AM »
I have considered using 'pmount' for this, but it is not as clean as I would like it, and MAY return different results on different systems.

But for clarity here is an example of using 'pmount' .....  drives a & b are fixed and d is removable; one partition of a fixed disk is mounted under /media, the other partition specified is not mounted at all.
Here are some results ....

[user@Dell ~]$ pmount /dev/sdd1                                   <-   the removable drive partition mounts OK
[user@Dell ~]$
[user@Dell ~]$ pmount /dev/sdd1
Error: device /dev/sdd1 is already mounted to /media/sdd1 <- it won't mount it again giving an error
[user@Dell ~]$
[user@Dell ~]$ pmount /dev/sda1                                   
Error: device /dev/sda1 is already mounted to /media/disk  <- as above sda1 is already mounted so I get no info
[user@Dell ~]$
[user@Dell ~]$ pmount /dev/sdb1
Error: device /dev/sdb1 is not removable                           <- sdb1 is not mounted and pmount refuses to do so as it is not on a removable disk.
[user@Dell ~]$


My contention is there should be a simple command available for me to determine if a specified device is removeable or not.

regards.
« Last Edit: March 14, 2011, 09:49:52 AM by Just19 »

Offline muungwana

  • Hero Member
  • *****
  • Posts: 6266
Re: Is it a Removable Drive?
« Reply #4 on: March 14, 2011, 12:10:51 PM »

run this command from the terminal and you will get a "1" for a removable drive and a "0" for a non removable drive

cat  /sys/block/<drive>/removable

in your case:

"cat /sys/block/sdd/removable"  will return "1" since it is a removable drive.

A combination of the above command and reading of "/etc/mtab" to check for already mounted partitions will give you the info you need.
.. 3 things are certain in life : death, taxes and software bloat ..
.. tell me something i don't know, something i can use as i struggle to reason with the world around me ..

Offline AS

  • Hero Member
  • *****
  • Posts: 4098
  • Have a nice ... night!
Re: Is it a Removable Drive?
« Reply #5 on: March 14, 2011, 12:16:53 PM »
From manual page of pmount:

Quote
ยท device is removable (USB, FireWire, or MMC device, or /sys/block/drive/removable is 1) or whitelisted in /etc/pmount.allow

for removable device (i.e. /dev/sdc) you will find a file named /sys/block/drive/removable containg a line of text which is 1,

try this shell script (to be finished...):
Code: [Select]
for DEVICE in /sys/block/*/removable
do
        if test `cat $DEVICE` -eq "1"
        then
                echo "device " $DEVICE is removable
        else
                echo "device " $DEVICE is fixed;
        fi
done

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
Re: Is it a Removable Drive?
« Reply #6 on: March 14, 2011, 02:06:38 PM »
Thank you both for the replies.

That is the solution I was looking for.

Thanks again

regards.

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
Re: [ SOLVED ] Is it a Removable Drive?
« Reply #7 on: March 23, 2011, 10:15:39 AM »
Although the initial problem is solved I thought to ask a further question in this thread about incorporating this into a BASH script.

Not being a scripter I am a bit lost with how to achieve this .......

I want to find all the devices attached to the PC, so I use


Quote
$ sfdisk -s | grep -e /dev/ | cut -d: -f1
/dev/sda -> fixed disk
/dev/sdb -> fixed disk
/dev/sdd -> removable disk

or maybe

Quote
$ sfdisk -s | grep -e /dev/ | cut -d: -f1 | cut -d / -f3
sda
sdb
sdd


That is as it should be.
Next I want to display - maybe using Zenity - those devices to the user for them to select one.
Again not a problem ......

Code: [Select]
#!/bin/bash

DRV=$(sfdisk -s | grep -e /dev/ | cut -d: -f1)
#
# omit removable drives from the list
#
DEVICE=$(zenity --height=300 --width=250 --list --title "Selection" \
--text "    Please select a device " \
--column "Drive" $DRV )
echo $DEVICE
exit 0


What I would like to do is to omit those drives that are removable from the list presented to the user - in the above case that would be /dev/sdd.

Maybe checking against /sys/block/<drive>/removable ?  where <drive> is  sda, sdb, etc and not /dev/sda, /dev/sdb.

I would have a preference to not using files to store data ...  just variables or such ...  but if needs must ...

Pointers would be appreciated .......  I seem unable to get my head around this for some reason ....  well maybe the reason is I do not know BASH  ;D

Thanks for any help.

regards.

Offline AS

  • Hero Member
  • *****
  • Posts: 4098
  • Have a nice ... night!
Re: [ SOLVED ] Is it a Removable Drive?
« Reply #8 on: March 23, 2011, 12:55:27 PM »
Hi Just19,

the following should do what you are searching for:

Code: [Select]
#!/bin/bash
DRV_ALL=$(sfdisk -s | grep -e /dev/ | cut -d: -f1)
#
# omit removable drives from the list
#
DRV=""
for TEST_REMOVABLE in $DRV_ALL
do
        if test $(cat /sys/block/`basename $TEST_REMOVABLE`/removable) -eq "0"             # only not removable disks
        then
                DRV=$DRV" "$TEST_REMOVABLE
        fi
done

DEVICE=$(zenity --height=300 --width=250 --list --title "Selection" \
--text "    Please select a device " \
--column "Drive" $DRV )
echo $DEVICE
exit 0

While testing the above code, I discovered that the SD card reader of my system is recognized as not removable.   ??? (a PCI device "O2 Micro" /dev/mmcblk0).
this is a bug of the related module, at the same time its name is enough different to not be confused as a disk.

AS

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
Re: [ SOLVED ] Is it a Removable Drive?
« Reply #9 on: March 23, 2011, 01:15:08 PM »
Thank you!

Much appreciated.  ;)

test is one of many I have never used ......  a first time for everything  ;D

regards

Offline AS

  • Hero Member
  • *****
  • Posts: 4098
  • Have a nice ... night!
Re: [ SOLVED ] Is it a Removable Drive?
« Reply #10 on: March 23, 2011, 01:31:58 PM »
Thank you!

Much appreciated.  ;)

test is one of many I have never used ......  a first time for everything  ;D

regards

Have never used zenity before.  ;)  A good exchange!

AS