Solved

How to get hard disk sector size in Linux?

Posted on 2004-09-01
3
7,324 Views
Last Modified: 2012-08-13
Hello!

I would like to write to disk in atomic way to support transactions. To achive this, I have to write to disk in portions equal to the size of disk sector size. The problem is that it is srangely difficult to obtain the disk sector size in Linux. In Windows I use GetDiskFreeSpace call which returns me "bytes per sector" value. As I understand, for modern hard drivers (ATA and may be SCSI, I'm not sure) disk sector size is equal to 512bytes... But anyway, I'd like to be sure and ask Linux/device/controller/whatevery-you-want, but I don't know how to do it

Thanks for attention to my question,
Foma
0
Comment
Question by:Foma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 40

Assisted Solution

by:jlevie
jlevie earned 100 total points
ID: 11955777
You can work out what it is from what's returned by an HDIO_GET_IDENTITY ioctl(). See /usr/include/linux/hdreg.h.

Are you going to be writing directly to the disk (no file system)? It seems to me that's the only time that the sector size would be meaningful.
0
 
LVL 5

Accepted Solution

by:
mtmike earned 250 total points
ID: 11958278
You can also use the BLKSSZGET ioctl which works on any block device. It returns the hardware sector  size. Available in kernel 2.4 and 2.6 (linux/fs.h).

Note that writing single sectors does not really guarantee atomicity. The disk driver is still free to reorder writes. You will have to write synchronously either by passing the O_SYNC flag to open() or by using fsync().

If you are writing to a file system (instead of directly to disk) then logical file blocks may not correspond to physical disk blocks.
0
 

Author Comment

by:Foma
ID: 11971523
Thank you jlevie, thank you mtmike for responses.

BLKSSZGET ioctl fully fits my requirements. I’ve written a test program and it works fine. HDIO_GET_IDENTITY ioctl gives lots of information and can be useful too.

Concerning my task. I’m writing to disk using file system, but it doesn’t make sense because using portions of data equal to sector size, which are properly aligned I get atomicity of writes (with the one exception noted below). And of course, I use open with O_SYNC flag.

>Note that writing single sectors does not really guarantee atomicity. The disk driver is still free to reorder writes
Yes, modern hard disks are tooooo ‘smart’. As far as I know, this is the reason why SQL Server 7.0, for example, cannot _guarantee_ recovery in all cases on standard hardware. And we have to live with it… For critical tasks we have to use hard disks, which are capable of writing to disk everything they have in buffers in case of power off. And use RAID to save our data in case of disk breakage.
0

Featured Post

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question