9

I want store some data on the disk in linux. I want this data stored in continuous disk block in physical disk. If i in order write this data to a normal file, maybe the block that the file occupies is not continuous in physical disk. Is there any way to do this job?

1
  • 3
    Why? What you trying to achieve? Commented Apr 9, 2012 at 10:51

4 Answers 4

9

Disk partitions are continuous regions of a disk.

So one way to do what you want to do is to resize your disk partitions and create a new one with gparted (gnome) or partitionmanager (kde) or similiar - of an appropriate size for your file.

You can then write directly to your new partition (not using and bypassing a filesystem) by using the file:

/dev/sdxn 

Where sdxn = {sda1, sda2, ..., sdb1, ... ...} etc. is the letter/number of the partition.

Alternatively you can set aside an entire disk by writing directly to it (bypassing partition table alltogether) using the file:

/dev/sdx 

Where sdx = {sda, sdb, sdc, ...} etc. is the letter of the disk.

Warning: Don't make a typo and write to the wrong one (that has a filesystem on it) or you will corrupt it. Best to make a symbolic link ln -s /dev/sdxn /home/fred/mydata, and then always write to mydata file.

Sign up to request clarification or add additional context in comments.

Comments

4

The filesystem code (inside the kernel, e.g. in linux-3.1.6/fs/ext4/ for ext4 file systems inside the linux-3.1.6 kernel source) is managing the disk blocks used for a given file. So you cannot organize the disk blocks of some of your files by yourself. However, you might give some hints to the kernel using some weird system calls.

If you don't like that, you could avoid the filesystem all together by writing directly to an unmounted partition, e.g. by doing write(2) syscalls to a file descriptor obtained by open(2)-ing for example /dev/sda2; but unless you really know what you are doing (and the formulation of your question makes me feel you don't understand the exact role of file systems), I won't recommend doing that.

Kernel file system code is quite good, and kernel file system cache is very efficient.

If you want to speed-up your reads, consider perhaps using readahead(2) or fadvise(2) or madvise(2) system calls.

You could also, when creating your filesystem, tune it for your particular purposes. For instance, if you know you'll have mostly quite big files in it, you could use a bigger than standard block size (e.g. mke2fs -b 8192), etc...

But don't think that software tricks would speed-up significantly your application; if you do a lot of disk IO, the real bottleneck is the hardware (so using SSD instead of hard disks might be easier).

1 Comment

It turns out that EXT4 has a maximum extent size of 128MiB, so files larger than this can't be contiguous on EXT4. These hints you mention – are there any such “weird” system calls to hint to the kernel on write? Or maybe to allocate a contiguous file of zeros which can then be written to. I'm looking to do this on FAT32 for LiveCD ISO files (usually ~700MiB), and preferably also on a *nix filesystem.
4

You cannot ask that to a regular file system.

If for some reason, you really want to store contiguous data on disk, you need to use a (free) raw device and manage yourself the data layout on it. This is what some databases are doing. Note that even in that case, there is no guarantee for the blocks to be contiguous. The device might be provided by a hardware or software RAID layer or be a zvol from a ZFS pool.

Comments

0

Try using rsync --preallocate:

rsync --preallocate /path/to/source/file /path/to/destination/

rsync will preallocate a contiguous block of storage and copy the file into it.

Just make sure the file is not already at the destination, or rsync won't re-allocate and re-copy it. If it is, delete it, empty trash to make sure it's really gone, then run this command.

Verify it copied contiguously:

filefrag /path/to/destination/file

"1 extent found" means it's contiguous, more than one means it's fragmented.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.