on partitioning, formating and mounting partitions

tested on Centos v7

On Windows and DOS, we typically use letters (C:, D:, F: etc...) to identify file systems (directory heirarchy) . Linux only has one directory hierarchy and a single root. The top level is called the root dirtectory, identified by the slash symbol ("/"). In order to have multiple file system in Linux, we "mount" file systems using a directory "mount points". This may include hard disks, network drives, CD roms, floppy disk drives. Typically we mont file systems into directory /mnt.

Subjects:

  • fdisk - command to creation and manipulate partition tables.
  • mkfs - how to format a partition
  • mount and umount partitions
  • mount pernamently - adding entry in /etc/fstab file
  • /proc/partitions file - partition block allocation information.
  • to change attributes of partition
  • blkid - display information about available block devices.
  • lsblk - information about all or the specified block devices.
  • df - Show information about the file system


fdisk command to creation and manipulate partition tables.

fdisk is a menu driven program for creation and manipulation of partition tables. It uses DOS type partition tables. Linux allows only 4 primary partitions. You can have a much larger number of logical partitions by sub-dividing one of the primary partitions. Only one of the primary partitions can be sub-divided.

If you are sing Gnome GUI, there is a graphical tool under Applications > Utilities > Disks. This is easier than command line command, fdisk and more intuitive.

> fdisk -l

Disk /dev/sda: 80.0 GB, 80026361856 bytes, 156301488 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00020769

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   156301311    77637632   8e  Linux LVM

Disk /dev/mapper/centos-root: 50.6 GB, 50579111936 bytes, 98787328 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 4160 MB, 4160749568 bytes, 8126464 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-home: 24.7 GB, 24691867648 bytes, 48226304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


What can we learn from the above fdisk listing?

  • we have a 80 Gig Hard Disk
  • we have twp partitions called /dev/sda1 and /dev/sda2
  • we are booting from /dev/sda1
  • we have three logical drives called centos-root, centos-swap, and centos-home

running fdisk

> fdisk --help
fdisk: invalid option -- '-'
Usage:
 fdisk [options]     change partition table
 fdisk [options] -l  list partition table(s)
 fdisk -s       give partition size(s) in blocks

Options:
 -b              sector size (512, 1024, 2048 or 4096)
 -c[=]           compatible mode: 'dos' or 'nondos' (default)
 -h                    print this help text
 -u[=]           display units: 'cylinders' or 'sectors' (default)
 -v                    print program version
 -C            specify the number of cylinders
 -H            specify the number of heads
 -S            specify the number of sectors per track


running fdisk for a specific disk

 
>  fdisk /dev/sda2
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x0bff3280.

Command (m for help): m


You can request help within fdisk.

> m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)


It is interesting to note all the available partition types in Linux.

Command (m for help): l

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT
1e  Hidden W95 FAT1 80  Old Minix


to create a new partition

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1):


mkfs - how to format a partition

# mkfs.xfs
no device name given in argument list
Usage: mkfs.xfs
/* blocksize */         [-b log=n|size=num]
/* metadata */          [-m crc=0|1,finobt=0|1]
/* data subvol */       [-d agcount=n,agsize=n,file,name=xxx,size=num,
                            (sunit=value,swidth=value|su=num,sw=num|noalign),
                            sectlog=n|sectsize=num
/* force overwrite */   [-f]
/* inode size */        [-i log=n|perblock=n|size=num,maxpct=n,attr=0|1|2,
                            projid32bit=0|1]
/* no discard */        [-K]
/* log subvol */        [-l agnum=n,internal,size=num,logdev=xxx,version=n
                            sunit=value|su=num,sectlog=n|sectsize=num,
                            lazy-count=0|1]
/* label */             [-L label (maximum 12 characters)]
/* naming */            [-n log=n|size=num,version=2|ci,ftype=0|1]
/* no-op info only */   [-N]
/* prototype file */    [-p fname]
/* quiet */             [-q]
/* realtime subvol */   [-r extsize=num,size=num,rtdev=xxx]
/* sectorsize */        [-s log=n|size=num]
/* version */           [-V]
                        devicename
 is required unless -d name=xxx is given.
 is xxx (bytes), xxxs (sectors), xxxb (fs blocks), xxxk (xxx KiB),
      xxxm (xxx MiB), xxxg (xxx GiB), xxxt (xxx TiB) or xxxp (xxx PiB).
 is xxx (512 byte blocks).

To format a partition with mkfs.xfs

mkfs.xfs -L data /dev/sdb2


mount and umount partitions

The mount command mounts a storage device or filesystem located on a device, making it accessible and attaching it to an existing directory structure. The umount command "unmounts" a mounted filesystem, informing the system to complete any pending read or write operations, and safely detaching it.

The mount and unmound commands are only good until system is rebooted. See next section on how to make it permanent.

you have to mount a linux partition into a directory. here we temporarily mount /dev/sbd2 into directory mnt.

mount /dev/sbd2 /mnt
umount /dev/sbd2

Options in mount command...

umount  -help

Usage:
 umount [-hV]
 umount -a [options]
 umount [options]  | 

Options:
 -a, --all               unmount all filesystems
 -A, --all-targets       unmount all mountpoins for the given device
                         in the current namespace
 -c, --no-canonicalize   don't canonicalize paths
 -d, --detach-loop       if mounted loop device, also free this loop device
     --fake              dry run; skip the umount(2) syscall
 -f, --force             force unmount (in case of an unreachable NFS system)
 -i, --internal-only     don't call the umount. helpers
 -n, --no-mtab           don't write to /etc/mtab
 -l, --lazy              detach the filesystem now, and cleanup all later
 -O, --test-opts   limit the set of filesystems (use with -a)
 -R, --recursive         recursively unmount a target with all its children
 -r, --read-only         In case unmounting fails, try to remount read-only
 -t, --types       limit the set of filesystem types
 -v, --verbose           say what is being done

 -h, --help     display this help and exit
 -V, --version  output version information and exit


/etc/fstab file -

#cat /etc/fstab

# /etc/fstab
# Created by anaconda on Sat Aug  6 10:41:47 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=117ea6f0-68a1-49f0-bb96-e490e21e8128 /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
[root@localhost admin]#

UUID will not change

To add a new
LABEL=myLabel   /mnt/newMount  xfs  default

The LABEL corresponds to label identified in mkfs.xfs command.

then do
mount -a 


to change attributes of partition

umont myFolder
xfs_admin -L oldLabel newLabel  /dev/sdb2

then change etc/fstab and do mount -a



/proc/partitions file - partition block allocation information.

The /proc/partitions file contains partition block allocation information.

> cat /proc/partitions
major minor     #blocks  name

   8        0   78150744 sda
   8        1     512000 sda1
   8        2   77637632 sda2
  11        0    1048575 sr0
 253        0   49393664 dm-0
 253        1    4063232 dm-1
 253        2   24113152 dm-2

In above command the major number 8 indicates it is a disk device. The minor column indicates partitions on the same device. 0 is your entire disk, 1 is your primary, 2 your extended and 5 your logical partition. The next two columns are block size and name of disk/partition. sr0 is the CD location.





blkid - display information about available block devices.

The blkid command allows you to display information about available block devices. For each listed block device, the blkid command displays available attributes such as its universally unique identifier (UUID), file system type (TYPE), or volume label (LABEL).

# blkid
/dev/sda1: PTTYPE="dos"
/dev/sda2: UUID="mhZfne-jFt2-lgYW-Q36x-WSGm-TDcJ-edO6Ou" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="316eca67-bc34-4cae-8e15-4bfe6d5ad63e" TYPE="xfs"
/dev/mapper/centos-swap: UUID="f930a9a0-4fff-4eb3-b034-f618b8e41dc9" TYPE="swap"
/dev/mapper/centos-home: UUID="bb556983-773a-4a67-9f2c-29dc2e7bad2c" TYPE="xfs"


lsblk - information about all or the specified block devices.

lsblk lists information about all or the specified block devices. The lsblk command reads the sysfs filesystem to gather information. The command prints all block devices (except RAM disks) in a tree-like format by default. Use lsblk --help to get a list of all available columns.

# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0 74.5G  0 disk
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0   74G  0 part
  ├─centos-root 253:0    0 47.1G  0 lvm  /
  ├─centos-swap 253:1    0  3.9G  0 lvm  [SWAP]
  └─centos-home 253:2    0   23G  0 lvm  /home
sr0              11:0    1 1024M  0 rom


df - Show information about the file system

df command show information about the file system on which each FILE resides, or all file systems by default.

# df --help
Usage: df [OPTION]... [FILE]...
Show information about the file system on which each FILE resides,
or all file systems by default.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all             include dummy file systems
  -B, --block-size=SIZE  scale sizes by SIZE before printing them; e.g.,
                           '-BM' prints sizes in units of 1,048,576 bytes;
                           see SIZE format below
      --direct          show statistics for a file instead of mount point
      --total           produce a grand total
  -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G)
  -H, --si              likewise, but use powers of 1000 not 1024
  -i, --inodes          list inode information instead of block usage
  -k                    like --block-size=1K
  -l, --local           limit listing to local file systems
      --no-sync         do not invoke sync before getting usage info (default)
      --output[=FIELD_LIST]  use the output format defined by FIELD_LIST,
                               or print all fields if FIELD_LIST is omitted.
  -P, --portability     use the POSIX output format
      --sync            invoke sync before getting usage info
  -t, --type=TYPE       limit listing to file systems of type TYPE
  -T, --print-type      print file system type
  -x, --exclude-type=TYPE   limit listing to file systems not of type TYPE
  -v                    (ignored)
      --help     display this help and exit
      --version  output version information and exit

Display values are in units of the first available SIZE from --block-size,
and the DF_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).

SIZE is an integer and optional unit (example: 10M is 10*1024*1024).  Units
are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).

FIELD_LIST is a comma-separated list of columns to be included.  Valid
field names are: 'source', 'fstype', 'itotal', 'iused', 'iavail', 'ipcent',
'size', 'used', 'avail', 'pcent', 'file' and 'target' (see info page).

GNU coreutils online help: 
For complete documentation, run: info coreutils 'df invocation'

Example:

# df
Filesystem              1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root  49369548 1678848  47690700   4% /
devtmpfs                  1926668       0   1926668   0% /dev
tmpfs                     1937120       0   1937120   0% /dev/shm
tmpfs                     1937120  197056   1740064  11% /run
tmpfs                     1937120       0   1937120   0% /sys/fs/cgroup
/dev/mapper/centos-home  24101380   64792  24036588   1% /home
/dev/sda1                  508588  190136    318452  38% /boot
tmpfs                      387424       0    387424   0% /run/user/1000