How to save a file in a continuous disk block in linux

I want to save some data to disk on Linux. I want this data to be stored in a continuous drive on a physical drive. If I write this data to a regular file, perhaps the block that the file occupies is not continuous on the physical disk. Is there any way to make this work?

+6
source share
3 answers

Disk partitions are contiguous areas of the disk.

Thus, one way to do what you want to do is to resize the disk partitions and create a new one with gparted (gnome) or partition manager (kde) or similar - the appropriate size for your file.

Then you can directly write your new section (without using or bypassing the file system) using the file:

/dev/sdxn 

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

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

 /dev/sdx 

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

Warning: Do not make a typo or write the wrong file (on which there is a file system), or you will damage it. It is best to make a symbolic link ln -s / dev / sdxn / home / fred / mydata, and then always write to the mydata file.

+7
source

The file system code (inside the kernel, for example, in linux-3.1.6/fs/ext4/ for ext4 file systems inside the linux-3.1.6 kernel source) controls the disk blocks used for this file. Thus, you cannot independently organize the disk blocks of some of your files. However, you can give some clues to the kernel using some weird system calls.

If you do not like this, you can avoid file system collaboration by writing directly to the unmounted partition, for example. making write (2) syscalls for the file descriptor obtained by open (2) - for example, /dev/sda2 ; but if you really don’t know what you are doing (and the wording of your question makes me feel that you do not understand the exact role of file systems), I will not recommend it.

The kernel file system code is pretty good, and the kernel file system cache is very efficient.

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

You can also configure it for your specific purposes when creating your file system. For example, if you know that it will mainly contain large files, you can use the size of the standard block (for example, mke2fs -b 8192 ), etc.

But do not think that software tricks will significantly speed up your application; if you do a lot of disk I / O, the real bottleneck is the hardware (so using SSDs instead of hard drives can be easier).

+3
source

You cannot ask about this on a regular file system.

If for some reason you really want to store continuous data on disk, you need to use a (free) raw device and manage it on the data layout. This is what some databases do. Please note that even in this case there is no guarantee that the blocks will be adjacent. The device can be provided with a hardware or software RAID layer or be zvol from the ZFS pool.

+3
source

Source: https://habr.com/ru/post/912744/


All Articles