Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
What is the loop device in the installation iso image?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Installing Gentoo
View previous topic :: View next topic  
Author Message
marclop
n00b
n00b


Joined: 04 Apr 2020
Posts: 3

PostPosted: Sat Apr 04, 2020 12:08 pm    Post subject: What is the loop device in the installation iso image? Reply with quote

I'm doing a fresh install from a USB drive, and I noticed there is a loop device in the result of lsblk named loop0
Code:
# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0    7:0    0 314.1M  1 loop /mnt/livecd

I noticed there is an entire root directory under /mnt/livecd, like
Code:
# ls /mnt/livecd
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  sys  tmp  usr  var

It seems this directory is not mentioned in the handbook, and is not used during the installation? I want to know why there is such a loop device?

P.S. This is my first time gentoo try, and as you might already noticed, English is not my native language. Hope I made myself clear enough, thanks in advance! :wink:
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 18434

PostPosted: Sat Apr 04, 2020 5:38 pm    Post subject: Reply with quote

man loop wrote:
DESCRIPTION
The loop device is a block device that maps its data blocks not to a physical device such as a hard disk or optical disk drive, but to
the blocks of a regular file in a filesystem or to another block device. This can be useful for example to provide a block device for a
filesystem image stored in a file, so that it can be mounted with the mount(8) command. You could do

$ dd if=/dev/zero of=file.img bs=1MiB count=10
$ sudo losetup /dev/loop4 file.img
$ sudo mkfs -t ext4 /dev/loop4
$ sudo mkdir /myloopdev
$ sudo mount /dev/loop4 /myloopdev


See also: https://en.wikipedia.org/wiki/Loop_device
_________________
The media sells it and you live the role.
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 45383
Location: 56N 3W

PostPosted: Sat Apr 04, 2020 8:06 pm    Post subject: Reply with quote

marclop,

Welcome to Gentoo.

The liveCD/USB requires its own root filesystem. Its actually a whole root filesystem inside a file.
When you boot the liveCD/USB that file is mounted using a loopback mount as root.

Look at the files and directories on the USB drive when its not in use for installing Gentoo.

That answers the question you asked.

The file is compressed, I think its squashfs, to make it faster to download and more importantly, fit onto a CD.

I'm sure that raises more questions than it actually answers.
On to your next Gentoo learning opportunity. :)
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
marclop
n00b
n00b


Joined: 04 Apr 2020
Posts: 3

PostPosted: Sun Apr 05, 2020 4:42 am    Post subject: Reply with quote

Thanks for your reply!

Now I understand where this loop device come from: when my computer boot from usb drive, grub(or another program?) mount the image.squashfs to /mnt/livecd.

But I do have some new questions: What is the relation between / and /mnt/livecd? I guess / is based on /mnt/livecd and during the booting process, some program added some change to /mnt/livecd (like /etc/portage/make.conf) and made it the new /. And maybe that's why everytime I boot from usb drive, changes I made would lost. If I understand it right, which program(s) did these jobs?
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 45383
Location: 56N 3W

PostPosted: Sun Apr 05, 2020 9:47 am    Post subject: Reply with quote

marclop,

My explanation above was a bit simplistic.
The root filesystem is a file is both compressed and read only. Even though its on a USB stick, it can't be modified in place.
The file its in is a fixed size. There is no room to add files or even make an existing file bigger.
Files are usually updated by makind the new file under a temporary name, deleting the old file then renaming the new file.

Files on a block storage device always take an integer number of blocks. A block is usually 4kB on disk these days. Thus even a one byte file is allocated a whole 4kB block.
This leads to a lot of unusable space. On average, 2kB per file. With a squashfs filesystem in a file, this unusable space is not stored.

You are correct about the boot loader role.
It passes the root filesystem location to the kernel on the kernel command line, then the kernel performs the mount as it starts up.

I said above that the filesystem in a file is read only. That would not be very satisfactory for performing a Gentoo install. Its correct as far as it goes but it doesn't go far enough.
The make it appear writeable, an overlay filesystem is used. That's a part of RAM used to store changes to the filesystem in a file.
Think of the changes in RAM being a layer over the top of the read only filesystem.

When a file is written, the existing file is copied to RAM (this is called copy on write, CoW for short), and the changes are made in the normal way.
When the changed file is read, the RAM is checked first. I'll gloss over directories, which are just files too but the same thing happens.
As you say, because the changes are never saved to your USB stick, they are lost at reboot.
Keep in mind that all this is intended to work with read only media.

Its possible to add a persistence mode, so the changes can be saved across reboots but that feature is not on the minimal CD.
It involves saving the overlay somewhere that has read/write access and restoring during next boot.

Lots of things make use of the overlay filesystem. Logs are written there, look in /var/log/
Changes you make to /etc go there and many more.
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
marclop
n00b
n00b


Joined: 04 Apr 2020
Posts: 3

PostPosted: Sun Apr 05, 2020 11:59 am    Post subject: Reply with quote

Thanks for your comprehensive information! I have used other linux distributions for two years but never think about these questions, seems I need more fundamental OS knowledges :P
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Installing Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum