Google Answers Logo
View Question
 
Q: Linux SCSI disc programming ( No Answer,   1 Comment )
Question  
Subject: Linux SCSI disc programming
Category: Computers > Programming
Asked by: huayi-ga
List Price: $20.00
Posted: 30 Sep 2002 22:33 PDT
Expires: 30 Oct 2002 21:33 PST
Question ID: 71049
I am working on low-level SCSI I/O access under Linux system. Please
give me answers about:
1. How to Read/Write a "specific block" from SCSI disk, using C
language.
2. How to ***implement*** "FORMAT" command (used to format a disk )in
C language under Linux.

Request for Question Clarification by voyager-ga on 01 Oct 2002 02:24 PDT
Hi huayi,

just to be sure: are you talking about low-level or high-level
formatting? If you are talking about high-level, which filesystem?

voyager-ga

Clarification of Question by huayi-ga on 01 Oct 2002 08:16 PDT
Hi voyager,

What I was trying to say is high-level formatting.
The targeting filesystem is FAT and EXT2.

Thank you!

huayi
Answer  
There is no answer at this time.

Comments  
Subject: Re: Linux SCSI disc programming
From: pjrc-ga on 01 Oct 2002 09:06 PDT
 
To read or write directly to blocks on the disk, all you need to do is
open /dev/sda (or whatever the device name is for your disk) and then
use the normal "read", "write" and "lseek" calls to access it just
like a file.  Of course, when you call "open" to get the file
descriptor (an integer), be sure you specify O_RDWR.  Type "man 2
read", "man 2 lseek", etc for the documentation of each of these
standard system calls.  You could probably also use the fopen, fread,
etc calls, but you're probably better off keeping to the unbuffered
(at the C library level) calls.

Beware of calling lseek beyond the end of the disk and then accessing
it, particularly if the disk is a USB-based flash media card using the
SCSI emulation layer (sometimes linux 2.4.18 will mis-detect the disk
geometry and thus the total size after a hot-swap).

You could write lots of code to format the disk by writing directly
into the sectors, or you could just call one of the existing format
commands (mke2fs, mkdosfs, etc) using the "system" call.  Don't forget
to check the return value to see if the command was successful.  Type
"man 3 system" for details if you've not used the system call.  It's
really very simple.

If you write to the partition table, there is some ioctl you must call
(you'll have to look it up, since this is a free comment and not a
paid answer).  If you're intending to only access within an existing
partition, you can open /dev/sda1, /dev/sda2, etc and the kernel will
make offset 0 the beginning of that partition and prevent writes from
spilling onto the other partitions.

Also, everything I've said here works equally well for IDE disks
(/dev/hda, /dev/hdb, etc).

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy