Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Can not detach loop device after mounting --rbind sys in it
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
poxu
n00b
n00b


Joined: 19 May 2017
Posts: 25

PostPosted: Wed Oct 02, 2024 11:15 pm    Post subject: Can not detach loop device after mounting --rbind sys in it Reply with quote

Hello!

I am trying to compile Gentoo in a directory, mounted to a loop device.
The idea is that I have an image, which I could write directly to an ssd and then reboot.

To do that I create a file, then make it a loop device with losetup and make home, boot and root partitions there

Then I mount root partition to a local directory and do what handbook tells me to do

But after I unmount that directory, loop device still exists and I can not detach it.
That only happens if I mount --rbind sys or dev directory

I have made a minimal example, which reproduces the problem. It doesn't even have gentoo files in it )) And no partition table, just a file with a file system inside


Code:

#!/bin/sh
ROOT_FS=root_fs_dir

# create 2 megabytes img file
dd if=/dev/zero of=drive.img  bs=2M  count=1

DEVICE=drive.img

#uncomment this to use losetup
#DEVICE=$(losetup --find --show drive.img)

mkfs.ext4 "${DEVICE}"

mkdir -p "${ROOT_FS}"

mount -o loop "${DEVICE}" "${ROOT_FS}"

cd "${ROOT_FS}"

mkdir -p proc
mkdir -p sys
mkdir -p dev
mkdir -p run

mount --types proc /proc proc

# if next two line are uncommented, then loop device can not be detached
#mount --rbind /sys sys
#mount --make-rslave sys

# if next two line are uncommented, then loop device can not be detached
#mount --rbind /dev dev
#mount --make-rslave dev

mount --bind /run run
mount --make-slave run
cd ..


sleep 2s
umount -R "${ROOT_FS}"

echo "attached loop device, here it is"
losetup --list | grep img

echo
echo "trying to detach loop device. "
echo "If next line is 'finished trying to detach loop device', then device is detached"

#uncomment this to use losetup
#losetup --detach "${DEVICE}"

losetup --list | grep img
echo "finished trying to detach loop device"

echo
echo "checking if device still exists. "
echo "If it does not, next line should be 'finished cheking if device still exists"
lsblk | grep "${DEVICE}"
echo "finished checking if device still exists"


Could someone advise me how to detach the loop device? Or understand what blocks it from detaching. What do I do wrong?
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1251
Location: Richmond Hill, Canada

PostPosted: Thu Oct 03, 2024 12:34 am    Post subject: Reply with quote

poxu,

because "--rbind" the "r" stand for recursive meaning it will descending in to sub-directories for mount-point and create those mount-point and mount the sub-mount. So when unmounting you also need to perform recursive unmount.

I see that you do use 'umount -R ..." that is correct way of doing it. However you did not check if the "umount -R ..." actually return successful finish. So may be the "umount -R ..." failed for some reason.

I also recommend you do
Code:
cd ${ROOT_FS}
umount -R dev
umount -R sys
cd ..
umount -R ${ROOT_FS}
Better augmented with check return status for each umount command.
My recommendation is because that is how I always do and usually it always work.
Back to top
View user's profile Send private message
poxu
n00b
n00b


Joined: 19 May 2017
Posts: 25

PostPosted: Thu Oct 03, 2024 2:25 pm    Post subject: Reply with quote

Quote:
I see that you do use 'umount -R ..." that is correct way of doing it. However you did not check if the "umount -R ..." actually return successful finish. So may be the "umount -R ..." failed for some reason.


Directory is unmounted successfully, the problem is that loop device is not detached. It stays present with auto clear flag on
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1251
Location: Richmond Hill, Canada

PostPosted: Thu Oct 03, 2024 8:29 pm    Post subject: Reply with quote

After some research I think the only way to clear it is reboot.

As far as I know it is something have to do with the --make-rslave that cause it. This could be a bug in kernel.
Back to top
View user's profile Send private message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3439

PostPosted: Thu Oct 03, 2024 9:17 pm    Post subject: Reply with quote

If that's the case, do you really have to use --rbind?
You only need /proc /sys /dev and /dev/pts to have all the important interfaces inside your chroot.

Well, there's also efivars, but that one is seldom used and it's just a convenience feature anyway; you can still reboot into bios and set those things there instead.
_________________
Make Computing Fun Again
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 22687

PostPosted: Thu Oct 03, 2024 9:30 pm    Post subject: Reply with quote

poxu wrote:
The idea is that I have an image, which I could write directly to an ssd and then reboot.
That suggests to me you intend to use dd or similar to copy to the solid state device. Don't do that. Copy the files to the SSD as files, so that you do not need to copy the free space.

Rather than rebooting, why not use a mount namespace to contain the mounts, and then discard it when you are done?

What is the output of cat -n /proc/self/mountinfo both before and after the umount -R? Does it work better if you use umount -l?
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