is there anything wrong with this unusual use of loop device?

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
User avatar
BarryK
Posts: 2692
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 132 times
Been thanked: 738 times

is there anything wrong with this unusual use of loop device?

Post by BarryK »

I want to mount a file, with ext4 filesystem, in a very unusual way. It works, but I wonder if I am breaking some rule.

I have a file, testdummy.ext4, in folder testdummy. I created a ext4 filesystem in it:

Code: Select all

# losetup /dev/loop2 testdummy/testdummy.ext4
# mke2fs -t ext4 -L "EASYSAVE" -m 0 -b 4096 -O encrypt /dev/loop2
# sync
# losetup -d /dev/loop2

To access the ext4 filesystem, I mounted it:

Code: Select all

# losetup /dev/loop2 testdummy/testdummy.ext4
# mount -t ext4 -o discard /dev/loop2 testdummy

So now the file testdummy.ext4 has disappeared:

Code: Select all

# ls testdummy
lost+found
# 

It works, I can read and write files.

Although I cannot see testdummy.ext4 anymore, /dev/loop2 is still connected to it, so it works. I suppose that /dev/loop2 is connected to the inode of testdummy.ext4, not the path, which is why it continues to work.

If I unmount /dev/loop2, I can once again see testdummy/testdummy.ext4

I am just wondering if I am doing something that works "accidentally" and is not really supposed to be done?

Ignore those "-O encrypt" and "-o discard", that is not relevant.

User avatar
Jafadmin
Posts: 385
Joined: Tue Aug 04, 2020 4:51 pm
Has thanked: 68 times
Been thanked: 85 times

Re: is there anything wrong with this unusual use of loop device?

Post by Jafadmin »

Too many "testdummies" I think.

I would try testdummy-a, testdummy-b, etc .., and see what happens.

scsijon
Posts: 202
Joined: Fri Jul 24, 2020 10:11 am
Has thanked: 6 times
Been thanked: 18 times

Re: is there anything wrong with this unusual use of loop device?

Post by scsijon »

no, as far as i've always understood it, that's the way it should work. With only one appearance of the directory structure available at any time, whether mounted in a /dev/loop or unmounted as a file.

Clarity
Posts: 3829
Joined: Fri Jul 24, 2020 10:59 pm
Has thanked: 1628 times
Been thanked: 523 times

Re: is there anything wrong with this unusual use of loop device?

Post by Clarity »

Hello @BarryK

I dont see anything wrong.

Question
Would 'fallocate ...' be a simpler options to achieving your result(s) desired ... and a little easier in its understanding? (A single command affording what you want.)

Hope this is helpful.
P.S. Barry or anyone: You may find this forum link also useful for image-iso loop mounts.

jamesbond
Posts: 717
Joined: Tue Aug 11, 2020 3:02 pm
Location: The Pale Blue Dot
Has thanked: 124 times
Been thanked: 402 times

Re: is there anything wrong with this unusual use of loop device?

Post by jamesbond »

BarryK wrote: Mon May 24, 2021 10:04 am

I am just wondering if I am doing something that works "accidentally" and is not really supposed to be done?

This works and is a standard/supported feature. It is called as an "overmount".

If you want to make use of this feature, don't worry and just go and use it. I've been using it for years with "encfs" (with some sort of ROX GUI). An encfs-encrypted directory contains garbage when viewed. I can "open" it (which behind the scenes run encfs mount over the same directory), and then that directory now shows proper unencrypted files. When I'm done, I can "close" it (which behind the scenes I just run "umount") and the directory turns back to showing garbage.

In fact, this happens every time the system boots up, if you use initramfs. It is how "switch_root" works. Nothing is being switched, really, what really happens is that the new root (could be aufs' "new_root", or one of the disk partitions) is "overmounted" on root directory ("/").

So go ahead and use it. It's a supported feature.

amigo
Posts: 56
Joined: Wed Nov 03, 2021 8:06 pm
Location: Germany
Has thanked: 1 time
Been thanked: 4 times

Re: is there anything wrong with this unusual use of loop device?

Post by amigo »

I used to use this in KISS linux as a way to have certain directories read-only. For instance, for /usr/bin, I'd create a filesystem image .iso, ext2 or whatever, place that in a read-write framework or as part of normal boot. I'd put the image under /usr/bin, then during bootup, look for such images(s) and mount them during early boot. I experimented with using a compressed ext2-comp image. That way I could save quite a lot of space in the overall image.
It's a clever, sneaky way to hide things in plain sight.

BTW, hello to all you guys -I've finally signed up here after all the murga-linux forum end-of-line. In the meantime, I've aged a bit, and probably won't be so cantankerous, as some might remember from me.

User avatar
Keef
Posts: 277
Joined: Tue Dec 03, 2019 8:05 pm
Has thanked: 3 times
Been thanked: 75 times

Re: is there anything wrong with this unusual use of loop device?

Post by Keef »

Hello again amigo. I was wandering what you were up to. Cantankerous? Nah, that couldn't be you.

amigo
Posts: 56
Joined: Wed Nov 03, 2021 8:06 pm
Location: Germany
Has thanked: 1 time
Been thanked: 4 times

Re: is there anything wrong with this unusual use of loop device?

Post by amigo »

Hi! I had kinda retired from programming after 10 years on the same projects. For years I had mostly coded during the cold months. But, when the lockdowns came in 2020, I re-visited a question from years earlier -would it be possible to use, or at least demonstrate neural networks from a shell script. Happy to say that it can, indeed, be done. I'll probably posting soon with a call for testers/users of some pretty interesting math stuff.

User avatar
BarryK
Posts: 2692
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 132 times
Been thanked: 738 times

Re: is there anything wrong with this unusual use of loop device?

Post by BarryK »

Welcome back, amigo!

amigo
Posts: 56
Joined: Wed Nov 03, 2021 8:06 pm
Location: Germany
Has thanked: 1 time
Been thanked: 4 times

Re: is there anything wrong with this unusual use of loop device?

Post by amigo »

Thanks Barry. Bit of rain out there lately, I heard.

User avatar
snoring_cat
Posts: 206
Joined: Tue Sep 21, 2021 3:40 pm
Location: Earth
Has thanked: 24 times
Been thanked: 46 times

Re: is there anything wrong with this unusual use of loop device?

Post by snoring_cat »

Barry,

would the following work for you

Code: Select all

dd if=/dev/zero of=testdummy.ext4 bs=1M count=1 seek=100
mkfs -t ext4 -L "EASYSAVE"  testdummy.ext4

mkdir /mnt/easysave
mount testdummy.ext4 /mnt/easysave

umount /mnt/easysave

This should quickly create a 100M testdummy.ext4 file with ext4 on it. Also note that when mounting, you do not have to explicitly use -t ext4.

Pease feel free to tweak any of the example values as you see fit. Other than encryption, I want to throw out that you could use a dynamically growing filesystem similar to qcow2 (Please refer to libqcow-utils package and "qcowmount" command).

Meeeooow!

-- substance over noise, since 5 minutes in the future --

Post Reply

Return to “Programming”