3

I have to test my program, that it completely removes files with their contents from disk. To test that I do the following:

  1. Create a file with some known string inside.
  2. My program deletes the file.
  3. I search the string on the disk where the file was located. To do that I make grep consider the disk as a pile of raw data:
$ grep -a -o -c 'some_string_to_be_found' /dev/sda1

The test actually works as expected (finds the string if I delete the file manually, and doesn't if my program deletes it).

The problem is that the disk size may be big, so it would require a huge amount of time to complete the test. Also, the label for the disk may vary on different machines.

Therefore I think about how to use a virtual filesystem instead. Create a disk in RAM, using tmpfs:

pc:/mnt$ mkdir tmpfs
pc:/mnt$ chmod 777 tmpfs/
pc:/mnt$ mount -t tmpfs -o size=50M tmpfs /mnt/tmpfs/

create/fill/delete a file, and then try to find its content, using something like:

$ grep -a -o -c 'some_string_to_be_found' /dev/tmpfs

The problem is that it results in

grep: /dev/tmpfs: No such file or directory

So the question is: is it possible to use tmpfs as a device, or read the raw memory allocated for the virtual filesystem?

Alexey
  • 1,198
  • 1
  • 15
  • 35
  • you can do this creating a symbolic link in "dev" before doing the grep ```ln -s /mnt/tmpfs /dev/tmpfs``` – ZiTAL Sep 27 '18 at 08:44
  • @ZiTAL, yep, I know. The point is to provide grep access to raw data (if tmpfs was a device), rather than to an ordinary filesystem (mounted in mnt). The filesystem doesn't contain the deleted file. But raw data still may contain its content. – Alexey Sep 27 '18 at 08:45
  • I don't think you can do this, the ram's raw data is not common dir that you can read a file list – ZiTAL Sep 27 '18 at 08:50
  • 1
    You can use a *"loop device"* for this... https://www.thegeekdiary.com/how-to-create-virtual-block-device-loop-device-filesystem-in-linux/ – Mark Setchell Sep 27 '18 at 08:57
  • If you want to be sure that all data is destroyed in production environment, then you should make the test by using same devices (/dev/sda1) and filesystems that are used in production environment. With grep it could be hard anyway, because fs might be encrypted or fragmented. – SKi Sep 27 '18 at 08:58
  • @MarkSetchell this is the working solution, thanks. – Alexey Sep 27 '18 at 09:37
  • Excellent! Go ahead and write up what you did as an answer for all to see and refer to, and you can also *"accept"* it as correct and grab the points. Good luck! – Mark Setchell Sep 27 '18 at 09:45
  • @MarkSetchell though you've provided the answer for my task, I doubt it would fit for the question itself, cuz it's about using tmpfs as a device. It seems I've asked a wrong thing... – Alexey Sep 27 '18 at 09:49

1 Answers1

1

Although I haven't found a way how to treat tmpfs as a device, there is a way for solving my taks described above (may be useful sor someone).

We will treat a file as a device. The algorithm is as follows:

  1. Create an empty file with specified size:

     # touch storage_file
     # truncate -s 10M storage_file
    
  2. Create a filesystem inside that file:

     # mkfs.ext4 storage_file
    
  3. Now we are able to create a device from that file and mount it as an ordinary disk:

     # losetup /dev/loop0 storage_file
     # mkdir /mnt/loopfs
     # mount -o loop /dev/loop0 /loopfs
    
  4. That's all. We're able to treat the file as a device. Create/delete arbitrary files/directories inside it. And grep through /dev/loop0 works as expected, w/o processing entire physical storage device, crawling inside the storage_file only.

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
Alexey
  • 1,198
  • 1
  • 15
  • 35