1

I have mounted an ext4 file system on Dir directory and tweaked some code of directory read which requires having files in the directory inodes with non-sequential inode numbers for its testing. As I am creating files with the shell script, which allocates inodes to the files with sequential inode numbers. Because for the files being created at the same time, inodes are allocated sequentially from the inode freelist, which generally have inode numbers sequentially. I have used following shell script to create files in Dir,

#! /bin/bash
for n in {1..1000}; do
    dd if=/dev/urandom of=file$( printf %03d "$n" ).bin bs=1 count=$(( RANDOM + 1024 ))
done

ls -i Dir gives following o/p

567 file001.bin  
568 file002.bin
569 file002.bin  
570 file004.bin  
571 file005.bin  
572 file006.bin 
573 file007.bin  
574 file008.bin
575 file009.bin
576 file010.bin
..

How can I make these files have nonsequential inodes?

3

Well a straightforward approach would be to just create a bunch of temporary files before an each .bin file:

function randomFiles() {
    for (( i=1; i<=$[$RANDOM%$1+1]; i++ ))
    do
            mktemp -q --tmpdir=.
    done
}

for n in {1..1000}; do
    dd if=/dev/urandom of=file$( printf %03d "$n" ).bin bs=1 count=$(( RANDOM + 1024 ))
    randomFiles 10
done

rm -f tmp.*

This will create 1 to 10 temporary files after an each .bin file, shifting the next inode number forward.

  • But would it be a good approach? Because for a single valid file we are creating 10 temporary files extra, that means if I want to create 1 million files, I have to create almost 10 million temporary files. Isn't there any way to have inode number what we want at file creation itself? – mahesh bhosale Mar 31 '17 at 12:11
  • >if I want to create 1 million files, I have to create almost 10 million temporary files If you wish you can probably move rm -f tmp inside the loop, to drop them on each iteration, so that you will have 10 temporary files at most at any given point of time (if you really want to generate a million files instead of just 1000). >But would it be a good approach? Well, that is a straighforward approach :) Should do it for the 'testing' purposes. Creating and then removing these temporary files will slow down your script by ~ 3-5% (as it already does a lot of IO). – zeppelin Mar 31 '17 at 13:03
  • Also it all depends on your definition of ` non-sequential `, if you consider skipping just one inode number to be enough (i.e. 1->3->5..), you can limit this to one just one temporary file per iteration. – zeppelin Mar 31 '17 at 13:07
  • > If you wish you can probably move rm -f tmp inside the loop, to drop them on each iteration. But after removing the files, there would be a probability of reusing removed inodes, hence might be a possibility to have sequential inodes. – mahesh bhosale Apr 1 '17 at 10:06
  • not if you first create the temporary files, then your data file, then remove the temporary files – zeppelin Apr 1 '17 at 22:18
1

Just randomize the order you create them in:

#! /bin/bash
for n in $(seq 1 1000 | sort -R); do
    dd if=/dev/urandom of=file$( printf %03d "$n" ).bin bs=1 count=$(( RANDOM + 1024 ))
done
  • Inode numbers will still be in the "file creating order" (i.e. if you sort them by the timestamp). Not sure on if this is what OP really wanted. – zeppelin Mar 31 '17 at 13:03
  • @zeppelin The OP doesn't mention anything about sorting by time, and the example given sorts by filename, which this solution solves. – Patrick Mar 31 '17 at 13:27
  • hmm, I see, maybe I'm taking it too broad indeed. – zeppelin Mar 31 '17 at 13:42
0

Create a test filesystem image once and for all. Use a filesystem debugger if necessary to get the inode numbers you want. To perform the test, make a copy of the image and mount it.

You can use a FUSE filesystem to mount a filesystem image without needing root permissions. There's no FUSE implementation of ext4, but there's one for ext2, one for zfs, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.