-1

I'm trying to write a free space zeroizer using open(2), lssek[64](2), and write(2). I am trying to determine of a disk sector is in use.

I have took a look at dd(1) from coreutils, but the utility is not performing similar checks. In the Windows world, I could call DeviceIoControl with FSCTL_GET_VOLUME_BITMAP.

Given a raw sector, is there a system call which allows me to determine if the sector is in use?

jww
  • 97,681
  • 90
  • 411
  • 885

1 Answers1

3

The traditional way this is accomplished (since your method is subject to filesystem-corrupting race conditions), is to create a giant file, zero it, then delete the file. In fact, you can do this with sh directly, no need for a file:

# dd will run until disk space is exhausted
dd if=/dev/zero of=__somefile.bin bs=1M conv=noerror; rm __somefile.bin
Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • 'subject to filesystem-corrupting race conditions'. Agreed - I have not looked into locking yet. I thought a good first step was to determine if it was in use. – jww May 11 '11 at 01:06
  • 2
    You can't lock a sector in either Windows or Linux - if you were operating on an offline disk you could do something like this, but there is simply no safe way to write to a mounted volume. I don't mean to be harsh, but it is very easy to implement something that appears to work, but will silently eat data and corrupt volumes and it will be very hard to catch – Ana Betts May 11 '11 at 01:12