1

I accidentally wrote to a delete file through the loop device.

OS: Ubuntu 20.04

Here is my scenario:

1 - I created a RAW image file:

# qemu-img create -f raw sda.img 10G

2 - I created partiton with sfdisk dump:

# sfdisk sda.img < sda.dump

3 - I mounted the image through loop device (loop18):

# losetup -f --show -P sda.img

I get this:

# lsblk /dev/loop18
NAME       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop18       7:18   0   10G  0 loop 
└─loop18p1 259:0    0  9,8G  0 part 

4 - I deleted the image file (accidentally):

# rm sda.img

5 - I copied the backup image to the partition:

# dd if=sda1.img.bak of=/dev/loop18p1 status=progress

There was no error. The dd command worked fine.

The losetup command indicates the image file is deleted:

# losetup /dev/loop18
/dev/loop18: [2049]:1972579 ( (deleted))

I wrote randomly data to the partition for test (ctrl+C):

# dd if=/dev/urandom of=/dev/loop18p1 status=progress 
146953728 octets (147 MB, 140 MiB) copiés, 4 s, 36,7 MB/s^C
327621+0 enregistrements lus
327621+0 enregistrements écrits
167741952 octets (168 MB, 160 MiB) copiés, 5,10425 s, 32,9 MB/s

Why did it work ? Is it safe for my ext4 file system of my Ubuntu OS ?

Thanks.

Laura
  • 11
  • Files are only permanently deleted when their name is removed (all hard links) and the file is not open anywhere. Usually it would be a process that might keep a file open, but I guess the loop driver could, too. – ilkkachu Mar 04 '21 at 16:28
  • 2
    Related: the section on deleted files here: https://unix.stackexchange.com/a/120312/170373 – ilkkachu Mar 04 '21 at 16:41
  • Ow thank you very much :) – Laura Mar 08 '21 at 20:04

0 Answers0