Disk partitioning, file system creation, mounting under Linux

Keywords: Linux Vmware CentOS Fragment

brief introduction

When you learn Linux, you need to learn partitions in Linux. Learning Linux partitioning, there are many conceptual things to learn, such as partition type, file system type, etc. Wait a minute. For me, I don't like this kind of conceptual thing very much. It's not at all friendly for me to come up with a large paragraph of text. So I organized a note to record how to partition disks in Linux, create file systems and mount them. First, it can be used practically, then slowly understand the content.

Environmental Introduction

Operating System: CentOS 6.6

Introduction to Commands

Partition command

fdisk partition

Fdisk can be used to view hard disk partitions, or fdisk can be used to partition.
Options:

- b SECTOR_SIZE: Specify the size of each partition
 - l: List partitions for the specified hard disk
 - s PARTITION: Outputs the size of the specified partition in blocks
 - u: When listing hard disk partitions, use blocks to list partition sizes instead of cylinders
 - v: Display version information

Suboptions:

p:print, showing existing partitions
 n:new, create
 d:delete, delete
 w:write, write to disk and exit
 q:quit, give up updating and quit
 m: Get help
 l: List partitioned id
 t: Adjust partition id

Parx Recognition Partition

* Options:*

- a: Notify the kernel to re-read the hard disk partition table
 - d: Delete partition table information for partitioned hard disks
 - l: List partitions
 TypeYTPE: Specify partition type
 - nr M-N: Specified partition range

Create File System Command

mkfs Create File System

Options:

- t TYPE: Specifies the file system type of the piercer
 - v: Display version information
 - c: Before creating a file system, check the partition for bad blocks

mkswap creates swap partitions

Options:

- c: Before creating a swap partition, check that there are bad blocks in the partition
 - f: Enforcement

Mount command

Mount mount partition

mount [-fnrsvw] [-t vfstype] [-o options] device dir
Options:

-t vsftype:Specify the file system type on the device to be mounted
-r : readonly,Read-only mounting
-w: read and write,Read-write mount
-n: No updates/etc/fstab
-a: Automatic mounting of all devices that support automatic mounting (defined in/etc/fstab In the file, and the mount option has the "automatic mount" function.
-L 'LABEL': Specify mounting devices with volume labels
-U 'UUID': withUUIDSpecify the device to mount
-B,--bind: Bind directories to another directory


-o options:Options for mounting file systems
async: Asynchronous mode
sync: Synchronization mode
atime/noatime:Contains directories and files
diratime/nodiratime:Directory access timestamp
auto/noauto:Whether Automount is Supported
exec/noexec:Supports running applications on file systems as processes
dev/nodev:Do you support using devices on this file system?
suid/nosuid;
remount: remount
ro: Read-only mounting
rw: Read-write mount
user/nouser:Whether Ordinary Users are allowed to mount this device
acl: Enable this file system acl function
defaults: rw,suid,dev,exec,auto,nouser,and async

umount uninstall partition

umount [-dflnrv] {dir|device}...
Options:

- a: Remove all file systems recorded in / etc/mtab;
- h: Display help;
- n: Do not store information in / etc/mtab file when uninstalling.
- r: If you can't uninstall it successfully, try to re-mount the file system in a read-only way.
- T < File System Type >: Unmount only the file system specified in the option;
- v: Display detailed information during execution;
- V: Display version information.

swapon enables swap partitions

swapon [-f] [-p priority] [-v] specialfile...
Options:

- a: Activate all switching partitions
 - p PRIORITY: Specify Priority

swapoff disables swap partitions

swapoff [-v] specialfile...
Options:

- a: Disable all swap partitions

Introduction of steps

Ordinary partitions are created and mounted

  • View hard disks and partitions
[root@sg-pc /]# lsblk
NAME                       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                         11:0    1  4.3G  0 rom  
sda                          8:0    0   20G  0 disk 
├─sda1                       8:1    0  500M  0 part /boot
└─sda2                       8:2    0 19.5G  0 part 
  ├─vg_sgpc-lv_root (dm-0) 253:0    0 17.6G  0 lvm  /
  └─vg_sgpc-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]
sdb                          8:16   0   20G  0 disk 
[root@sg-pc /]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x179a39d6

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): 
  • fdisk is used to partition the / dev/sdb hard disk into three main partitions, each of which is 5G in size, and an extended partition, 4G in size.
[root@sg-pc /]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +5G

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (655-2610, default 655): 
Using default value 655
Last cylinder, +cylinders or +size{K,M,G} (655-2610, default 2610): +5G

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (1309-2610, default 1309): 
Using default value 1309
Last cylinder, +cylinders or +size{K,M,G} (1309-2610, default 2610): +5G

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Selected partition 4
First cylinder (1963-2610, default 1963): 
Using default value 1963
Last cylinder, +cylinders or +size{K,M,G} (1963-2610, default 2610): +2610
Value out of range.
Last cylinder, +cylinders or +size{K,M,G} (1963-2610, default 2610): 
Using default value 2610

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x179a39d6

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         654     5253223+  83  Linux
/dev/sdb2             655        1308     5253255   83  Linux
/dev/sdb3            1309        1962     5253255   83  Linux
/dev/sdb4            1963        2610     5205060    5  Extended

Command (m for help): n
First cylinder (1963-2610, default 1963): 
Using default value 1963
Last cylinder, +cylinders or +size{K,M,G} (1963-2610, default 2610): +2G 

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x179a39d6

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         654     5253223+  83  Linux
/dev/sdb2             655        1308     5253255   83  Linux
/dev/sdb3            1309        1962     5253255   83  Linux
/dev/sdb4            1963        2610     5205060    5  Extended
/dev/sdb5            1963        2224     2104483+  83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
  • Load the newly partitioned partition into the hard disk partition table and view the partition status
[root@sg-pc /]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
[root@sg-pc /]# cat /proc/partitions 
major minor  #blocks  name

   8        0   20971520 sda
   8        1     512000 sda1
   8        2   20458496 sda2
   8       16   20971520 sdb
   8       17    5253223 sdb1
   8       18    5253255 sdb2
   8       19    5253255 sdb3
   8       20          1 sdb4
   8       21    2104483 sdb5
 253        0   18391040 dm-0
 253        1    2064384 dm-1
  • Create a filesystem for the partition, and select the / dev/sdb1 partition as an example
[root@sg-pc /]# mkfs -t ext4 -L 'backup' /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=backup
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
328656 inodes, 1313305 blocks
65665 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1346371584
41 block groups
32768 blocks per group, 32768 fragments per group
8016 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
  • Through the blkid command, you can view the information of the hard disk device
[root@sg-pc /]# blkid
/dev/sda1: UUID="5e605252-9e26-4ca0-b353-e3e6028bc6ca" TYPE="ext4" 
/dev/sda2: UUID="drR3Sx-Ht8g-nGFi-2vR3-CD3z-qSjY-PUXiye" TYPE="LVM2_member" 
/dev/mapper/vg_sgpc-lv_root: UUID="9ccc9289-ada6-48b8-82f1-42d2f7edba06" TYPE="ext4" 
/dev/mapper/vg_sgpc-lv_swap: UUID="9c4c6c97-b20d-4548-a249-8d98f6b400f1" TYPE="swap" 
/dev/sdb1: LABEL="backup" UUID="4a7b5acb-fc24-446d-b9e0-a14afbe171d9" TYPE="ext4" 
/dev/sdb2: LABEL="myswap" UUID="79f07b7d-c504-43c2-9e0f-87bee77d59b9" TYPE="swap" 
  • Mount the partition that has created the file system in the specified directory
[root@sg-pc /]# mkdir /backup
[root@sg-pc /]# mount /dev/sdb1 /backup/
  • Check partition mounting

    You can see that / dev/sdb1 has been mounted in the / backup directory.

Creation and mounting of switching partitions

The creation and mounting steps of the swap partition are the same as those of the ordinary partition. It also divides the partition first, then creates the file system on the partition, and then activates the swap partition. Because the swap partition is a little different from the normal partition, the partition ID of the partition needs to be adjusted first.

  • To use the / dev/sdb2 partition as a swap partition, you first need to modify the partition ID of the / dev/sdb2 partition
[root@sg-pc /]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x179a39d6

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         654     5253223+  83  Linux
/dev/sdb2             655        1308     5253255   83  Linux
/dev/sdb3            1309        1962     5253255   83  Linux
/dev/sdb4            1963        2610     5205060    5  Extended
/dev/sdb5            1963        2224     2104483+  83  Linux

Command (m for help): l

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

Command (m for help): t
Partition number (1-5): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap / Solaris)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
  • File system for creating swap partitions
[root@sg-pc /]# mkswap -L 'myswap' /dev/sdb2
Setting up swapspace version 1, size = 5253248 KiB
LABEL=myswap, UUID=fee42b41-d38f-498e-a27f-70a962734a26
  • Activate switching partitions
[root@sg-pc /]# free -m
             total       used       free     shared    buffers     cached
Mem:           996        642        354          1        112        304
-/+ buffers/cache:        224        771
Swap:         2015          0       2015
[root@sg-pc /]# swapon -a /dev/sdb2
[root@sg-pc /]# free -m
             total       used       free     shared    buffers     cached
Mem:           996        645        350          1        112        304
-/+ buffers/cache:        228        767
Swap:         7146          0       7146

Auto-mounting of partition boot-up

The partitions of the hard disk mentioned above are temporarily mounted in the system. When the system restarts, the partitions we mount will not be automatically mounted at present, and we need to perform the steps of mounting, which is very inconvenient. If we need the hard disk partition to be mounted automatically when the system restarts, we need to write the partition configuration to the file / etc/fstab.

/ Each line in the etc/fstab file represents a hard disk partition that needs to be automatically mounted at startup.
Each row of record friends consists of six fields, in the order of:
* Equipment to be mounted
* mount point
* File system type
* Mount options
Generally defaults
* Dump frequency
Refers to the frequency of backup, 0 means no backup, 1 means backup every day, 2 means backup every other day.
* Self-checking order
0 means not self-checking
1 means self-checking first, the larger the value, the lower the order of self-checking.

Example

Let's take the. / dev/sdb1 partition as an example and set it to mount the / dev/sdb1 partition to the corresponding mount point every time the machine restarts.

Add a line as shown in the / etc/fstab file to indicate that every time the machine restarts, the / dev/sdb1 partition will be mounted on the / backup mount point, and the file system type of the partition is ext4, the mount option is defautls, no backup, no self-checking.

summary

Generally speaking, hard disk partitioning and mounting under Linux are mainly three steps:
1. Partition
2. Creating a File System
3. Mount partition

Posted by jcrocker on Fri, 14 Jun 2019 17:02:43 -0700