04-A.7.2: Disk Setup Process - mkfs & mount
- Page ID
- 26840
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)The mkfs Command
The mkfs command is used to make a file system on a device, usually a partition on a hard disk drive (HDD), or it can also be a USB drive, etc.
Syntax:
mkfs [ -V ] [ -t fstype ] [ fs-options ] filesys [ blocks ]
Command Options:
Options | Option Meaning |
---|---|
-t fstype | Specifies the type of file system to be built. If not specified, the default file system type (currently ext2) is used. |
fs-options | File system-specific options to be passed to the real file system builder. Although not guaranteed, the following options are supported by most file system builders. |
-c | Check the device for bad blocks before building the file system. |
-l filename | Read the bad blocks list from filename. |
Command Line Formatting
The mkfs utility is used to create filesystem (ext2, ext3, ext4, etc) on your Linux system. You should specify the device name to mkfs on which the filesystem is to be created. WARNING: Executing these commands will destroy all the data on your filesystem.
To format the new partition as ext4 file system you need to use the file system specific mkfs - where you can use ext3, ext4, etc.
pbmac@pbmac-server $ sudo mkfs.ext4 /dev/sda1
As always, substitute "/dev/sda1" with your own partition's path.
Create A Mount Point
Now that the drive is partitioned and formatted, you need to choose a mount point. This will be the location from which you will access the drive in the future. I would recommend using a mount point with "/media", as it is the default used by Ubuntu. For this example, we'll use the path "/media/mynewdrive"
pbmac@pbmac-server $ sudo mkdir /home/Project2020
Now we are ready to mount the drive to the mount point.
Mount the Drive
You can choose to have the drive mounted automatically each time you boot the computer, or manually only when you need to use it.
Automatic Mount at Boot
You'll need to edit /etc/fstab: (gedit is a simple text editor, you can use nano or vim).
pbmac@pbmac-server $ sudo gedit /etc/fstab
Once in editor you should add the line that begins with "/dev/sda6" to the end of the file. The following example to add a partition we have created and it will reside on the /dev/sda6 and is labeled /home/Project2020. The green text is an explanation of the various fields in this file.
# <file system> <mount point> <type> <options> <dump> <pass> proc /proc proc nodev,noexec,nosuid 0 0 /dev/sda6 /home/Project2020 ext4 defaults 0 2 [--------] [----------------] [--] [------------------------------] Linux Device Mount point File Mount Options Sytem Type
Note: some Linux distros now recommend to use UUID in the /etc/fstab file. Check with your distro documentation to find out how to discover the UUID value for your partitions. Here is some help from Ubuntu.
If you are on a distribution of Linux that requires the use of the UUID mentioned above then you have a few more steps. You may have to consult your distros man pages for how to find the UUID for your storage devices. The example below uses commands on an Ubuntu distro.
pbmac@pbmac-server $ ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 Jun 15 11:22 3709f1b7-6417-413e-a4cd-2bec3002ba25 -> ../../sda6
We know the partition will be mounted on /dev/sda6, so we now have the UUID value. Edit the /etc/fstab file and enter:
# <device> <dir> <type> <options> <dump> <fsck>
UUID=fcdc8d9a-73f9-40a2-8a66-3d6f18f16ba3 /home/Project2020 ext4 defaults 0 2
You can now run sudo mount -a (which will mount all of the file system specified in the /etc/fstab file) or reboot the computer to have the changes take effect.
If you want to allow a normal user to create files on this drive, you can either give this user ownership of the top directory of the drive filesystem: (replace USERNAME with the actual username).
pbmac@pbmac-server $ sudo chown -R pbmac:pbmac /home/Project2020
Or, in a more flexible way and practical if you have several users, allow for instance the users in the proj2020 group (usually those who are meant to be able to mount removable disks, desktop users) to create files and sub-directories on the disk:
pbmac@pbmac-server $ sudo chgrp proj2020 /home/Project2020
pbmac@pbmac-server $ sudo chmod g+w /home/Project2020e
pbmac@pbmac-server $ sudo chmod +t /home/Project2020
The last chmod +t adds the sticky bit, so that people can only delete their own files and sub-directories in a directory, even if they have write permissions to it (see man chmod).
Manually Mount
Alternatively, you may want to manually mount the drive every time you need it.
For manual mounting, use the following command:
pbmac@pbmac-server $ sudo mount /dev/sda6 /home/Project2020
When you are finished with the drive, you can unmount it using:
pbmac@pbmac-server $ sudo umount /home/Project2020
Adapted from:
"InstallingANewHardDrive" by Robert Unverzagt, The Community Help Wiki is licensed under CC BY-SA 4.0
"mkfs Command in Linux with Examples" by shivaysabharwal, Geeks for Geeks is licensed under CC BY-SA 4.0