3

I'm doing some experiments 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 I'm doing a LOT of tests, it gets really annoying to issue the umount/mount combo every time.

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

  • I'm using Ubuntu 10.10
  • All command-line parameters passed to the script must be "re-passed" to the mount command (so I can use -a, -t type, etc)
  • Preferably, only the last parameter should be passed to umount

4 Answers 4

6

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

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

now whenever you run remount /whatever, it'll do an umount, 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.

4
  • 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, 2011 at 20:42
  • Right, good suggestion. Adjusted the code accordingly
    – phemmer
    Jun 16, 2011 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, 2011 at 22:31
  • 1
    Yes function remount() { umount "${!#}" && mount "$@"; }
    – phemmer
    Jun 17, 2011 at 0:37
4

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)

3
  • Ya, this is very limited, the only thing it can change is mount flags.
    – phemmer
    Jun 16, 2011 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, 2011 at 22:39
  • 2
    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.
    – phemmer
    Jun 17, 2011 at 0:46
1

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
2
  • 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, 2011 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, 2011 at 22:32
1

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

3
  • This is extremely error prone. Spaces in any of the arguments will break it.
    – phemmer
    Jun 17, 2011 at 0:43
  • Rule of thumb: never use $*, only "$@". There's a simple way of obtaining the last parameter, see my edit. Jun 17, 2011 at 7:24
  • 1
    eval, ew. "${!#}"
    – phemmer
    Jun 17, 2011 at 21:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .