View previous topic :: View next topic |
Author |
Message |
poxu n00b
Joined: 19 May 2017 Posts: 25
|
Posted: Wed Oct 02, 2024 11:15 pm Post subject: Can not detach loop device after mounting --rbind sys in it |
|
|
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 |
|
|
pingtoo Veteran
Joined: 10 Sep 2021 Posts: 1252 Location: Richmond Hill, Canada
|
Posted: Thu Oct 03, 2024 12:34 am Post subject: |
|
|
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 |
|
|
poxu n00b
Joined: 19 May 2017 Posts: 25
|
Posted: Thu Oct 03, 2024 2:25 pm Post subject: |
|
|
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 |
|
|
pingtoo Veteran
Joined: 10 Sep 2021 Posts: 1252 Location: Richmond Hill, Canada
|
Posted: Thu Oct 03, 2024 8:29 pm Post subject: |
|
|
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 |
|
|
szatox Advocate
Joined: 27 Aug 2013 Posts: 3442
|
Posted: Thu Oct 03, 2024 9:17 pm Post subject: |
|
|
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 |
|
|
Hu Administrator
Joined: 06 Mar 2007 Posts: 22693
|
Posted: Thu Oct 03, 2024 9:30 pm Post subject: |
|
|
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 |
|
|
|