Mounting the filesystem in linux

How to mount and umount a file system in Linux

A file system residing on a SATA/PATA or SCSI device needs to be mounted manually to access it. The mount command allows the root user to manually mount a file system. The first argument of the mount command specifies the file system to mount. The second argument specifies the target directory where the file system is made available after mounting it. The target directory is referred to as a mount point.

The general syntax for mounting a file system with mount is:

By using mount, you can override the default settings in /etc/fstab. For example, entering the following mounts the partition /dev/sdd1 to the directory /data:

You do not usually specify the file system type because it is recognized automatically (using magic numbers in the superblock, or simply by trying different file system types; see man mount for details).

The following are some of the options you can use when mounting a file system with the command mount or by entering them in /etc/fstab.

  • remount. This option causes file systems that are already mounted to be mounted again. When you make a change to the options in /etc/fstab, you can use remount to incorporate the changes.
  • rw, ro. These options indicate whether a file system should be writable (rw) or only readable (ro).
  • sync, async. These options set synchronous (sync) or asynchronous (async) input and output in a file system. The default setting is async.
  • atime, noatime. These options set whether the access time of a file is updated in the inode (atime) or not (noatime). The option noatime should improve the performance.
  • nodev, dev. The nodev option prevents device files from being interpreted as such in the file system.
  • noexec, exec. You can prohibit the execution of programs on a file system with the option noexec.
  • nosuid, suid. The nosuid option ensures that the suid and sgid bits in the file system are ignored.

Some options only make sense in the file /etc/fstab. These options include the following:

  • auto, noauto. File systems set with the option noauto in the file /etc/fstab are not mounted automatically when the system is booted.
  • user, nouser. This option lets users mount the file system. Normally, this is a privilege of the user root.
  • defaults. This option causes the default options rw, suid, dev, exec, auto, nouser, and async to be used.

The options noauto and user are usually combined for removable media such as floppy disk or CD-ROM drives.

Unmount a File System

Once a file system is mounted, you can use the umount command (without an “n”) to unmount the file system. You can unmount the file system by using umount with the device or the mount point.
For example to unmount a file system (dev/sdd1) mounted at /data, you could enter one of the following:

In order to unmount the file system, no application or user may use the file system. If it is being used, Linux sees the file system as being “busy” and will refuse to unmount the file system and will produce the below error.

Unmounting is not possible if the mount point is accessed by a process. For umount to be successful, the process needs to stop accessing the mount point.

The lsof command lists all open file and processes accessing them in the provided directory. It is useful to identify which processes currently prevent the file system from successful umounting.

Читайте также:  Windows home edition download

You can also use the fuser command to get the process IDs of the processes currently running on the mpunt point you want to umount.

You can also kill all the processes on the mount point using the fuser command.

Once the processes are identified, an action can be taken, such as waiting for the process to complete or sending a SIGTERM or SIGKILL signal to the process. In this case, it is sufficient to umount the mount point.

Force Umount the file systems

There might be times when the system (kernel) sees the file system as busy, no matter what you try to do. In these cases, you can enter umount -f to force the file system to unmount. However, we recommend using this only as a last resort, as there is probably a reason why the kernel thinks the file system is still mounted.

View Currently Mounted File Systems

You can view the file systems currently mounted by entering the command mount. Information similar to the following appears:

You can also view this information in the file /proc/mounts.

Источник

How to create and mount filesystems in Linux

File System Types

Creating a file system writes information to the device and creates order of the empty space. This file system–related data consumes a small percentage of the space. The remaining space on the disk drive is split into small, consistently sized segments called blocks. Linux supports a number of file system types, some of which are described as follows.

Filesystem Description
ext2 High performance for fixed disk and removable media
ext3 Journaling version of ext2
ext4 Supports larger files and file system sizes
vfat MS-DOS file system useful when sharing files between Windows and Linux
XFS High-performance journaling file system
Btrfs Addresses scalability requirements of large storage systems

Creating Filesystems

The command to build a Linux file system on a device, or hard disk partition, is mkfs. The syntax for the command is:

The mkfs command is actually a front end for the different file system builder utilities such as mkfs.ext2 and mkfs.ext4. These utilities are executable directly from the command line. When using the mkfs wrapper, include the -t fstype option to specify the type of file system to be built. If not specified, the default file system type, ext2, is created.

To see which supported file system types are installed, use the ls /sbin/mkfs* command:

The mkdosfs, mkfs.msdos, and mkfs.vfat files are symbolic links to mkfs.fat.

Using mkfs

The default file system type created when using the mkfs command is ext2. As previously mentioned, mkfs is a wrapper that calls other file system build utilities. Therefore, any of the following commands create an ext2 file system on the specified device:

To create an ext3 file system, use any of the following commands:

To create an ext4 file system, use any of the following commands:

Configuration File

A number of options are available to customize block size, fragment size, blocks per group, journal options, number of inodes, and other parameters. Without including any options, the defaults that are specified in the /etc/mke2fs.conf configuration file are used.

File System Labels

A useful option for the file system build utilities is the -L name option. This assigns a label to the partition; this label can be used instead of the device name when mounting the file system. Labels are limited to a maximum size of 16 characters. For existing file systems, the e2label command is used to display or set a label.

File systems are automatically assigned a universally unique identifier (UUID). UUIDs can be used when mounting the file system. To display the UUID, the label, and the file system type, use the blkid command. The following examples illustrate creating different file systems, with and without a label, and displaying the information with the blkid command. To create an ext2 file system and display information, enter:

To create an ext3 file system and display information, enter:

To create an ext4 file system, assign a label name, and display information, enter:

Читайте также:  Linux изменить пароль админа

Mounting File Systems

File systems on different partitions and removable devices, such as CDs, DVDs, or USB flash drives, must be attached to the directory hierarchy to be accessed. To attach a partition or device, a mount point must be created. A mount point is simply a directory created with the mkdir command. After a directory, or mount point, is created, attach the partition by using the mount command. Syntax for the mount command is:

The following example creates a mount point (/test) and attaches the partition:

Alternatively, mount the partition or device by referencing the UUID or label. The following example displays the UUID and label, using the blkid command, and mounts the partition by referencing each:

The mount command without any options displays all currently attached file systems:

In this example, the /dev/xvdf1 partition is mounted on /test. The file system type is ext4 and is mounted for both reading and writing. The df command also displays mounted file systems. Example:

The information in the proc file system displays mounted file systems. Example:

Mount Options

To specify mount options, use the –o flag followed by a comma-separated string of options. The following are some of the available options for the mount command:

  • auto: Allows the file system to be mounted automatically by using the mount –a command
  • loop: Mounts the image as a loop device
  • noauto: Disallows the automatic mount of the file system by using the mount –a command
  • noexec: Disallows the execution of binary files on the file system
  • nouser: Disallows an ordinary user (other than root) to mount and unmount the file system
  • remount: Remounts the file system in case it is already mounted
  • ro: Mounts the file system for reading only
  • rw: Mounts the file system for both reading and writing
  • user: Allows an ordinary user (other than root) to mount and unmount the file system

For example, to mount the /dev/xvdf1 partition on the /test mount point as read-only with only the root user able to mount and unmount the file system, enter:

To mount an ISO image by using the loop device (assuming that the ISO image is present in the current directory and the mount point exist), enter:

Journaling Mount Options

The ext3 and ext4 file systems have three journaling levels that can be set with the -o option in the mount command or in the options section of /etc/fstab:

  • data=journal: The highest level. The one that does the most journaling. This writes the journal entries for all the data and metadata changes. All data is committed into the journal before being written into the main file system.
  • data=ordered: The default mode. All data is forced directly out to the main file system before its metadata is committed to the journal.
  • data=writeback: The lowest level. Data ordering is not preserved. Data can be written into the main file system after its metadata has been committed to the journal.

Unmounting File Systems

To unmount a file system, use the umount command. The partition name, the device name, or the mount point is used as an argument. Example:

/etc/fstab File

The /etc/fstab file is called the file system mount table and contains all the information that the mount command needs to mount devices. When adding a new file system, create the appropriate entry in /etc/fstab to ensure that the file system is mounted at boot time. The following is an example of entries in the /etc/fstab file:

The first column is the device to mount. The UUID or the label name should be used in place of the device name, because device names could change. The second column is the mount point, except the swap partition entry. The third column is the file system type. The fourth column specifies mount options. The fifth column is used by the dump command. The number 1 means to dump the file system and 0 means the file system does not need to be dumped. The last column is used by the fsck program to determine the order in which file system checks are done at reboot time. The root file system should be specified with a value of 1 and the other file systems should have a value of 2. A value of 0 does not check the file system.

Читайте также:  Intel core i5 2500k windows 10

Источник

How to Create, Format and Mount Filesystems in Linux

A filesystem is an organization of data and metadata on the storage device. If you want to access any files in Unix like operating system, the filesystem has to be mounted where the file resides.

The well-know Linux filesystems are Ext, Ext2, Ext3, Ex4, BtrFS, ReiserFS, ZFS, XFS, JFS, and Swap.

Let’s create a partition on Linux, create a filesystem, and learn how to mount that filesystem.

Step 1: Create a Partition

Before creating a file system, make sure you have enough unallocated disk space ( or free cylinders).

You can check disk space using fdisk -l or parted print free command:

Here you can see 1305 cylinders are present on ‘/dev/sda’ disk and used up to 1147 cylinders. Therefore, we can create a new partition.

You may use fdisk or parted command to create a new partition.

In the following example, I am using fdisk command to create a partition on the hard drive named ‘/dev/sda’ (first hard disk).

Step 2: Set Disk Label on the partition

You can use e2label command to set or change disk labels. The e2label command must be run as root user.

The following command set disk datafiles on the disk partition named ‘/dev/sda3’.

To view the disk label, run the following command:

Step 3: Create a filesystem

In Linux, you can create filesystem using mkfs, mkfs.ext2, mkfs.ext3, mkfs.ext4, mke4fs or mkfs.xfs commands. On RHEL and CentOS system you may install an additional package called e4fsprogs which can manage ext4 filesystem.

The following commands create an ext4 filesystem on the ‘/dev/sda3’ disk partition:

Step 3: Mounting a Filesystem

The most commonly used method for mounting the filesystem is either manually using mount command or by adding entries in /etc/fstab file, so that filesystem gets mounted during boot time.

In the above example, we have mounted ‘/dev/sda3’ partition to ‘/data’ directory.

You can verify by executing the following command:

Also, you can unmount /dev/sda3 using umount command.

Whenever linux system reboots the ‘/data’ filesystem gets unusable. If you want to use the filesystem again, you have to mount it manually.

To avoid this repeated mounting after Linux boot, we have to add entries in /etc/fstab file so it will be persistent over reboots.

Here we will brief about /etc/fstab configuration file. You should add an entry in fstab file as follows:

An example fstab file:

device name: Name of the device/partition or source path (What to mount) /dev/sda3

mount point: Where data is attached to the filesystem (Where to mount) /data

type of the FS: Type of the filesystem are ext2, ext3, ext4, nfs, proc, etc.

options: In this option, you can apply a security policy to the particular file system. For example, when you mount, you can either set no execution of the binaries or you can set read-only filesystem. By default, the filesystem is having rw, suid, rw, exec, auto, nouser and async.

dump: This is used for filesystem backup. If value zero is set, backup is ignored. If 1 is set, the filesystem is backed up.

fsck: This option is to determine on which order the filesystems should be checked.

Display Mount Information

You can run df -h or lsblk command to get mounted device information such as mount point, filesystem size, etc.

The findmnt is a very handy tool to list all mounted filesystem, run the command as below:

Conclusion

In this tutorial, we learn how to create a filesystem in Linux by first creating a partition, formatting, and finally mounted. If you have any questions or feedback, feel free to leave a comment.

Источник

Оцените статью