Monday, July 12, 2010

Partition mounting in Linux


Permissions and Ownership on NTFS Partitions

The NTFS filesystem does not support Linux permissions or ownership per se. You can't successfully change ownership with the Linux command chown and you can't successfully change permissions with the Linux command chmod. Ownership and permissions are set only in the mount command.
The permissions and ownership properties that are available for NTFS under Linux are not written into the individual files to be retained when the filesystem is unmounted or the computer is turned off. Permissions and ownership obtained under Linux are temporary artifices imposed via the mount command and maintained temporarily by the operating system. They are transient properties that last only until the NTFS partition is unmounted.
Fnd and examine your NTFS partitions
You use commands in a console/terminal window to see the NTFS partitions that are sensed by the kernel and to show which (if any) of those partitions are mounted
The fdisk command can list partitions, like so:
sudo /sbin/fdisk -l
Look for the lines contining "NTFS" in the output, like this one from my computer:
/dev/sdb1 * 1 1306 10490413+ 7 HPFS/NTFS
That identifies an NTFS partition on device sdb1, the first partition on the second internal drive.
If it's mounted, you'll see it in the response to the console command df -Th which shows the usage and locations of all mounted partitions. Look for lines containing the Type fuseblk:
chand@chand-Laptop:~> df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb1 fuseblk 80G 27G 53G 34% /media/disk
That shows my NTFS partition at sdb1 has a size of 80Gb of which 27Gb are used and 53Gb are free/unused and that my NTFS partition is mounted in the folder "disk" at /media/disk.
You can also see your NTFS partitions in the GUI viewers. KDE users look in the "My Computer" link on the Desktop. Gnome users look in Gnome's Nautilus under the Computer --> Filesystem icon. Partitions are also viewable in Yast --> System --> Partitioner.
Treatment of NTFS partitions during installation of openSUSE
The openSUSE installer program detects all internal NTFS partitions and suggests to you that they be mounted in directory /windows, with sub-directories labelled C, D, E and so on, one for each NTFS partition. Thus the first will be mounted in directory /windows/C, the second in /windows/D and so on. If you accept the defaults, you will later find your NTFS partitions (and any FAT partitions) mounted in those directories.
Any internal NTFS partitions that you create or add after installation of openSUSE will not be mounted for you and you'll have to mount them yourself as shown below.
These default mounts in openSUSE are controlled by entries in the filesystem table, a text file called fstab located at /etc/fstab. Here's an example of an entry from my fstab file for an NTFS partition on partition 1 of the first drive (all on one line):
/dev/disk/by-id/ata-WDC_WD5000BEVT-60ZAT1_WD-WXN509S27952-part1     /windows/C     ntfs-3g users,gid=users,fmask=133,dmask=022,locale=en_US.UTF-8 0 0
The first string /dev/disk/by-id/ata-WDC_WD5000BEVT-60ZAT1_WD-WXN509S27952-part1 specifies the partition to be mounted by its ID code. You have the option to specify it by device (in this case sdb1) instead of by the ID code. The second string /windows/C is the path to the mount directory. The third string specifies the driver to be used: ntfs-3g. The fourth string users,gid=users,fmask=133,dmask=022,locale=en_US.UTF-8 lists the options to apply to the mount. In short, these options (selected by the openSUSE developers) give read-only access for normal users and read-write access for the root user. Note: option users doesn't work; it's a developers mistake; ignore it. The final string 0 0 is an admin string; ignore it.
Many users, if not most, prefer to have write access to the NTFS partitions. That simple adjustment is covered in the next paragraph.

Allowing write access to the read-only mounts created during installation
I mentioned above that the default installation leaves normal users with read-only access to their partitions. It's a simple adjustment to make the drives writeable. You change the options string to it's simplest form, from this:
users,gid=users,fmask=133,dmask=022,locale=en_US.UTF-8
to this
defaults,locale=en_US.UTF-8
The file fstab is a text file and the easiest way to alter a text file is with a text editor. Open a console window and enter one of the following commands to open the file for editing:
Gnome users: gnomesu gedit /etc/fstab
KDE users: kdesu kwrite /etc/fstab
And replace the long options with these: defaults,locale=en_US.UTF-8. If you find a locale option that differs from locale=en_US.UTF-8, keep your locale option.
That was the quick fix for giving write permissions to partitions mounted read-only by the openSUSE installation program. There are other options for mounting NTFS partitions and these are covered below.
Mounting NTFS partitions permanently (with a line in fstab)
This section is for NTFS partitions that aren't already permanently mounted. If your partition was already automatically mounted during installation of openSUSE and all you want is to allow write access to it, read the sections above to make it writeable.

Tip #1: in most cases we don't have to use the locale= option. I'm going to leave it out from now on.
Suppose that you want to mount a partition permanently in a folder you create for it (e.g. mount_point), located anywhere you like in your filesystem, say at /path_to/mount_point.
To mount your NTFS partition permanently, add your version of the following line into the file system table, fstab. [and leave the last line in the file as a blank line.] Recommended option for world-writeable mount:
/dev/sdb1    /path_to/mount_point    ntfs-3g    defaults    0 0
When you reboot, the partion will mount into the folder /path_to/mount_point with permissions drwxrwxrwx, i.e with read/write access for everybody, in the style of Microsoft's insecure filesystems.
Here's an alternate option for fstab: If you want the permissions to be linux-like, you can specify a particular owner for the mount folder and its contents with this sort of line in fstab:
/dev/sdb1    /path_to/mount_point    ntfs-3g    uid=1000,gid=100,umask=0022    0 0

Tip #2: The numerical form above appears all over the net, but I've found that names work just as well so I recommend using names for openSUSE versions 11.x because it's much simpler:
/dev/sdb1    /path_to/mount_point    ntfs-3g    uid=chand,gid=users,umask=0022    0 0
In this example the "umask" with octal value 0022 produces permissions drwxr-xr-x on folder mount_point, for the owner=chand (1000) group=users (100), just like normal openSUSE user permissions. If you change the umask option to umask=0027, the permissions become drwxr-x--- and only suzette can read the mount's contents.

[Tip #3: If you want to find a user's UID & GID, run the terminal command: id chand]
Mounting NTFS partitions temporarily (using the command line)
 First you must create a directory where the partition will be mounted. This is called the mount point. For example, we'll use the directory "mount_point" located at /path_to/mount_point. It doesn't matter who owns the directory or what the permissions on it are before the mount is created, because these are automagically replaced (in the moment of mounting) by ownerships and permissions contained in (or implied in) the mount command.

Second you must know the details of your NTFS partition. Use the sudo /sbin/fdisk -learlier example we got this result revealing sdb1: command to look at your disks. In our
/dev/sdb1 * 1 1306 10490413+ 7 HPFS/NTFS
Then you can use this mount command to make the partition writeable for all users:
mount -t ntfs-3g   /dev/sdb1   /path_to/mount_point
When you execute the mount, the mount point automagically assumes these properties: user = root, group = root and permissions = drwxrwxrwx. This give write access to everyone.
If you prefer the partition to belong to a particular user, e.g. chand with UID=1000 and GID=100, then use this version:
mount -t ntfs-3g   -o uid=1000,gid=100,umask=0022   /dev/sdb1   /path_to/mount_point
 
Tip #2: The numerical form above appears all over the net, but I've found that names work just as well so I recommend using names for openSUSE versions 11.x because it's much simpler:
mount -t ntfs-3g   -o uid=chand,gid=users,umask=0022   /dev/sdb1   /path_to/mount_point
In this example the "umask" with octal value 0022 produces permissions drwxr-xr-x on folder mount_point, for the owner=chand, group=users, just like normal openSUSE user permissions. If you change the umask option to umask=0027, the permissions become drwxr-x--- and only suzette can read the mount's contents.

[Tip #3: If you want to find a user's UID & GID, run the terminal command: id chand]
 External (USB) NTFS partitions now automount with read-write access
 

OpenSUSE from version 11.1 inclusive:
The openSUSE devs arranged for external (USB) NTFS partitions to automount read-write with permissions drwxrwxrwx beginning version 11.1. Prior to that the automount was read-only. See the next paragraph for earlier releases.
OpenSUSE pre version 11.1: When you plug a USB NTFS drive into openSUSE it automounts read-only by design of the openSUSE developers. If you want it mounted read-write, you can either unmount it and then remount it using one of the CLI commands I've outlined above or you can change the system default way of automounting NTFS drives so they will always automount read-write. To do that you essentially put a link into the directory /sbin that redirects the automount process to the ntfs-3g driver. The following command will create the permanent adjustment:
sudo ln -s /sbin/mount.ntfs-3g /sbin/mount.ntfs
If at some later stage you want to revert back to the default of automounting USB drives read-only, simply delete the link mount.ntfs (located at /sbin/mount.ntfs).

Appendix: Bits and Pieces
The locale option: If the partition contains files with a national character set that has not been set (by the operating system) before the partition is mounted, those files can be invisible and appear to have vanished; very disconcerting. Setting the locale option can render them visible again; e.g. add locale=en_US.UTF-8 for us English, utf8 or locale=cs_CZ.utf8 for Czech, etc. To see them all on openSUSE enter this command in a console: locale -a. Most people do not need to include the locale option.

Failed mounts -- The "force" option (obsolete from openSUSE 11.2 onwards): If the partition is unmounted in a disorderly fashion, e.g. power failure, unmounted improperly by a user, etc, then the so-called "dirty" bit remains set and a consistency check (e.g. by booting to Windows and running chkdsk) is needed before the drive can be mounted again. You can bypass this requirement by including the force option. Just add the word force into the comma-separated list of options in your mount instructions.

Commencing openSUSE 11.2 the force option was superceded in the ntfs-3g driver by the recover and norecover options. The default is the recover option, so you don't have to explicitly include it. The option causes an attempt to recover and repair a corrupted or inconsistent NTFS volume if possible. See the man pages.

Failed Mounts -- Resetting the "dirty" bit in windows: Sometimes you get a message like this one beginning "$LogFile indicates unclean shutdown. Mount is denied because NTFS is marked to be in use.......". These are symptomatic of disorderly processes previously interrupting a clean dismount or shutdown. If you have Windows (2000 or higher) installed or handy, boot the drive in that and allow Windows to right the situation either automatically by running a consistency check on booting or by running chkdsk /f yourself from the command prompt. Alternatively you can run chkdsk /p from the repair facility after you boot from the Windows install CD and run to the end where you select to repair the installed system.

The GUI application ntfs-config:
openSUSE comes with the RPM ntfs-config, although it's not installed by default. You have to start it with this command in a console issued as root (enter su first): ntfs-config. It brings up a GUI that shows all your NTFS partitions and allows you to mount them as read-only or as read-write filesystems with simple one-click actions. You should not use this application on external drives because ntfs-config writes an entry to fstab for a permanent mount. If you reboot with that entry still remaining in fstab, you will have an error situation that will prevent booting to a Desktop Manager if the USB drive is not powered on when next you reboot. I recommend against installing or using this application for external drives because it's obsolete IMO.

Table for umask, dmask, fmask:With regard to the Linux-like permissions you get when you use the umask=xxxx option, you may adjust adjust the folder, document or user permissions quite widely. You should read the man pages. Check out the umask, dmask and fmask options. Here's a handy little table of octal permissions to use for directory permissions:
  • owner=rwx group=rwx other=rwx; i.e for drwxrwxrwx use umask=0000
  • owner=rwx group=rwx other=r-x; i.e for drwxrwxr-x use umask=0002
  • owner=rwx group=rwx other=---; i.e for drwxrwx--- use umask=0007
  • owner=rwx group=r-x other=r-x; i.e for drwxr-xr-x use umask=0022
  • owner=rwx group=--- other=---; i.e for drwx------ use umask=0077

No comments:

Post a Comment