Loading...
Basic Linux

How to mount and umount a file system in Linux

Mount a File System

  • After formatting a partition we cannot add the data into the partition> In order to add the data in the partition it is required to be mounted.
  • Mounting is a procedure where we attach a directory to the file system
  • They can be mounted to any directory, which is referred to as a mount point. Every mount point before is a directory.
  • If you mount a file system on a directory that is not empty. Everything within that director becomes inaccessible. Therefore, you should create a new directory as a mount point for each of your file systems.
  • There are only two commands for mounting a file system:
    mount Mounts a file system.
    umount Unmounts a file system.

Step 1:
Start by going to the /opt directory, where you can make some directory to serve as a mount points.

#cd    /opt

#mkdir   company_data

#mldir    backup

Syntax:

mount   [options]    [Device]   [Mount_point]

Options:
-r Mounts as read_only
-w Mounts as read/write (the default)
-L Mounts the file system with the name LABLE
-v provides verbose output

Step 2:
Mount the two file systems.

#mount    /dev/sda6   /opt/company_data

#mount    /dev/sda7   /opt/backup

Notice that you don’t specify a file system type or any mount options. The reason is that the mount command automatically detects the file system type and mounts it for you. By default, the file system is also mounted with the defaults option (rw).

Step 3:
To unmount a file systems:
System:

unmount   [options]   [Mount_point]

options:
-f Force unmount
-v Provides verbose output

Step 4:
You can use the “fuser and lsof” commands to check for open files and users that are currently using files on a file system.
Syntax:

fuser   [options]    [Mount_point/File system]

Options:
-c Check the mounted the file system
-k Kill processes using the file system
-m Show all processes using the file system
-u Display user IDs
-v verbose output

Check to see what users are currently using the file system

#fuser   -cu   /dev/sda6

#lsof      /dev/sda6

To kill the open connections, you can use the fuser cmp again:

#fuser    -ck   /opt/backup

Now you should be to unmount the file system

#umount    /opt/backup

Now you know how to mount and unmount file system, but there is something else you need to look at. I you reboot your system right now, all the file systems that you just mounted will no longer be available when the system comes backup. Why? The mount command is not persistent, so anything that is mounted with it will no longer be available across system reboots. I suppose you want to know how to fix that, right? The system looks at two config files

  1. /etc/mtab: Contains a list of all currently mounted file systems.
  2. /etc/fstab: Mount all listed file systems with given option at boot time.

1. View the /etc/mtab file:

#cat    /etc/mtab

Every time you mount or unmount a file system this file is updated to always reflect what is mounted on the system

You can also query to check whether a particular file system is mounted.

#cat   /etc/mtab  |  grep   backup

You can use the mount command with no options to also view the currently file systems:

#mount

2. Go through the ?etc/fstab file.
The file follows this syntax:

<device>   <Mountpoint>   <File system type>   <Mount options>  <write data during shutdown>   <check sequence>

View the /etc/fstab file:

#cat   /etc/fstab

The first tree fields should be fairly obvious because you been working with them throughout the chapter. The fourth fields defines the options that you can use to mount the the file system. The fifth field defines whether data should be backup (also called dumping) before a system shutdown or reboot occurs. This field commonly use a value of 1. A value of 0 might be used if the file system is a temporary storage space for files such as /tmp. The last field defines the order in which file system checking should take place. Fore the root file system, the value should be 1; Everything else should be 2. If you have a removable file system{CD-ROM or External). You can define a value of 0 and skip the checking altogether. Because, you want the two file systems created earlier to be mounted when the system boots, you can add two definitions for them here.

Open the /etc/fstab file for editing:

#vim   /etc/fstab

/dev/sda6   /opt/backup   ext3   defaults  0   0

:wq!

You can use the mount command with -a options to remount all file systems define in the /etc/fstab file

#mount    -a

Extra File System Commands

Label:
Labels enable you to determine a specific file system more easily with a common name, instead of /dev/sda6.an added benefit is the systems being able to keep its label even if the underlying disk is switch with a new one.
Step 1. Take your file system offline.

#umount   /dev/sda6

Step 2. Let’s label the file system data to denote that its the commany_data file system

#e2label   /dev/sda6    CData

Step 3. You can the same command to also verify.

#e2label    /dev/sda6

Step 4. Find the file system you just labelled.
Syntax:

findfs     LABLE= <label>  |  UUID = <uuid>

#findfs   LABLE = CData

  • You can also query more information about the device using the blkid command.
    Syntax:

blkid   [options]

Options:
-s Show specified tag(s)
dev Specifies the device to probe

Step 5. Combine the blkid command with grep for specific results.

#blkid      |   grep   CData

Step 6. When you finish your maintenance, you can remount the file system with the new label instead of the device path.

#mount   LABEL = CData   /opt/company_data

You could even update the /etc/fstab file to use the label information instead of the device path.

#vim   /etc/fstab
LABEL = CData /opt/company_data ext3 default 0 0
: wq!

You can use the mount command to verify the label names.

#mount    -l

You also can use the df command to view the usage information for your file systems.
Syntax:

df   [options]

Option:
-h Specifies human_readable format
-l Local File Systems only
-T Point the File System type

# df     -h

#  df    -Th  


Leave a Reply

Your email address will not be published. Required fields are marked *