Solved Mountable character devices?

Hello.

What form can have character devices.

I have seen da0, dap0, ada0, cd0,...

Does it exist a logic for the name (da0= usb?, ada0=disk?)

Thanks.
 
All devices are character devices nowadays, there are no block devices anymore.

Traditionally the names were da* for SCSI and ad* for IDE, even older was wd* for IDE. This distinction is somewhat gone now as everything uses CAM (and thus SCSI).
 
Thanks SirDice

So, if using a bash script for automounting all mountable-devices, it needs to check for da*, ada*, wd* and cd*. (nothing missing?).

Other question, what kind of partition can be found: das0, dap0, (something else?).

Thanks.

Fre;D
 
Hello.

Here script for mounting all devices, formatted with UFS, EXT4, FAT, FAT32 or NTFS.
fuse needs to be installed.
Tested on VirtualBox, VMware and "real machines".
Code:
#!/bin/sh

# Script for mounting all devices, formated with UFS, EXT4, FAT, FAT32 or NTFS.

clear
substring1=da
substring2=wd
substring3=p
substring4=s

# unmount all
cd /dev

# analyze character-files in /dev
for i in $( ls ); do

# seeking if the character-file in /dev is a mountable disk
if [ "$i" != "${i%$substring1*}" ] || [ "$i" != "${i%$substring2*}" ] ; then

# seeking if the character-file in /dev has partition
if [ "$i" != "${i%$substring3*}" ] || [ "$i" != "${i%$substring4*}" ] ; then
echo "Substring IS in $i"

# unmout the device if conditions above are ok
umount /dev/$i
fi
fi
done

#read some
clear

# remove all directories used by previous mount
if [ -d "/media/disk" ]; then
rmdir /media/disk/*
else
mkdir /media/disk
fi
echo "removed dir"

#read some
clear

cd /dev

# mount all

# analyze character-files in /dev
for i in $( ls ); do

# seeking if the character-file in /dev is a mountable disk
if [ "$i" != "${i%$substring1*}" ] || [ "$i" != "${i%$substring2*}" ] ; then

# seeking if the character-file in /dev has partition
if [ "$i" != "${i%$substring3*}" ] || [ "$i" != "${i%$substring4*}" ] ; then
echo "Substring IS in $i"

# Create directory for the finded device
mkdir /media/disk/$i

# trying to mount msdos
mount_msdosfs /dev/$i /media/disk/$i
clear
# trying to mount ext4
ext4fuse  /dev/$i /media/disk/$i
clear
# trying to mount ntfs
ntfs-3g /dev/$i /media/disk/$i
clear
mount /dev/$i /media/disk/$i
fi
fi
done
echo "done"

# read some
clear

PS: Comments are welcome (mainly how to not try mount if already mounted) !

Fre;D
 
Last edited by a moderator:
You might want to have a look at the output of sysctl kern.disks. And you can figure out what filesystem is used with file(1), based on the return you can use the correct mount(8) command. You could also process the output of gpart(8) to figure out the filesystem type and partitioning scheme.
 
You don't seem to have any formatting in your script. This might be a side effect of the forum software, but if not you should think about adding some indentation to make things more readable. For instance you have nested if statements, but this is not obvious at first glance. The shell interpreter doesn't care how it's formatted but the next person reading it probably will.

"Programs must be written for people to read, and only incidentally for machines to execute." ― Harold Abelson, Structure and Interpretation of Computer Programs
 
Hello and thanks everybody for your great advices.
Of course, I will study all your propositions.

Hum, in a script, how to catch the output of a command ?
Because it you do mount /dev/ad0 /media/disk0 and the device is mounted ok => no output message.

So the idea was :
Code:
if "there is a message from mount" then
if "there is a message from extafuse" then
if "there is a message from msdosfs" then
if "there is a message from ntfs-3g" then
"device is not mountable"
Hum, about sysutils/automount....
I like the idea but I dont like how it works.
For example, with VirtualBox or VMware, it does not work well at runtime.
With the script above, I always can re-mount device, even if it was unmounted and used by host system.

Code:
You don't seem to have any formatting in your script.
Ok, I will do it (and you are totally right).
[EDIT] Done...

Fre;D
 
Use case for multiple conditionals.

The output of mount with no parameters can be checked to see if a particular path is mounted.

Spaces and tabs are free, please indent to indicate control flow.
 
The output of mount with no parameters can be checked to see if a particular path is mounted.

Spaces and tabs are free, please indent to indicate control flow.

Yep, it is what I want to do.
But I did not find the syntax how to "catch" the output of mount in a script ( for analyze it ).

Fre;D
 
It's the same as any other output. Use backticks, $(), or redirect to a file.

An example with backticks:
Code:
#!/bin/sh
mounted=`/sbin/mount`
echo $mounted
 
Hum, in a script, how to catch the output of a command ?
Because it you do mount /dev/ad0 /media/disk0 and the device is mounted ok => no output message.
Most commands have a return code you can use to check if the command succeeded or not. It's a special variable you can check in scripts.

Code:
mount /dev/ad0 /media/disk0
if [ $? -eq 0 ]; then
  echo "Success!"
else
  echo "Something went wrong!"
fi

From sh(1):
Code:
     $?      Expands to the exit status of the most recent pipeline.

A value of 0 means the command was successful. Any other value may indicate an error or failure.
 
=>
Code:
mount /dev/ad0 /media/disk0
if [ $? -eq 0 ]; then
echo "Success!"
else
echo "Something went wrong!"
fi

Excellent.
Many thanks.

By the way... =>
Code:
#!/bin/sh
mounted=`/sbin/mount`
echo $mounted
=> echo $mounted = "Lot of blabla" => It works ;-)

But this =>
Code:
#!/bin/sh
mounted=`/sbin/mount /dummy/dev /dummy/mnt`
echo $mounted
=> echo $mounted = "" => nothing ;-(

Fre;D
 
...
By the way... =>
Code:
#!/bin/sh
mounted=`/sbin/mount`
echo $mounted
=> echo $mounted = "Lot of blabla" => It works ;-)

The output of that command went to stdout(4) so it got captured in the variable.

But this =>
Code:
#!/bin/sh
mounted=`/sbin/mount /dummy/dev /dummy/mnt`
echo $mounted
=> echo $mounted = "" => nothing ;-(

Fre;D
The output of /sbin/mount /dummy/dev /dummy/mnt would be empty on stdout(4) for the variable. It would all show to stderr(4).
 
If the command succeeded there would be no output to capture. Same if you entered the command on the command line, if it works you just get a prompt with no messages. So the variable would also be empty.
 
Hello and many thanks to clarify.

SirDice => Your tip ( if [ $? -eq 0 ]; then) is working like charm => exactly what I want => many, many thanks.

Now, as cherry on top of the cake, is it possible to change the name (or move) a mounted character ?

For example, if /dev/da0 was mounted on /media/disk/da0 => change into /media/disk/ntfs_da0
I have try with mv /media/disk/da0 /media/disk/ntfs_da0 but without luck...

Thanks.

Fre;D
 
No, you can't move it like that. You would have to unmount and remount it where you want it. Alternatively, if you require both mountpoints you could mount the second one using nullfs(5).
 
Back
Top