Accessing raw devices (hard drives) with Java ?

  • Thread starter =?iso-8859-1?q?S=E9bastien_de_Mapias?=
  • Start date
?

=?iso-8859-1?q?S=E9bastien_de_Mapias?=

Hi,
Let's I have (under Solaris for example) a device /dev/[r]dsk/c0t2d0s0
that's not mounted (so I can't performe any 'ls' etc.), and I'd like
to
traverse its bytes: is there a way to access this data from Java ?
Has anyone already tried to write such a program ?

Thanks !
Regards,
Seb
 
M

Mike Baranczak

Sébastien de Mapias said:
Hi,
Let's I have (under Solaris for example) a device /dev/[r]dsk/c0t2d0s0
that's not mounted (so I can't performe any 'ls' etc.), and I'd like
to
traverse its bytes: is there a way to access this data from Java ?

Short answer: no.

Long answer: Java only gives you a very high-level view of the file
system. If you want to access an unmounted partition from Java, you'll
either have to write some JNI code or use Runtime.exec() to call a
native command which reads the partition data (dd if=/dev/dsk/c0t2d0s0
of=whatever etc...)

-MB
 
A

Andreas Leitgeb

Mike Baranczak said:
Let's I have (under Solaris for example) a device /dev/[r]dsk/c0t2d0s0
that's not mounted (so I can't performe any 'ls' etc.), and I'd like
to traverse its bytes: is there a way to access this data from Java ?
Short answer: no.
Really?

Sebastien, did you try to just simply open /dev/dsk/c0t2d0s0
and read from it? (You need to be running as root, of course!)
 
N

Nigel Wade

Sébastien de Mapias said:
Hi,
Let's I have (under Solaris for example) a device /dev/[r]dsk/c0t2d0s0
that's not mounted (so I can't performe any 'ls' etc.), and I'd like
to
traverse its bytes: is there a way to access this data from Java ?
Has anyone already tried to write such a program ?

Have /you/ tried to write such a program?

In UNIX a device special file is pretty much like any other file. That's one of
the simplicities of UNIX. The device driver for that device should provide the
basic open/close/read/write functions to the kernel, so that higher level
languages can perform those operations. I presume Java only makes uses of those
native methods to access a file.

Try writing a program to read from it just like you would any binary file. Of
course, you most likely need root permission to open a block special file even
for read because you are bypassing all the filesystem level permissions on
contents of that device. And you need to be able to make sense of what you
read...
 
M

Mike Baranczak

Andreas Leitgeb said:
Mike Baranczak said:
Let's I have (under Solaris for example) a device /dev/[r]dsk/c0t2d0s0
that's not mounted (so I can't performe any 'ls' etc.), and I'd like
to traverse its bytes: is there a way to access this data from Java ?
Short answer: no.
Really?

Sebastien, did you try to just simply open /dev/dsk/c0t2d0s0
and read from it? (You need to be running as root, of course!)

OK, that does make more sense.

-MB
 
R

Roedy Green

traverse its bytes: is there a way to access this data from Java ?
Has anyone already tried to write such a program ?

You need to write some low level accessing code in C with JNI glue..
With every passing year this gets harder and harder to write as
security in OSes tightens.

Basically all you need is a read( offset, length ) and write(offset,
length).

Keep in mind there will be many parts of the disk the OS will not let
you touch, e.g. the paging file or the bootsector. To do this sort of
thing properly, you need to use the Boot-It approach where you boot to
a very simple OS without security from CD. Then you can do anything
you want without OS interference.

see http://mindprod.com/bgloss/bootit.html
http://mindprod.com/jgloss/jni.html
 
G

Gordon Beaton

You need to write some low level accessing code in C with JNI glue..

No you don't, you can use e.g. FileInputStream or RandomAccessFile.

On Unix, those devices look and behave just like files to any process.

/gordon

--
 
R

Roedy Green

No you don't, you can use e.g. FileInputStream or RandomAccessFile.

what's to stop you from reading other people's files? You must need
some special privilege to use this access, no?
 
G

Gordon Beaton

what's to stop you from reading other people's files? You must need
some special privilege to use this access, no?

Yes, you need appropriate permissions, and that's true no matter what
special device or regular file you choose to open (they are
conceptually the same thing).

But the permissions have nothing whatsoever to do with the technique
you choose; the two issues are orthogonal.

/gordon

--
 
N

Nigel Wade

Roedy said:
You need to write some low level accessing code in C with JNI glue..

You shouldn't need to with Solaris.
With every passing year this gets harder and harder to write as
security in OSes tightens.

The only security on a device special file in Solaris is the basic filesystem
permissions on the device special file itself. If you have read permission on
that file you can open it and read the contents.
Basically all you need is a read( offset, length ) and write(offset,
length).

Keep in mind there will be many parts of the disk the OS will not let
you touch, e.g. the paging file or the bootsector.

Not in Solaris. A device special file provides you with read (and/or write)
access to the raw disk/partition. If you read direct from the device special
file Solaris makes no interpretation of the contents of that raw
disk/partition, it is simply a stream of bytes just as any other binary file.
To do this sort of
thing properly, you need to use the Boot-It approach where you boot to
a very simple OS without security from CD. Then you can do anything
you want without OS interference.

Alternatively, just use the facilities provided by the OS you already have
installed...
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Roedy said:
what's to stop you from reading other people's files? You must need
some special privilege to use this access, no?

I don't think the term "other people's files" make much sense
in the context of a raw device.

"other peoples bytes" or "other peoples blocks" maybe.

:)

Typical you have stuff like databases in raw partitions. And
then the database and often also an application provides a
different type of security.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top