LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-24-2016, 09:10 AM   #1
majest
LQ Newbie
 
Registered: Oct 2015
Posts: 6

Rep: Reputation: Disabled
fallocate - does it fill the space with zeros?


When I read the man page for fallocate it is a little unclear (to me) whether it creates a file that to all intents and purposes is filled with zeros.

Quote:
NAME
fallocate - preallocate space to a file

SYNOPSIS
fallocate [-n] [-o offset] -l length filename

DESCRIPTION
fallocate is used to preallocate blocks to a file. For filesystems which support the fallocate sys‐
tem call, this is done quickly by allocating blocks and marking them as uninitialized, requiring no
IO to the data blocks. This is much faster than creating a file by filling it with zeros.
I've run a few tests allocating disk space and reading it to see if there are any remnants of previous unwiped disk contents and did not see anything. But I'd really like to be sure!

fallocate -l 10M myfile.bin
xxd -a myfile.bin

0000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................
*
09ffff0: 0000 0000 0000 0000 0000 0000 0000 0000 ................

My understanding is that fallocate allocates space on the disk but mark the blocks as uninitialized, which the operating system can interpret as 0 without having to fetch the values. It is only after you write to a block that anything actually gets put on the disk.

I noticed another utility called "truncate" does almost exactly the same thing but the difference is it creates a sparse file which only allocates blocks when they are written, so they may end up not being contiguous on the disk.

If my understanding is correct, it seems both should be a safe alternative to initializing the file with zeros (e.g. using fwrite from C or something like that). I'd appreciate any feedback on this interpretation of fallocate and truncate - thanks.

Last edited by majest; 04-24-2016 at 02:36 PM.
 
Old 04-24-2016, 10:37 AM   #2
af7567
Member
 
Registered: Nov 2012
Posts: 156

Rep: Reputation: 54
While the filesystem can return zero for uninitialised blocks any data on the physical media would still be there. So, if those blocks were read directly instead of going through the filesystem then the data on them could still be read.
 
Old 04-24-2016, 02:33 PM   #3
majest
LQ Newbie
 
Registered: Oct 2015
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by af7567 View Post
While the filesystem can return zero for uninitialised blocks any data on the physical media would still be there. So, if those blocks were read directly instead of going through the filesystem then the data on them could still be read.
OK that's good to know. Just out of interest, how would you go about accessing the disk without going through the filesystem? Would using C code and fread be an example of that or are we talking deeper?
 
Old 04-24-2016, 03:35 PM   #4
af7567
Member
 
Registered: Nov 2012
Posts: 156

Rep: Reputation: 54
I have never tried anything like that, but if you know the block number and the block size of the device you could probably use the dd command to read from the raw hard disk (eg /dev/sda) and output the blocks to a new file.
 
Old 04-24-2016, 05:13 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,962

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Essentially any command that operates on a file can also operate on a disk drive or partition provided you have the necessary permissions. Running something as rudimentary as "cat /dev/sda" will dutifully copy the entire content of that disk drive to stdout, with the binary content really messing up your terminal display in the process. The tool most often used to read or write sections of a disk drive is dd because some of its options are suitable to the task. You can also use tools like strings, grep, or hexedit to peruse the contents of a drive or partition. Editors that try to read the entire content of the source into memory are not suitable, however.
 
Old 04-25-2016, 04:17 PM   #6
majest
LQ Newbie
 
Registered: Oct 2015
Posts: 6

Original Poster
Rep: Reputation: Disabled
OK it's a little bit clearer.

In short, if you are interacting with a file on the disk (which fopen does) then it will use the filesystem, and hence you will see zero initialized data because of the "uninitialized" tag of the blocks.

However if you are looking at the disk independently of any files, then you may see non-zeros in the places where the supposedly all-zero files are stored.

Basically, if you are dealing with filenames then to all intents and purposes fallocate is writing zeros, even though it is not actually physically zeroing the space.

Last edited by majest; 04-25-2016 at 04:21 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I fill remaining space with zeros on DVD Sed_Awk Linux - Newbie 2 12-13-2011 12:37 PM
[SOLVED] Increase home to fill free space gap after resizing windows tradet Linux - General 3 08-10-2010 04:48 PM
[SOLVED] deleted file still fill space on the HD! Dr_Death_UAE Linux - General 16 06-17-2010 01:01 AM
bash/sed/awk fill each line in text file with space to fixed length khairil Programming 11 01-09-2008 06:28 AM
MBR was filled with zeros n123 Linux - Software 2 08-28-2007 03:26 PM


All times are GMT -5. The time now is 02:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration