Linux system disk management .md

1. Disk basic overview

1.1Linux disk naming

Naming the Linux disk and disk interfaces related to the following rules:

Traditional IDE interface hard disk: / dev / HD [AZ]
SCISI interface hard disk: / dev / sd [az]
virtual hard disk: / dev / vd [az]
as the device name in the definition rules, and so can other partitions
system hard disk name of a SCSI interface to / dev / sda
system name of the second disk SCSI interface is / dev / sdb
system partition represented by the digits, 1-4 and the left primary partitions using extended partition, logical partitions from 5 onwards


//第一块磁盘
/dev/sda
//第一块磁盘的第一个分区
/dev/sda1
//第二块磁盘的第一个分区
/dev/sdb1

1.2MBR with GPT

Before using a new disk, you must be partitioned. MBR (Master Boot Record) and GPT (GUID Partition Table) are two different ways to store partition information on the disk. The partition information contains information partition from where to start, so the operating system to know which sector is which partition belongs, and which partition is bootable. When you create a partition on the disk, you have to choose between MBR and GPT

Master Boot Record MBR is short, i.e. the master boot record, the disk is located at the most front section of the guide (Loader) code is mainly used to load the operating system boot and boot

Features:

Support up to 2TB MBR disk, it can not handle disks larger than 2TB capacity
only supports up to four primary partitions. If you want more partitions, you need to create an extended partition, and in which to create logical partitions

GPT disks is the use of disk GUID partition table, GUID partition table (GUID Partition Table, abbreviation: GPT) which means "globally unique identifier partition table" is the standard structure and layout of a physical hard disk partition table

Features:

GPT is no limit on the size of the disk
can create up to 128 partitions
Note: the mutual conversion between MBR and GPT will lead to data loss

2. Check the disk capacity

Use the df command to view disk capacity, without parameters k units:


df -i   //查看inode使用情况
df -h   //以G或者T或者M人性化方式显示
df -T   //查看文件类型

//使用df命令查看磁盘,下面分别介绍每列什么含义
[root@liping ~]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root xfs 17G 1.1G 16G 7% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 8.7M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 143M 872M 15% /boot
tmpfs tmpfs 378M 0 378M 0% /run/user/0

Use lsblk check the partitions:

[root@liping ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk 
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part 
  ├─rhel-root 253:0 0 17G 0 lvm /
  └─rhel-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk 
sdc 8:32 0 20G 0 disk 
sdd 8:48 0 20G 0 disk 
sr0 11:0 1 3.8G 0 rom  

Use the du command to see the capacity of the directory or file, without parameters k units:

du -sh opt  //人性化输出显示大小
-s:列出总和
-h:人性化显示容量信息
[root@liping ~]# du -sh /etc
31M /etc

3. Partition

Before partition, you need to give the virtual machine to add a disk to do the follow-up experiments vmware virtual machine to us, please proceed as follows:

1. Locate the corresponding virtual host, right click, select Settings
2. Click the Add button inside the Hardware Wizard, select the "hard" in hardware type, click Next
3. Select the default disk type, and then create a new virtual disk, resize (not check the allocation of space immediately)
4. Finally, click Next, finish to

Partitioning tools fdisk and gdisk , when the hard disk is smaller than 2T we should use fdisk to partition, and when the hard drive is larger than 2T application gdisk to partition

3.1 fdisk partition

[root@localhost ~]# fdisk  /dev/sdb
Command (m for help): m //输入m列出常用的命令
Command action
   a   toggle a bootable flag   //切换分区启动标记
   b   edit bsd disklabel     //编辑sdb磁盘标签
   c   toggle the dos compatibility flag    //切换dos兼容模式
   d   delete a partition     //删除分区
   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 //创建新的Sun磁盘标签
   t   change a partition's system id   //修改分区ID,可以通过l查看id
   u   change display/entry units       //修改容量单位,磁柱或扇区
   v   verify the partition table       //检验分区表
   w   write table to disk and exit     //保存退出
   x   extra functionality (experts only)   //拓展功能
1P1E6L
[root@liping ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0x8a096506 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n
Partition type:
   p primary (0 primary, 0 extended, 4 free)
   e extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-41943039,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+100M
分区 1 已设置为 Linux 类型,大小设为 100 MiB

命令(输入 m 获取帮助):n
Partition type:
   p primary (1 primary, 0 extended, 3 free)
   e extended
Select (default p): e
分区号 (2-4,默认 2):
起始 扇区 (206848-41943039,默认为 206848):
将使用默认值 206848
Last 扇区, +扇区 or +size{K,M,G} (206848-41943039,默认为 41943039):
将使用默认值 41943039
分区 2 已设置为 Extended 类型,大小设为 19.9 GiB

命令(输入 m 获取帮助):n
Partition type:
   p primary (1 primary, 1 extended, 2 free)
   l logical (numbered from 5)
Select (default p): l
添加逻辑分区 5
起始 扇区 (208896-41943039,默认为 208896):
将使用默认值 208896
Last 扇区, +扇区 or +size{K,M,G} (208896-41943039,默认为 41943039):+2G
分区 5 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):n
Partition type:
   p primary (1 primary, 1 extended, 2 free)
   l logical (numbered from 5)
Select (default p): l
添加逻辑分区 6
起始 扇区 (4405248-41943039,默认为 4405248):
将使用默认值 4405248
Last 扇区, +扇区 or +size{K,M,G} (4405248-41943039,默认为 41943039):+2G
分区 6 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):n
Partition type:
   p primary (1 primary, 1 extended, 2 free)
   l logical (numbered from 5)
Select (default p): l
添加逻辑分区 7
起始 扇区 (8601600-41943039,默认为 8601600):
将使用默认值 8601600
Last 扇区, +扇区 or +size{K,M,G} (8601600-41943039,默认为 41943039):+2G
分区 7 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):n
Partition type:
   p primary (1 primary, 1 extended, 2 free)
   l logical (numbered from 5)
Select (default p): l
添加逻辑分区 8
起始 扇区 (12797952-41943039,默认为 12797952):
将使用默认值 12797952
Last 扇区, +扇区 or +size{K,M,G} (12797952-41943039,默认为 41943039):+2G
分区 8 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):n
Partition type:
   p primary (1 primary, 1 extended, 2 free)
   l logical (numbered from 5)
Select (default p): l
添加逻辑分区 9
起始 扇区 (16994304-41943039,默认为 16994304):
将使用默认值 16994304
Last 扇区, +扇区 or +size{K,M,G} (16994304-41943039,默认为 41943039):+2G
分区 9 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):n
Partition type:
   p primary (1 primary, 1 extended, 2 free)
   l logical (numbered from 5)
Select (default p): l
添加逻辑分区 10
起始 扇区 (21190656-41943039,默认为 21190656):
将使用默认值 21190656
Last 扇区, +扇区 or +size{K,M,G} (21190656-41943039,默认为 41943039):+2g
不支持的后缀:“g”。
支持:10^N: KB (千字节), MB (兆字节), GB (吉字节)
            2^N: K (约千字节), M (约兆字节), G (约吉字节)
Last 扇区, +扇区 or +size{K,M,G} (21190656-41943039,默认为 41943039):+2G
分区 10 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x8a096506

   设备 Boot Start End Blocks Id System
/dev/sdb1 2048 206847 102400 83 Linux
/dev/sdb2 206848 41943039 20868096 5 Extended
/dev/sdb5 208896 4403199 2097152 83 Linux
/dev/sdb6 4405248 8599551 2097152 83 Linux
/dev/sdb7 8601600 12795903 2097152 83 Linux
/dev/sdb8 12797952 16992255 2097152 83 Linux
/dev/sdb9 16994304 21188607 2097152 83 Linux
/dev/sdb10 21190656 25384959 2097152 83 Linux

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@liping ~]# partprobe /dev/sdb
[root@liping ~]# lsblk 
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk 
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part 
  ├─rhel-root 253:0 0 17G 0 lvm /
  └─rhel-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk 
├─sdb1 8:17 0 100M 0 part 
├─sdb2 8:18 0 1K 0 part 
├─sdb5 8:21 0 2G 0 part 
├─sdb6 8:22 0 2G 0 part 
├─sdb7 8:23 0 2G 0 part 
├─sdb8 8:24 0 2G 0 part 
├─sdb9 8:25 0 2G 0 part 
└─sdb10 8:26 0 2G 0 part 
sdc 8:32 0 20G 0 disk 
sdd 8:48 0 20G 0 disk 

3.2 gdisk partition

[root@liping ~]# gdisk /dev/sdc
GPT fdisk (gdisk) version 0.8.6

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +500M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): n
Partition number (2-128, default 2): 
First sector (34-41943006, default = 1026048) or {+-}size{KMGTP}: 
Last sector (1026048-41943006, default = 41943006) or {+-}size{KMGTP}: +500M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdc: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): A98A3D7C-7723-46AA-8E0A-2AEEF884F903
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 39894973 sectors (19.0 GiB)

Number Start (sector) End (sector) Size Code Name
   1 2048 1026047 500.0 MiB 8300 Linux filesystem
   2 1026048 2050047 500.0 MiB 8300 Linux filesystem

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.
[root@liping ~]# partprobe /dev/sdc
[root@liping ~]# lsblk 
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk 
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part 
  ├─rhel-root 253:0 0 17G 0 lvm /
  └─rhel-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk 
├─sdb1 8:17 0 100M 0 part 
├─sdb2 8:18 0 1K 0 part 
├─sdb5 8:21 0 2G 0 part 
├─sdb6 8:22 0 2G 0 part 
├─sdb7 8:23 0 2G 0 part 
├─sdb8 8:24 0 2G 0 part 
├─sdb9 8:25 0 2G 0 part 
└─sdb10 8:26 0 2G 0 part 
sdc 8:32 0 20G 0 disk 
├─sdc1 8:33 0 500M 0 part 
└─sdc2 8:34 0 500M 0 part 
sdd 8:48 0 20G 0 disk 
sr0 11:0 1 3.8G 0 rom /mnt

4. Disk Format

Mkfs command to format the disk, create a file system

//mkfs常用的选项有
-b  //设定数据区块占用空间大小,目前支持1024、2048、4096 bytes每个块。
-t  //用来指定什么类型的文件系统,可以是ext3,ext4, xfs
-i  //设定inode的大小
-N  //设定inode数量,防止Inode数量不够导致磁盘不足
-L  //预设该分区的标签label
[root@liping ~]# mkfs.
mkfs.btrfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.minix mkfs.xfs
[root@liping ~]# blkid
/dev/sda1: UUID="b9283078-2957-4615-82f6-897dac4cd6f5" TYPE="xfs" 
/dev/sda2: UUID="SKZXLA-7mfq-Mi7R-6Vi9-DuJu-WVwW-7sNZOe" TYPE="LVM2_member" 
/dev/sdc1: PARTLABEL="Linux filesystem" PARTUUID="c2517223-78ee-464b-83a6-36ab7c5f1e1f" 
/dev/sdc2: PARTLABEL="Linux filesystem" PARTUUID="862524e8-6da5-4d88-a942-0917efb218f5" 
/dev/sr0: UUID="2017-07-11-01-39-24-00" LABEL="RHEL-7.4 Server.x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/mapper/rhel-root: UUID="b2622c5c-07de-413a-8861-1728ff65674a" TYPE="xfs" 
/dev/mapper/rhel-swap: UUID="bfecd432-5935-4d8b-b2fb-795233927098" TYPE="swap" 
[root@liping ~]# 
[root@liping ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks: 
 8193, 24577, 40961, 57345, 73729

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成 

[root@liping ~]# blkid
/dev/sda1: UUID="b9283078-2957-4615-82f6-897dac4cd6f5" TYPE="xfs" 
/dev/sda2: UUID="SKZXLA-7mfq-Mi7R-6Vi9-DuJu-WVwW-7sNZOe" TYPE="LVM2_member" 
/dev/sr0: UUID="2017-07-11-01-39-24-00" LABEL="RHEL-7.4 Server.x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/mapper/rhel-root: UUID="b2622c5c-07de-413a-8861-1728ff65674a" TYPE="xfs" 
/dev/mapper/rhel-swap: UUID="bfecd432-5935-4d8b-b2fb-795233927098" TYPE="swap" 
/dev/sdb1: UUID="c57619a1-eeaf-4e1b-8c73-b77943dc1d0a" TYPE="ext4" 
/dev/sdc1: PARTLABEL="Linux filesystem" PARTUUID="c2517223-78ee-464b-83a6-36ab7c5f1e1f" 
/dev/sdc2: PARTLABEL="Linux filesystem" PARTUUID="862524e8-6da5-4d88-a942-0917efb218f5" 

5. Mount Disk

Need to create a mount point before mount the partition, mount point appears in the form of a directory
if the mount point directory to write data actually written to the partition
mount point proposal is an empty directory, not the partition does not affect the mount It uses, but hides the original file

5.1 temporary mount disk

命令:mount挂载磁盘,实质为文件系统指定访问入口
mount -t       //指定文件系统挂载分区,如ext3,ext4, xfs
mount -a       //重新读取/etc/fstab配置文件的所有分区
mount -o       //指定挂载参数
[root@liping ~]# mkdir /opt/sdb
[root@liping ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 17G 1.1G 16G 7% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.7M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 378M 0 378M 0% /run/user/0
/dev/sr0 3.8G 3.8G 0 100% /mnt
[root@liping ~]# mount /dev/sdb1 /opt/sdb
[root@liping ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 17G 1.1G 16G 7% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.7M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 378M 0 378M 0% /run/user/0
/dev/sr0 3.8G 3.8G 0 100% /mnt
/dev/sdb1 93M 1.6M 85M 2% /opt/sdb

5.2 permanent mount disk

//使用blkid命令获取各分区的UUID
[root@liping ~]# blkid |grep "sdb1"
/dev/sdb1: UUID="c57619a1-eeaf-4e1b-8c73-b77943dc1d0a" TYPE="ext4" 

// You can put the following lines to the / etc / fstab, the permanent mount, boot automatically mount

[root@liping ~]# blkid |grep "sdb1"
/dev/sdb1: UUID="c57619a1-eeaf-4e1b-8c73-b77943dc1d0a" TYPE="ext4" 
[root@liping ~]# vi /etc/fstab 


#
# /etc/fstab
# Created by anaconda on Fri Oct 11 00:36:04 2019
#
# 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/rhel-root / xfs defaults 0 0
UUID=b9283078-2957-4615-82f6-897dac4cd6f5 /boot xfs defaults 0 0
/dev/mapper/rhel-swap swap swap defaults 0 0
UUID="c57619a1-eeaf-4e1b-8c73-b77943dc1d0a" /opt/sdb ext4 defaults 0 0
[root@liping ~]# mount -a
[root@liping ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 17G 1.1G 16G 7% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.7M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 378M 0 378M 0% /run/user/0
/dev/sr0 3.8G 3.8G 0 100% /mnt
/dev/sdb1 93M 1.6M 85M 2% /opt/sdb

5.3 Uninstall disk mount

umount -lf  //强制卸载挂载
[root@liping ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 17G 1.1G 16G 7% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.7M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sdb1 93M 1.6M 85M 2% /opt/sdb
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 378M 0 378M 0% /run/user/0
[root@liping ~]# umount -lf /dev/sdb1
[root@liping ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 17G 1.1G 16G 7% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.7M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 378M 0 378M 0% /run/user/0

Guess you like

Origin www.cnblogs.com/liping0826/p/11655047.html