Unix/Linux Partitions and File Systems – fdisk, mkfs, mount, switch

1 Introduction to File Systems
2 The beneficial record -s command
three Creating File Systems – Three Steps
three.1 Step 1: Create the partition the usage of fdisk
three.2 Step 2: Create the record system within the partition the use of mkfs
three.2.1 Syntax: Using mkfs to create a document device
3.2.2 Details on the use of mkfs
3.2.Three Choosing a File System Type
three.3 Step three: Mount the record gadget on an current directory using mount
3.3.1 Manual mount: Use the best mount command
three.Three.2 Details on the use of mount
3.3.Three Un-mounting a hooked up file gadget: umount
3.3.4 Running a File System Check with fsck
3.3.Five Using mount and the /etc/fstab record
three.3.6 Example of an /and many others/fstab document
three.Three.7 Some commonplace alternatives in fstab utilized by mount
four Virtual Memory – Swap Partitions
4.1 Step 1: Create the partition the use of fdisk
four.2 Step 2: Initialize (layout) the swap place the use of mkswap
four.Three Step 3: Connect the swap location to the device the usage of swapon
1 Introduction to File SystemsIndexup to index
Creating a partition the usage of fdisk actually divides up the distance on a disk. It doesn’t by using itself allow the operating device to keep files there. You first should create some infrastructure to hold content material in the partition; you have to create a File System within the partition.

A File System is a way of storing content inside a disk partition. There are many kinds of document structures, each with exceptional characteristics. Linux helps a large variety. Proprietary operating systems usually most effective help their personal styles of file structures. (Linux can get entry to Linux, Apple, and Microsoft record structures, and dozens of others. Apple can most effective get right of entry to Apple and Microsoft document structures. Microsoft can only get right of entry to Microsoft document systems – “Does no longer play well with others.”)

A file device is the way the O/S stores and retrieves facts in the partition, e.G.

Create, move and delete files and directories (contact cp mkdir rmdir)
create, remove, modify filenames (ln, rm, mv)
open files for reading and writing (vi, cat, head, tail, kind)
Search for files (find)
are seeking within a document (grep)
listing content of a listing (ls)
and so on.
The above primary features are not unusual to maximum working structures, but they are carried out and controlled otherwise from one O/S to some other and possibly even from one form of document system to some other.

2 The useful record -s commandIndexup to index
The Unix/Linux report command may be very beneficial for identifying things within the document system, together with directories, applications, pix, files, and unique documents which includes disk partitions. The -s choice is beneficial for finding out what type of record device (if any) is inner a disk partition:

# report /dev/sda1
/dev/sda1: block special

# record -s /dev/sda1
/dev/sda1: Linux rev 1.0 ext4 filesystem facts (needs magazine recuperation) (extents) (big documents) (huge documents)

# record -s /dev/sda2
/dev/sda2: Linux/i386 swap report (new style) 1 (4K pages) length 140031 pages

# document -s /dev/sda
/dev/sda: x86 boot area; GRand Unified Bootloader, stage1 version 0x3, boot drive 0x80, 1st sector stage2 0x11cb38, GRUB model zero.94; partition 1: ID=0x83, lively, starthead 32, startsector 2048, 3072000 sectors; partition 2: ID=0x82, starthead 89, startsector 3074048, 1120256 sectors, code offset 0x48
If document -s doesn’t display a legitimate document gadget internal a disk partition, then you could’t mount the file system.

Three Creating File Systems – Three StepsIndexup to index
The 3 steps to create a file machine don’t change tons from one running machine to any other, but the particular info and utilities used vary greatly. The steps are:

Prepare the device (e.G. A disk, USB key, etc.) to receive a report machine
i.E. Partition the tool into portions (Linux fdisk)
Create the record system in the partition
i.E. Format or put together the partition with the chosen file device (Linux mkfs)
optionally verify the document gadget integrity – take a look at for flaws or mistakes (Linux fsck)
Mount the brand new report machine to make it seen to the users
i.E. Join the record machine into the prevailing listing tree (Linux mount)
The trio is constantly partition, make document device, and mount, in that order. Let’s look at every in greater detail:

three.1 Step 1: Create the partition the use of fdiskIndexup to index
See slides on Linux Disks and Partitions PDF
Create, regulate or alternate partition facts at the disk
Use fdisk, Disk Druid or some other partitioning device which could control and create Linux walls
Changing a partition start or stop typically destroys some thing become in the partition – lower back up first!
Using fdisk: see http://tldp.Org/HOWTO/Partition/fdisk_partitioning.Html
You have to create walls first, before growing report systems internal them
three.2 Step 2: Create the record gadget inside the partition the use of mkfsIndexup to index
Linux organizes information inside walls the use of numerous forms of document systems. Linux helps a large number of various record system sorts, including maximum of the Windows file machine kinds. You can use mkfs to create a file device to optimize the manner facts being stored in a partition. The mkfs command is simply a front-cease for a whole set of mkfs instructions, each distinct one decided on by the -t option to mkfs.

Three.2.1 Syntax: Using mkfs to create a report systemIndexup to index
mkfs -t type [other options] device_name
common kinds:   ext3, ext4, vfat
use vfat for USB keys, floppy disks, and Microsoft compatibility
the device_name is nearly always the /dev/ name of an existing partition, not an entire disk
e.G.   Mkfs -t ext4 /dev/sda1       (first disk, first partition)
e.G.   Mkfs -t vfat /dev/fd0       (first floppy disk)
mke2fs [options] device_name
specialized model of mkfs particular to Linux ext2/ext3/ext4 document structures
used while you need more manage of the info of the report gadget (RTFM)
fsck [options] device_name
Optionally, you can take a look at a report device for inconsistencies or errors after developing it the usage of fsck. (Almost in no way done right after mkfs except you watched that your disk might also have bad blocks on it.)
three.2.2 Details on using mkfsIndexup to index
A document system need to be created interior an current drive/partition
You have to have an current partition earlier than you may create a file system.
You must have a report device created before you may mount it.
The kind defaults to the vintage ext2 type, however for modern-day Linux tough disk structures you should continually specify the kind explicitly as both ext3 or ext4. These more moderen file system kinds are journalling record structures. Do now not use the antique default ext2 record device kind, mainly on massive (over 400MB) disks.
The device_name is the pathname (normally absolute) of the existing tool or partition so as to be used, usually of the form /dev/sdXN, where X is the letter of the tool (disk) being used and N is the partition range on that tool. (Recall that partitions are created the usage of the fdisk command.)
The mkfs commands do no longer check to look what's within the partition already. They will not ask “Are You Sure?” earlier than they re-format a partition and break something changed into previously there. Be extraordinarily careful to get the device name correct! A partition destroyed by way of mkfs is not possible to get better.
The mkfs instructions do now not care approximately the System ID (type) of a partition given within the partition desk. You can create any kind of document gadget in any form of partition. (For example, you may create an ext4 report system on a partition labelled inside the partition desk as NTFS or as Swap, however that is a awful idea.)
Creating a partition with fdisk does now not routinely create any sort of report gadget in that partition.
Creating a document system with mkfs does now not robotically mount or make to be had that file system to be used in Linux. The new document gadget is not on hand. More on mounting file systems inside the subsequent section.
If record -s can’t find a record device internal a disk partition, you can't mount it – you possibly forgot to make one the use of mkfs.
Three.2.Three Choosing a File System TypeIndexup to index
Your gadget deploy in all likelihood created “journalling” record systems to your virtual disk, the usage of the -t ext4 choice to mkfs. Another manner to request this type of report system is to apply the -j (Journalling) option to the unique mke2fs command. This form of Linux journalling file system is normally referred to as ext3 or ext4, and a few distributions may additionally have a small shell script named mke3fs or mke4fs that simply calls mke2fs with the proper -t or -j choice.

Journalling file systems are more proof against corruption because of surprising energy loss, permitting the device to return lower back up extra quick through fending off an extended report machine take a look at at boot time. This does NOT come up with permission to strength off a strolling Linux device! Always shut down cleanly. The accurate command-line for a direct, secure device close down is:

# shutdown -h now               # shut down and halt (usually power off)
# shutdown -r now               # close down after which reboot
See the person page for other alternatives.

3.Three Step three: Mount the record machine on an current directory using mountIndexup to index
Linux File System
Linux File System

Unlike Windows with its multiple power letters, Linux has a unmarried-ROOTed document device tree. “Mounting” attaches an current file system located on a block device (commonly a disk partition, e.G. /dev/hda2 or /dev/sda2) to the Linux directory structure, e.G. Onto a few listing /boot. Accessing that directory, and the whole thing underneath that listing, accesses the report machine on that disk partition.

Any quantity of separate file systems (saved in disk walls) may be connected anywhere within the same Linux directory tree, resulting in one unmarried ROOTed tree to access each record on every disk. You can detach a record device from one place in the tree and attach it somewhere else, however then the pathnames to files inside that report system might change to mirror the new mount place.

File structures may be hooked up from the Unix/Linux command line using the mount command. File structures may be mounted robotically at gadget boot time via placing their names and mount factors into the /and so forth/fstab report. File structures on detachable devices (USB keys, DVDs) also can be hooked up dynamically at device insertion time using regulations in system configuration documents (“automounting”).

(Most distribution use an existing /mnt/ listing to attach file structures dynamically and briefly to the listing shape, e.G. USB keys, CDROM and DVD, floppy disks, and many others.) Desktop operating systems might also use a /media/ listing for dynamic storage devices.

You can best mount record structures, now not partitions. An empty partition (one created with fdisk but no longer initialized with mkfs) can not be installed. If document -s can’t find a file machine interior a disk partition, you can not mount it – you likely forgot to make one the use of mkfs.

3.3.1 Manual mount: Use the right mount commandIndexup to index
mount
Without any alternatives: listing all of the presently mounted gadgets and their contained file systems. This is a very commonplace command usage.
Mount -a
Mount all the report systems indexed for vehicle mounting within the /and so on/fstab file (possible for root simplest).
Mount [options]  directory_name
Mount the report gadget in /and many others/fstab that fits the given name.
You deliver either the device_name or the directory_name of the access in the fstab document. The device receives the opposite one from the fstab.
E.G.   Mount /dev/sda1       # /and many others/fstab will supply the mount factor
e.G.   Mount /home                    # /and many others/fstab will deliver the partition tool call
mount [options] device_name directory_name
Ignore /etc/fstab and mount the report gadget within the given device_name onto the given directory_name mount point.
The partition have to have a document gadget created in it (e.G. With mkfs).
The directory_name should already exist. (You can also want to create it.)
e.G.   Mount /dev/sda1 /home       # /and so on/fstab isn't used
3.Three.2 Details on the usage of mountIndexup to index
Syntax: mount [options] device_name directory_name

The device_name is the pathname (generally absolute) of the tool containing the file system, e.G. A partition call consisting of/dev/sdb9. The tool is often a partition call, now not a disk name. The partition ought to have a recognized report device created interior it already, because you're mounting the record gadget that is inside the partition, no longer the partition itself.
The directory_name is the pathname (frequently absolute) of an present Linux directory, usually an empty directory. The document device inside the partition will be installed on this existing directory mount point. If the mount fails, check to ensure the directory exists! Let me repeat that: If the mount command fails, make certain that the mount factor listing exists!
Use mkdir to create a new empty listing mount point, if wished.
You can “cowl up” a non-empty directory through mounting on top of it; this quickly hides the authentic contents of the directory and is not normally an awesome concept.
Modern mount instructions are clever enough to understand what sort of report device resides interior a partition, however not often you could have to specify the kind of record device in the partition the usage of the -t choice.
If Linux can’t automatically discern out the type, it typically way the partition has no record device created on it in any respect – you forgot to use mkfs first. Use file -s to test.
You can bypass mount-time options the usage of the -o alternative.
The alternatives are the identical alternatives you could use within the /and so on/fstab file
e.G.  Mount -o 'ro,noatime' /dev/fd0 /mnt/floppy
The person mounting the report machine (if now not root) should have get admission to rights to the mount point listing and to the mount command.
Mounting a file system onto a directory that already contains files will briefly hide those documents until the document device is unmounted again.
Examples:
mkdir /mnt/foo ; mount /dev/sda1 /mnt/foo
mkdir /mnt/dosfloppy ; mount -t vfat /dev/fd0 /mnt/dosfloppy
mkdir /mnt/foobar ; mount /dev/sdb9 /mnt/foobar
three.Three.3 Un-mounting a installed document system: umountIndexup to index
You can un-mount a record device the usage of the (badly spelled) Linux umount command and give both the tool name or the mount factor, but not both:
umount /dev/sda1
umount /domestic
To un-mount a document system, no documents ought to be open interior it and no packages must have it as a current listing.
If the message “device is busy” appears, a process is currently having access to the tool.
A not unusual mistake is to try (and fail) to unmount the file machine of the contemporary listing – you have to cd all of your shells and tactics out of the file device first.
Sometimes you need to locate and kill approaches which can be using a record system as a current listing, so you can unmount the record device.
Commands to listing files open on a file machine (use those as root, and deliver the Linux listing that is the basis of the record gadget):
fuser /boot
lsof /
3.3.Four Running a File System Check with fsckIndexup to index
If the gadget reboots and finally ends up in “repair document gadget” mode, all the record systems can be hooked up “read handiest” so you can restore them.
If you want to edit/modify/alternate any files (/etc/fstab as an example) you will need to “re-mount” that file gadget as read-write.
To transfer /dev/sda1 to examine-write mode use the “remount” alternative:
mount –o remount,rw /dev/sda1
3.Three.Five Using mount and the /etc/fstab fileIndexup to index
To ensure that a report system is installed at every boot, add the document machine to the /and so forth/fstab report and it's going to continually be set up at boot time. To learn about the layout of this record:   man 5 fstab

File systems may be indexed inside the /and many others/fstab record to automate the mounting method. Both the mount point (the present listing on which the document device is to be established) and every other options may be stored within the fstab file.

The document structures may be diagnosed in fstab in the conventional manner via partition name (e.G. /dev/sda1), that's a chunk risky considering the fact that disk names can exchange depending on what is connected in your gadget at boot time. Another manner to perceive record structures is with a Volume Label or UUID (Unique UID) identifier that is configured into the partition and that doesn’t change even though the partition call adjustments. You can see those Labels and UUIDs the usage of the blkid command. You can set Labels and UUIDs right into a partition the usage of the tune2fs command.

If a document machine is not indexed inside the /and so on/fstab report and also you want to mount it, then all mount records approximately the document gadget and all alternatives want to accept at the command line. At minimum, you want the tool name (partition) and the prevailing mount point (directory) where the record device have to be installed:

e.G.   Mount /dev/sda2 /domestic
e.G.   Mount -o 'ro' /dev/sda2 /home_readonly
If a record system is indexed inside the /and many others/fstab document, then indicating either simply the mount point (the directory name) or simply the device name (e.G. /dev/sda1) will illustrate to the mount command that the rest of the information needed for the mount should be copied from the matching entry in the /and so on/fstab document:

e.G.   Mount /dev/sda1       # gets the listing call and alternatives from /and many others/fstab
e.G.   Mount /home                 # gets the tool call and options from /and many others/fstab
3.Three.6 Example of an /etc/fstab fileIndexup to index
Lines in /and so on/fstab starting with # are comments and are unnoticed:

# Sample /and many others/fstab taken from Linux set up with two disk walls
# 1                     2                       three       4               5 6
# DEVICE                MOUNT POINT             TYPE    OPTIONS    BACKUP FSCK
UUID=dd2e3867-0f87-4fe9-9195-d60ad7b28897 /     ext4    defaults   1      1
UUID=16dfd36c-7422-43ac-89eb-5132488274d7 change  switch    defaults   zero      zero
tmpfs                   /dev/shm                tmpfs   defaults   zero      zero
devpts                  /dev/pts                devpts  gid=5,mode=620 0  zero
sysfs                   /sys                    sysfs   defaults   0      0
proc                    /proc                   proc    defaults   0      zero
# 1                     2                       three       4          five      6
Each line in /and so forth/fstab has six fields that describes a document device:

Field 1: Identifies the device (partition) to be mounted
historically, this a partition call under /dev/ containing a record gadget, e.G. /dev/sda1
may be a LABEL or a UUID: Above, each ROOT / and change are established via UUID instead of with the aid of device name
we mount document structures with the aid of partition name, but the partition truely does need to have a document gadget created inner it for the mount to work, and the type subject has to fit the record gadget that is in the partition
Field 2: Names the present mount point directory in which that tool is to be mounted
this listing is known as the mount factor and have to exist already
normally, the listing should be empty (but it doesn’t must be)
you can “cowl up” a non-empty listing via mounting on top of it; now not usually a very good concept
Field 3: Names the kind of document gadget inside the partition being mounted
current Unix/Linux structures can parent this out robotically for maximum types of file structures in case you use the key-word vehicle here
auto is the exceptional idea, due to the fact that if you’re wrong approximately the kind, the system gained’t boot well
many kinds in fstab refer to pseudo-record-systems that don’t have real device names, e.G. Tmpfs, devpts, sysfs, proc, and once in a while the device call is given as actually none
Field four: Optional mount alternatives as a comma-separated list (no spaces!)
useful alternatives (see under): noauto, person
the key-word defaults uses some default alternatives that rely on the document machine type – RTFM
Field five: Which document systems to lower back up
A non-0 value suggests backup priority/order
A zero manner “don’t again up this record machine”
Field 6: The order wherein record gadget tests are executed at boot time
The fsck application uses this to run file machine tests so as
The root need to continually be checked first – price 1
Use price 2 or larger for any others that need to be checked
No price or a value of 0 means no document machine test
File systems do not need to be indexed inside the /etc/fstab file to be mounted – you may usually mount whatever manually from the command line. The fstab is only for matters which you want set up when the machine boots.

Having a report machine indexed within the fstab manner you most effective want to specify the tool call or the mount point to the mount command; the alternative statistics for the mount could be read from the /and many others/fstab document:

# grep foo /etc/fstab
/dev/sdb1 /mnt/foo ext4 defaults,noauto 1 2
# mount /dev/sdb1              # machine receives /mnt/foo mount factor from fstab
*** OR ***
# mount /mnt/foo               # system receives /dev/sdb1 tool from fstab
*** OR ***
# mount /dev/sdb1 /mnt/bar     # forget about fstab and do express mount
three.3.7 Some common options in fstab utilized by mountIndexup to index
automobile – may be mounted whilst mount is used with the -a choice (and so mounted at boot time) (default)
noauto – might be omitted through the -a choice (now not hooked up at boot time)
exec – allow execution of binaries (default)
ro – mount the document machine examine-simplest (read, execute, no writing)
rw – mount the document system study-write (default)
person – allow a non-root user mount and unmount this file device
nouser – simplest root can mount and unmount this file machine (default)
defaults – use default options:   auto, exec, rw, nouser
Defaults may range with distribution! Be cautious!
4 Virtual Memory – Swap PartitionsIndexup to index
Another use for disk walls is to maintain memory pages used to put in force Unix/Linux Virtual Memory. These are referred to as swap walls or the change location, and that they, too need a few minimum shape created to be used by the machine. Usually, you discover most effective one switch partition in a gadget, but systems with a whole lot of memory may additionally have more.

Again, there are the same old 3 steps: Partition, Create, and Connect

4.1 Step 1: Create the partition using fdiskIndexup to index
Same as creating some other kind of partition – use fdisk
Set the partition type ID to be Linux Swap to report that that is a switch partition. Many structures will robotically join and use change walls in the event that they have the ideal kind ID.
4.2 Step 2: Initialize (format) the change location the usage of mkswapIndexup to index
Make a Linux partition into a change partition
mkswap [options] device_name
e.G.   Mkswap /dev/sdb9
rarely want any alternatives
This destroys anything else become in the partition!
You don’t get asked to verify – kind it proper the primary time
The device_name is the pathname (commonly absolute) of the existing switch partition as a way to be used. These instructions do no longer care approximately the System ID (kind) of a partition given in the partition desk. You can initialize as change any kind of partition. Most Linux structures at boot time will robotically use as swap all walls that have a form of “Linux switch”, if they may be initialized as swap. You can add greater swap area to a jogging system using the above  commands.

To show the currently energetic switch partitions, omit the device_name and use simplest the -s or --precis option to the swapon command:

Display currently energetic switch walls: swapon -s
four.3 Step 3: Connect the swap region to the system the usage of swaponIndexup to index
Use swapon to prompt the switch location you just created with mkswap
swapon device_name
e.G.   Swapon /dev/sdb9
To list the presently active change regions:
swapon -s         (list all presently active change regions)
Use swapoff to de-spark off (stop the usage of) a switch vicinity:
swapoff device_name
e.G.   Swapoff /dev/sdb9
de-activates that switch partition and permits the partition to be re-formatted and used for some thing else
the -a alternative will disconnect all switch areas (now not commonly an excellent concept)
At boot time, partitions in /etc/fstab marked with kind change can be routinely connected and used as switch regions
You can manually ask that all fstab change partitions be related the use of:
swapon -a       (connect and enable all acknowledged swap partitions)
the swapoff command has an equal option to disconnect all active swap regions