Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
mounting for chroot and then umount is impossible?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
LukynZ
Apprentice
Apprentice


Joined: 19 Dec 2008
Posts: 230
Location: The Czech Republic

PostPosted: Sun Apr 21, 2013 7:54 am    Post subject: mounting for chroot and then umount is impossible? Reply with quote

Hi,

I've made script for mounting and chrooting my Arch instalation on other partition for managing from gentoo, this is script

Code:

#!/bin/bash

mount /dev/sda3 /mnt/arch
cp /etc/resolv.conf /mnt/arch/etc
mount -t proc none /mnt/arch/proc
mount --rbind /dev /mnt/arch/dev
mount --rbind /sys /mnt/arch/sys
chroot /mnt/arch /bin/bash


so far so good, everything works, then when I want quit this chroot, I type exit and I'm back in my gentoo shell as root and then I want to umount all mounted points, again by script

Code:
#!/bin/bash

umount /mnt/arch/dev
umount /mnt/arch/sys
umount /mnt/arch/proc
umount /mnt/arch


but now there is a problem

Code:
umount: /mnt/arch/dev: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
umount: /mnt/arch/sys: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
umount: /mnt/arch: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))


Is it possible umount folders as dev and sys without restart?
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6747

PostPosted: Sun Apr 21, 2013 8:04 am    Post subject: Reply with quote

I guess that you can umount without problems if you do not chroot, but that it is not always reproducible.
I had similar problems with kernels about 2.6.35-3.3 (roughly, I did not remember precise versions).
The bug was then elminated and then it reappeared in recent kernels 3.6(?) til now.

However, in your case it may also be that the --rbind is the problem: Try --bind instead.
Back to top
View user's profile Send private message
The Doctor
Moderator
Moderator


Joined: 27 Jul 2010
Posts: 2678

PostPosted: Sun Apr 21, 2013 8:07 am    Post subject: Reply with quote

Try umount -l. Its how the handbook handles this.

man umount wrote:
-l, --lazy
Lazy unmount. Detach the filesystem from the filesystem hierar-
chy now, and cleanup all references to the filesystem as soon as
it is not busy anymore. (Requires kernel 2.4.11 or later.)

_________________
First things first, but not necessarily in that order.

Apologies if I take a while to respond. I'm currently working on the dematerialization circuit for my blue box.
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6747

PostPosted: Sun Apr 21, 2013 8:28 am    Post subject: Reply with quote

The Doctor wrote:
Try umount -l

For sys and dev filesystms this might work, but it is very dangerous to use this, since it does not mean that the partition is really freed by kernel: It only appears to be free, but at shutdown it might cause other partitions from not being umounted cleanly. I had many defect filesystems from using -l in some scripts.
But you are right, for sys and dev this might be necessary.
Back to top
View user's profile Send private message
LukynZ
Apprentice
Apprentice


Joined: 19 Dec 2008
Posts: 230
Location: The Czech Republic

PostPosted: Sun Apr 21, 2013 12:26 pm    Post subject: Reply with quote

Thank you for tips, I'll try them and report back :)
Back to top
View user's profile Send private message
krinn
Watchman
Watchman


Joined: 02 May 2003
Posts: 7470

PostPosted: Sun Apr 21, 2013 7:25 pm    Post subject: Reply with quote

mv wrote:

However, in your case it may also be that the --rbind is the problem: Try --bind instead.


I agree with mv, rbind issue there.
if --rbind /home you may have /home/thatuser mount too
and umount fail then because /home is use by /home/thatuser

--bind should solve your issue
Back to top
View user's profile Send private message
LukynZ
Apprentice
Apprentice


Joined: 19 Dec 2008
Posts: 230
Location: The Czech Republic

PostPosted: Sun Apr 21, 2013 9:36 pm    Post subject: Reply with quote

true, with --bind only, I am able to umount all folders :) thx
Back to top
View user's profile Send private message
ulenrich
Veteran
Veteran


Joined: 10 Oct 2010
Posts: 1480

PostPosted: Tue Apr 23, 2013 5:11 pm    Post subject: Reply with quote

You can additionally remount readonly /dev:
Code:
#!/bin/bash
#GPL2 - by  Ralph Ulrich
declare -a R
declare -a Q
# To your editing pleasure:
j=0
R[$j]="$1"
M[$j]="0"
j=1
R[$j]="/etc/resolv.conf"
M[$j]="0"
j=2
R[$j]="/proc"
M[$j]="-o ro,relatime -t proc none"
j=3
R[$j]="/sys"
M[$j]="-o ro,nosuid,nodev,noexec,relatime -t sysfs none"
j=4
R[$j]="/dev"
M[$j]="-o bind /dev"
j=5
R[$j]="/dev/pts"
M[$j]="-o rw,nosuid,noexec,relatime,gid=5,mode=620 -t devpts none"
j=6
R[$j]="/dev/shm"
M[$j]="-o rw,nosuid,nodev,relatime,mode=777,size=4% -t tmpfs none"

mntme() {
    [ -z $1 ] \
         && helpExit "Error calling internal mntme without index"
    u="${R[0]}${R[$1]}"
    [  "${M[$1]}" = "" ] \
         && helpExit "Error empty mount option"
    if [ ! -e "$u" ] ; then
   mkdir -p $u \
    || helpExit "Error mount point could not created:\n mkdir -p $u"
    fi
    mount ${M[$1]} $u \
         && echo " mount ${M[$1]} $u"\
         || helpExit "Error could not\n mount ${M[$1]} $u"
}

umountall() {
    local i
    P=${R[0]}
    for ((i=${#R[@]}-1 ; i>=0 ; i--)) ; do
        u=$P${R[$i]}
        if [[ $i = 0 ]] ; then
           [[ $b_wasmounted = 1 ]] && echo "$me: keep mount $u" && continue
           [[ $b_wasmounted = 0 ]] && umount $P
        elif [[ /etc/resolv.conf = ${R[$i]} ]] ; then
            [ -e $u.$me ] && rm $u && mv $u.$me $u
        else
       umount $u || echo " didn't work: umount $u"
        fi
    done
}

me=$(basename $0)
helpExit() {
    if [ "help" = "$1" ] ; then
   echo -e "Help:\n $me -L LABEL | -d Dir | -b /dev/sdXn | --help | --edit"
    else
   [[ ! -z $1   ]] && echo -e "\n$1"
   umountall
    fi
    exit
}

mountall() {
    for ((i=0 ; i < ${#R[@]} ; i++)) ; do
        P=${R[0]}
        t=${R[$i]}
        case $t in
       $P)
      [[ $b_wasmounted = 0 ]] || continue
      if [[ -b $P ]] ; then
                   ici="/dev/$(basename $(readlink -f $(mount |grep '/dev/' |grep ' / ' |head -n 1 |sed -e's@ .*@@')))"
                   [[ $ici = $P ]] \
                       && helpExit "Error same filesystem: $P"
                       #Todo:  doesn't work for systemd: /dev/rootfs
                  if grep -q "$P" /proc/mounts ; then
                     b_wasmounted=1
                  elif grep -q "$P" /etc/fstab ; then
                     mount "$P" \
                        || helpExit "Error could not mount $p"
                  else
                      u=/media/${P:5}
                      mkdir -p $u \
                          || helpExit "Error mkdir -p $u"
                      mount $P $u \
                          || helpExit "Error cannot mount $P"
                   fi
               else
                   helpExit "Error cannot chroot into non existing $P"
               fi
               R[0]=$(mount |grep $P |sed -e 's@ type .*@@' -e 's@.* @@')
       ;;
       /etc/resolv.conf)
      [ -e $P$t ] && mv $P$t $P$t.$me
          #Todo:or: mount -o bind $(readlink -f $t) $P$t
      touch "$P$t" ||  helpExit "cannot handle $P$t"
      cat "$t" >"$P$t"
       ;;
       /dev)
      mntme $i
      mount -o remount,ro $P$t
      echo "mount -o remount,ro $P$t"
       ;;
            *)
      mntme $i
            ;;
        esac
    done
}

[[ -z $1 ]] \
   && helpExit "help"
[ "root" != "$USER" -a "root" != "$LOGNAME" ] \
   && helpExit "exit: You need to be root!"
[ -n "$MYCHANGEROOT" ] \
   && helpExit "exit: You already are in a chroot: $MYCHANGEROOT"

b_wasmounted=0
# default is Label -L
[ "$1" = "-L" -o "$1" = "--label" ] && shift
case $1 in
    -h|--help)
   helpExit "help"
    ;;
    -e|--edit)
   exec /usr/bin/mcedit "$0"
   exit
    ;;
    -d|--directory)
   [[ -z $2 ]] \
      && helpExit "missing argument to option -d"
   [[ -d $2 ]] \
      || helpExit "Error not existing directory"
   [[ -L $2 ]] \
      && helpExit "Error not chroot into symlink"
   b_wasmounted=2
   R[0]="$2"
    ;;
    -b|--blockdevice)
   [[ -z $2 ]] \
      && helpExit "missing argument to option -b"
   [[ -b /dev/${2:5} ]] \
      || helpExit "Error not existing blockdevice"
   R[0]="$2"
    ;;
    *)
   [[ -z $1 ]] \
      && helpExit "missing argument"
   [ ! -L "/dev/disk/by-label/$1" ] \
      && helpExit "Error Label $1"
   R[0]=/dev/$(basename $(readlink /dev/disk/by-label/$1))
    ;;
esac

trap umountall SIGINT SIGKILL
mountall
echo  "$me ${R[0]}  start ------------"
MYCHANGEROOT=${R[0]} TMP=/tmp chroot ${R[0]} /bin/bash --login
umountall
echo  "$me ${R[0]}  end --------------"
Back to top
View user's profile Send private message
beli.sk
n00b
n00b


Joined: 25 Dec 2009
Posts: 11

PostPosted: Mon Dec 09, 2013 12:24 pm    Post subject: Reply with quote

This could be useful for a recursive unmount:
Code:
umount `grep /mnt/chroot /proc/mounts | cut -f2 -d" " | sort -r`

Note: the recursively mounted filesystems are not recorded to /etc/mtab but they are visible in /proc/mounts
_________________
Beli | independent IT consultant | public(a)beli.sk | www.beli.sk
Back to top
View user's profile Send private message
tipp98
Tux's lil' helper
Tux's lil' helper


Joined: 28 Jan 2007
Posts: 113

PostPosted: Wed Mar 26, 2014 10:31 pm    Post subject: Reply with quote

I'm having the same problem. I had been using rbind as a mount option, `-o rbind`, and everything worked fine. With a recent system update, that option seems to have went away and I was forced to switch to `--rbind`. That's when things broke and I started getting the following after exiting my chroot environment:

PTY allocation request failed on channel 0
Must be connected to a terminal.

Should a bug be reported somewhere?
Back to top
View user's profile Send private message
steveL
Watchman
Watchman


Joined: 13 Sep 2006
Posts: 5153
Location: The Peanut Gallery

PostPosted: Thu Mar 27, 2014 7:48 am    Post subject: Reply with quote

tipp98 wrote:
Should a bug be reported somewhere?

No. The problem is the recursive bind, which is also bind-mounting /dev/shm and /dev/pts. See ulenrich's (rather quaint;) script, and this post.
Back to top
View user's profile Send private message
ulenrich
Veteran
Veteran


Joined: 10 Oct 2010
Posts: 1480

PostPosted: Thu Mar 27, 2014 8:48 am    Post subject: Reply with quote

steveL wrote:
The problem is the recursive bind, which is also bind-mounting /dev/shm and /dev/pts. See ulenrich's (rather quaint;) script, and this post.

Yes, rather ugly looking at it after a while :(
But shouldn't there be available a standard chroot script, which enables admin-users to avoid rbind mounts when chrooting?
Back to top
View user's profile Send private message
wuzzerd
Guru
Guru


Joined: 05 Jan 2005
Posts: 466
Location: New Mexico

PostPosted: Thu Mar 27, 2014 9:44 am    Post subject: Reply with quote

You could try unmounting them in reverse order.

Code:
#!/bin/bash

umount /mnt/arch/sys
umount /mnt/arch/dev
umount /mnt/arch/proc
umount /mnt/arch
Back to top
View user's profile Send private message
steveL
Watchman
Watchman


Joined: 13 Sep 2006
Posts: 5153
Location: The Peanut Gallery

PostPosted: Fri Mar 28, 2014 7:00 am    Post subject: Reply with quote

ulenrich wrote:
But shouldn't there be available a standard chroot script, which enables admin-users to avoid rbind mounts when chrooting?

The trouble with all those kind of "standard scripts" is that unless they're being used as part of something else, they're not much use on their own. They have to be adjusted for the user's situation, and it's usually simpler for the user just to write their own "script" (by running the commands).

Shell is the essential user-interface at the admin level, and indeed for the end-user on Unix. Most of us love a GUI, sure, but it's best understood with comprehension of processes and arguments supplied to them.
Back to top
View user's profile Send private message
tipp98
Tux's lil' helper
Tux's lil' helper


Joined: 28 Jan 2007
Posts: 113

PostPosted: Fri Mar 28, 2014 11:28 am    Post subject: Reply with quote

steveL wrote:
tipp98 wrote:
Should a bug be reported somewhere?

No. The problem is the recursive bind, which is also bind-mounting /dev/shm and /dev/pts. See ulenrich's (rather quaint;) script, and this post.


It used to work with -o rbind though. To me that indicates that rbind is not the problem, but the way umount deals with unmounting the rbind has broke. Maybe because umount no longer sees the rbind option in the list of mount options, just a guess. In any case, umount should be able to properly unmount something that was mounted through mount.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things 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