Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Im doing some experiences with NTFS(-3G) and fstab. For every change i do to fstab, I need to issue:

sudo umount /mountpoint
sudo mount /mountpoint

To check the results. Since Im doing a LOT of tests, its gets really annoying to issue umount/mount combo every time.

Is there any way I could combine these 2 commands in a single remountscript? Is there already any switch to mount that do that?

  • Im using Ubuntu 10.10
  • All command-line parameters passed to the script must be "re-passed" to mount comand (so I can use -a, -t type, etc)
  • Preferably, only the last parameter should be passed to umount
share|improve this question
add comment

4 Answers

up vote 5 down vote accepted

In your shell (assuming bash or other compatible shell) run

function remount() { unmount "$1" && mount "$1"; }

now whenever you run remount /whatever, it'll do an unmount, then a mount.
This will expire if you close your shell. So if you want it to persist, put it in your .bashrc, .profile, or whatever applies to your case.

share|improve this answer
    
I suggest using && instead of ; to connect those commands so that mount only runs if unmount returns a good exit code. You don't want to go mounting multiple layers! –  Caleb Jun 16 '11 at 20:42
    
Right, good suggestion. Adjusted the code accordingly –  Patrick Jun 16 '11 at 21:06
    
Great neat function! But $1 is just the first parameter right? Is there any way to pass all parameters to mount? And, if possible, only the last (usually the mountpoint) to umount? –  MestreLion Jun 16 '11 at 22:31
1  
Yes function remount() { umount "${!#}" && mount "$@"; } –  Patrick Jun 17 '11 at 0:37
add comment

mount <something> -o remount,any_additional_options

But this won't work if you need to change fs type (e.g. from ntfs to ntfs-3g)

share|improve this answer
    
Ya, this is very limited, the only thing it can change is mount flags. –  Patrick Jun 16 '11 at 21:18
    
But mount flags is the only thing I need to change, this this command seems to suit my needs perfectly. Is there any way it can be changed to something like mount $1 -o remount,$2 [$3 $4 $5... $n] ? –  MestreLion Jun 16 '11 at 22:39
1  
Just be careful. This is extremely useful as open filehandles on the mount can stay open. But not all mount flags can be changed, and sometimes it wont even warn you, it'll just say "OK DONE" without actually changing them. But if it suits what youre doing, its definitely nicer than a full unmount & mount. –  Patrick Jun 17 '11 at 0:46
add comment

You know you can chain any two commands together on the command line by using a ; to connect them? This makes things you do in pairs really easy to run from history in the command line.

umount /path ; mount /path

You might consider using an AND operator to connect them so that mount only gets run if umount succedes, like this:

umount /path && mount /path

If you want, you can even add your editor in there like this:

umount /path ; vim /etc/fstab ; mount /path

The drive will unmount, you will be shown the fstab, and when you exit the editor it will be mounted again.

Beyond that, if you need a more generic soluton for multiple paths, the suggestion of a function is good. Also, ANY series of comands in unix can be made into a script. Just save a text file like this:

#!/bin/bash
umount /path
mount /path

Then set it to be executable:

chmod +x filename

then run it like this:

./filename
share|improve this answer
    
The chained commands is an amazing ideia... specially with the editing included. Im using gedit, so thats not the case.. i have to ALT+TAB anyway. –  MestreLion Jun 16 '11 at 22:30
    
@MestreLion: There is no reason you can't use gedit just the same way! It will launch from the command line and things won't continue until it exits. –  Caleb Jun 16 '11 at 22:32
add comment

How about that (untested):

function remount() {
  eval last=\$$#
  umount "$last" &&
  mount "$@"
}

You can just copypaste that code in shell or append it to your .bashrc

share|improve this answer
    
This is extremely error prone. Spaces in any of the arguments will break it. –  Patrick Jun 17 '11 at 0:43
    
Rule of thumb: never use $*, only "$@". There's a simple way of obtaining the last parameter, see my edit. –  Gilles Jun 17 '11 at 7:24
1  
eval, ew. "${!#}" –  Patrick Jun 17 '11 at 21:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.