LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help me on how to umount partition when its opened on another shell... (https://www.linuxquestions.org/questions/linux-newbie-8/help-me-on-how-to-umount-partition-when-its-opened-on-another-shell-4175561056/)

mangya 12-09-2015 11:44 AM

Help me on how to umount partition when its opened on another shell...
 
Hello All

I've a LUKS parition on /dev/sda3. I don't want to mount this partition on boot, so I've written this script to mount and umount as and when its required. I've put this script in /etc/bash.bashrc

Any time I want to open this partition, I type 'yama 1'. To close I type 'yama 0'. I've also ensured, while umounting partition, if I'm in the same partition (directory), I'll be moved to home directory - and then it will be umounted. But this works fine when I'm on the same shell. While I'm on the partition in one tab, and if I open a new tab, and there I type 'yama 0', then I get error and partition will not be umounted.

How do I ensure, no matter where i issue command, the partition will be umounted successfully.

This is my script
Code:

function yama() {
  # Opens or closes ym filesystem
  # To open : yama 1
  # To close: yama 0

  choice=$1

  if [[ $# -ne 1 ]] ; then
    echo "No arguments given. Give '1' to mount, '0' to umnount."
    exit
  elif [[ $choice -eq 1 ]]; then
    if mountpoint -q "/ym"; then
      echo "Yama already mounted. Doing nothing."
    else
      echo "Mounting Yama..."
      sudo cryptsetup luksOpen /dev/sda3 drdo
      sudo mount /dev/mapper/drdo /ym
      echo "Yama Opened."
      cd /ym
    fi 
  elif [[ $choice -eq 0 ]]; then
    if mountpoint -q "/ym"; then
      echo "Unmounting Yama..."
      cd ~
      sudo umount /ym
      sudo cryptsetup luksClose drdo
      echo "Yama Closed"
    else
      echo "Yama Not mounted. Doing nothing."
    fi 
  else
    echo "I dont know what you said"
  fi 
}

Result
Code:

[abc@xyz ~]$ yama 1
Mounting Yama...
[sudo] password for abc:
Enter passphrase for /dev/sda3:
Yama Opened.
[abc@xyz /ym]$

[abc@xyz /ym]$ yama 0
Unmounting Yama...
Yama Closed
[abc@xyz ~]$

This is the error - when I try to close on another tab
Tab 1:
Code:

[abc@xyz ~]$ yama 1
Mounting Yama...
[sudo] password for abc:
Enter passphrase for /dev/sda3:
Yama Opened.
[abc@xyz /ym]$

Tab 2:
Code:

[abc@xyz ~]$ yama 0
Unmounting Yama...
[sudo] password for abc:
umount: /ym: target is busy
        (In some cases useful info about processes that
        use the device is found by lsof(8) or fuser(1).)
device-mapper: remove ioctl on drdo failed: Device or resource busy
device-mapper: remove ioctl on drdo failed: Device or resource busy
device-mapper: remove ioctl on drdo failed: Device or resource busy
device-mapper: remove ioctl on drdo failed: Device or resource busy
device-mapper: remove ioctl on drdo failed: Device or resource busy

The error message is suggesting to use lsof or fuser, but I know what's the problem - its opened in another tab.

How do i solve this?

Thanks

suicidaleggroll 12-09-2015 11:54 AM

You might be able to script lsof or fuser to get the terminal ID of the one sitting in the directory, and then use one of the suggestions here to send it a cd command to move out:
http://unix.stackexchange.com/questi...ctive-terminal

Or skip the cd and just kill the session that's in the wrong spot.

Emerson 12-09-2015 12:03 PM

Have you tried lazy umount?

mangya 12-09-2015 01:00 PM

Thank you very much.

@suicidaleggroll, I tried ttyecho, but it doesn't work on my pc (arch linux). Didn't try selector - it looks too complicated. Anyway thanks.
@Emerson, It worked wonderfully. Thanks.


All times are GMT -5. The time now is 01:27 PM.