Raspberry Pi_Eng_15.1.3 Mounting Device


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


15.1.3  Mounting Device

 

15.1.3.1    Meaning of Mount

 

On Linux systems, all files used on the system are supposed to belong to a file hierarchy, one large tree type with root (/) at the top. These files may appear to be in one hierarchy, but may actually be scattered across a variety of devices.

 

If a partition is created for a disk and the format operation specifies a particular filesystem type for the partition, the disk device is ready for data processing. However, to actually use these devices, the file system of this device must be linked to the entire file tree of Linux.

 

"mount" command adds the file system on device to the entire large file tree. In contrast, "umount" command detaches the file system of the device attached to the entire file tree.

 


 

15.1.3.2    Creating Directory for Mount

 

To add the file system of the device to the entire file tree of Linux, you must first create a directory to connect the file system of the disk device and create a mount point.

 

A newly mounted device is usually added under "/media" directory. You can use another if you want. Create a directory with the appropriate name depending on the device type.

 

sudo mkdir   /media/directory


 

15.1.3.3    Adjusting Permissions on the Mount Directory

 

You need to adjust the permissions for newly mounted directories, if necessary. You can use "chown" command to change the owner user of a directory to another user

 

sudo chown R <userid> <directory>

 

"chgrp" command allows all users in a group to access the directory.

 

sudo chgrp R <group> <directory>

 

You can use "chmod" command to grant "read", "write" or "execute" permissions to the directory for users. The following is a example to grant "write" permission to owner group.

 

sduo chmod R g+w  <directory>

 

 


 

15.1.3.4    "mount" Command - Specify Directory for Device

 

When Linux reads or writes data to and from the disk device, it processes it through a specific directory in the Linux file system. The operation of assigning a disk device to a directory of file system is called "mount".

 

[Command Format]

mount        [ options ]  [ -t type ]  <device>    <file-directory>

 

[Command Overview]

   This assigns the file system of a specific disk device to a specific directory of the entire file system in the system.

   User privilege          -- Super user. 

 

[Detail Description]

The "mount" command instructs the kernel to attach the filesystem on the device of the specified type to the specified directory. While the device filesystem is mounted, the directory specified for the device acts as root for the device filesystem.

 

A typical disk device is specified a device file name that corresponds to a partition, such as "/dev/sda1", but you can use a different method depending on the device type. For normal block devices, you can specify the desired device by specifying the LABEL or UUID for the partition using "-L" or "-U" option.

 

If you specify either directory or device, the "mount" command will search for a directoy that matches the label name of the device or device with the label name that matches the directory, and if it does not exist, it will find and process the necessary setting in "/etc/fstab" file.

 

Basically, the mounted device file system is valid only while the Linux system is running. If it boots newly, you must mount it again.

 

[Main Option]

-h, --help

Print a help message.

-l

Add the labels in the mount output. Mount must have permission

to read the disk device (e.g. be suid root) for this to work.

One can set such a label for ext2, ext3 or ext4 using the e2label(8) utility, or for XFS using xfs_admin(8), or for reiserfs using reiserfstune(8).

-a, --all

Mount all filesystems (of the given types) mentioned in fstab.

-L label

Mount the partition that has the specified label.

-U uuid

 

Mount the partition that has the specified uuid. These two options require the file /proc/partitions (present since Linux 2.1.116) to exist.

-t, --types vfstype

The argument following the -t is used to indicate the filesystem type.

-M, --move

Move a subtree to some other place. See above.

-w, --rw

Mount the filesystem read/write. This is the default. A synonym is -o rw.

 

[Used Example]

Here we will try to mount the USB memory manually. First, we will create a directory to be used as the mount point on the "mount" command. The following is to check the contents of the "/medir/pi" directory which is the mount location before doing the work, and check the contents again after creating the directory. You can see that there is no data under "/media/pi/ESD" directory.

 

pi@raspberrypi3:~ $ ls -l /media/pi

total 4

drwx------ 2 root root 4096 Jun 13  2016 SETTINGS

pi@raspberrypi ~ $ sudo mkdir /media/pi/ESD

 

pi@raspberrypi3:~ $ ls -l /media/pi

total 8

drwxr-xr-x 2 root root 4096 Jan 28 03:16 ESD

drwx------ 2 root root 4096 Jun 13  2016 SETTINGS

pi@raspberrypi3:~ $ ls -l /media/pi/ESD

total 0

 

Then perform the work to mount the USB memory. The following is to check the device name for USB memory with "blkid" command in order to do this, execute the "mount" command for that device, and check the contents of "/media/pi/ESD" directory.

 

pi@raspberrypi3:~ $ lsusb

pi@raspberrypi3:~ $ sudo blkid

/dev/mmcblk0p1: LABEL="RECOVERY" UUID="1715-1C28" TYPE="vfat" PARTUUID="0004fe7f-01"

/dev/mmcblk0p5: LABEL="SETTINGS" UUID="f8476b90-b9a0-4456-91e7-46fece4e553d" TYPE="ext4" PARTUUID="0004fe7f-05"

/dev/mmcblk0p6: SEC_TYPE="msdos" LABEL="boot" UUID="680D-6FDD" TYPE="vfat" PARTUUID="0004fe7f-06"

/dev/mmcblk0p7: LABEL="root0" UUID="4adc68f8-4b6e-41c8-ab14-bef7f71f803a" TYPE="ext4" PARTUUID="0004fe7f-07"

/dev/sda1: LABEL="ESD-USB" UUID="7290-B7A1" TYPE="vfat"

/dev/mmcblk0: PTUUID="0004fe7f" PTTYPE="dos"

pi@raspberrypi3:~ $ sudo mount /dev/sda1 /media/pi/ESD

 

pi@raspberrypi3:~ $ ls -l /media/pi/ESD

total 1624

-rwxr-xr-x 1 root root      43 Mar 31  2015 autorun.inf

drwxr-xr-x 5 root root    4096 Apr 10  2016 boot

-rwxr-xr-x 1 root root  400228 Oct 30  2015 bootmgr

-rwxr-xr-x 1 root root 1147736 Oct 30  2015 bootmgr.efi

drwxr-xr-x 4 root root    4096 Apr 10  2016 efi

-rwxr-xr-x 1 root root   79552 Oct 30  2015 setup.exe

drwxr-xr-x 2 root root    4096 Apr 10  2016 System Volume Information

drwxr-xr-x 6 root root    4096 Apr 10  2016 x64

drwxr-xr-x 6 root root    4096 Apr 10  2016 x86

drwxr-xr-x 3 root root    4096 Jan 26 21:51 Ztool

 

If you check the contents of the "/media/pi/ESD" directory after the mount operation is completed, you can see that it contains the USB memry data. This completes the mount operation for the USB memory.


 

15.1.3.5    Setting Automatic Mount

 

The mounted device file system is valid only while the system is running. If the system is shut down, the mount is released. Thus, if the system boot newly, it must mount it again.

 

You can use "/etc/fstab" file to make mount operation run automatically every time you boot.

 

The "/etc/fstab" file is originally created automatically during the Linux operating system installation phase, based on the information of root file system and other file system installed on the system. The following is the status of the file "/etc/fstab" after installing Raspberry Pi.

 

proc            /proc           proc    defaults          0       0

/dev/mmcblk0p5  /boot           vfat    defaults          0       2

/dev/mmcblk0p6  /               ext4    defaults,noatime  0       1

# a swapfile is not a swap partition, so no using swapon|off from here on, use  dphys-swapfile swap[on|off]  for that

 

To add the necessary information to this file, you can add a new item without touching the existing settings. The setting method is as follows

 

<file system>     <mount point>   <type>  <options>         <dump>           <pass>

 

Make the necessary settings on one line for one file system. Separate each field with Tab key. Details of each item are as follows.

 

 

   <file system>

 

It specifies the device of the partition that represents the file system. You can use several methods as shown below:

    How to use the device file name in "/dev"

You can use "fdisk" command or "blkid" command to check the names registered in "/dev".

Ex) /dev/sda1

 

    How to use LABEL

Use the format LABEL = <label>.

"blkid" command can be used to determine the LABEL.

Ex) LABEL="RECOVERY"

 

    How to use UUID    

Use the format UUID=<uuid>.

"blkid" command can be used to determine the UUID.

Ex) UUID="FEF225C2F2257FCF"

 

Using the LABEL = <label> or UUID = <uuid> form to specify device settings in "/etc/fstab" file is easier to read, less fluctuating, and easier to port.

 

 

   <mount point>

 

It is the location where the file system is to be mounted. All directory tree structures and stored data in the mounted file system are linked with this point.

 

 

   <type>

 

It specifies the file system type of the device. Specify the file system type of device, such as ext?, tmpfs, devpts, sysfs, swap, vfat, hfs, ufs. You can check the file systems currently supported by Linux on "/proc/filesystems" file.

 

 

   <options>

 

It is the option of the "mount" command. Set the file system attribute to use the file system for your purpose. defaults, auto, exec, suid, ro, rw, user, nouser, etc. (See "-o" option in "man mount" command)

 

 

   <dump>

 

Whether the file system is dumped: (has a value of 0 or 1)

It is only meaningful when performing a backup using "dump" command.

Number 0: file system that can not be dumped by "dump" command.

Number 1: file system that can be dumped by "dump" command.

 

 

   <pass>

 

Whether to check file system:

Indicates whether an integrity check using "fsck" command is required.

Number 0: no file system check required ("fsck" command is not executed)

Number 1: file system check required first (root file system)

Number 2: file system check required first (rest of file system except root file system)

 

 


 

15.1.3.6    "umount" Command - Release Device's Directory

 

To detach the file system of a particular device from the entire file tree of Linux, use the "umount" command.

 

[Command Format]

umount      [ options ]   [ device ]    [ file-directory ]

 

[Command Overview]

   This detaches the file system of a specific device from the entire file tree of the system.

   User privilege          --Super user.

 

[Detail Description]

   If a file on a mounted file system is in use, or a process is using that directory as its working directory, or the swap file on the file system is in use, it can not be umounted for that device.

 

[Main Option]

-h

Print help message and exit.

-r

In case unmounting fails, try to remount read-only.

-n                      

Unmount without writing in /etc/mtab.

-a

All of the file systems described in /etc/mtab are unmounted. (With umount version 2.7 and later: the proc filesystem is not unmounted.)

-t vfstype

Indicate that the actions should only be taken on file systems of the specified type.  More than one type may be specified in a comma separated list. The list of file system types can be prefixed with no to specify the file system types on which no action should be taken.

-O options

Indicate that the actions should only be taken on file systems with the specified options in /etc/fstab. More than one option type may be specified in a comma separated list. Each option can be prefixed with

no to specify options for which no action should be taken.

-f

Force unmount (in case of an unreachable NFS system). (Requires kernel

2.1.116 or later.)

-l

Lazy unmount. Detach the filesystem from the filesystem hierarchy now,

and cleanup all references to the filesystem as soon as it is not busy

anymore. (Requires kernel 2.4.11 or later.)