Linux中磁盘命名方式
Linux中磁盘的命名方式与磁盘的接口有关,规则如下:
- 传统IDE接口硬盘:
/dev/hd[a-z]
- SCISI接口硬盘:
/dev/sd[a-z]
- 虚拟化硬盘:
/dev/vd[a-z]
在设备名称的定义规则如下, 其他的分区可以以此类推
系统的第一块SCSI接口的硬盘名称为/dev/sda
系统的第二块SCSI接口的硬盘名称为/dev/sdb
系统中分区由数字编号表示, 1-4留给主分区使用和扩展分区, 逻辑分区从5开始
//第一块磁盘
/dev/sda
//第一块磁盘的第一个分区
/dev/sda1
//第二块磁盘的第一个分区
/dev/sdb1
小拓展:请问第四块IDE接口硬盘的第三个逻辑分区名称是什么?
答:hdd7
MBR与GPT
在使用新磁盘之前,你必须对其进行分区。MBR和GPT是在磁盘上存储分区信息的两种不同方式。这些分区信息包含了分区从哪里开始的信息,这样操作系统才知道哪个扇区是属于哪个分区的,以及哪个分区是可以启动的。在磁盘上创建分区时,你必须在MBR和GPT之间做出选择:
MBR是Master Boot Record的简称,也就是主引导记录,是位于磁盘最前边的一段引导(Loader)代码,主要用来引导操作系统的加载与启动
特点:
- MBR支持最大2TB磁盘,它无法处理大于2TB容量的磁盘
- 只支持最多4个主分区。若想要更多分区,需要创建扩展分区,并在其中创建逻辑分区
GPT磁盘是指使用GUID分区表的磁盘,GUID磁盘分区表(GUID Partition Table,缩写:GPT)其含义为“全局唯一标识磁盘分区表”,是一个实体硬盘的分区表的结构布局的标准
特点:
- GPT对磁盘大小没有限制
- 最多可以创建128个分区
注意:MBR与GPT之间互相转换会导致数据丢失
磁盘容量检查
df 报告文件系统磁盘空间的使用情况
//-h 以人类友好的方式显示
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 379M 0 379M 0% /dev
tmpfs 396M 0 396M 0% /dev/shm
tmpfs 396M 5.6M 391M 2% /run
tmpfs 396M 0 396M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 1.7G 49G 4% /
/dev/mapper/rhel-home 46G 354M 45G 1% /home
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 80M 0 80M 0% /run/user/0
//-T 查看文件类型
[root@localhost ~]# df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
devtmpfs devtmpfs 387440 0 387440 0% /dev
tmpfs tmpfs 405244 0 405244 0% /dev/shm
tmpfs tmpfs 405244 5668 399576 2% /run
tmpfs tmpfs 405244 0 405244 0% /sys/fs/cgroup
/dev/mapper/rhel-root xfs 52403200 1709576 50693624 4% /
/dev/mapper/rhel-home xfs 47212008 362212 46849796 1% /home
/dev/sda1 xfs 1038336 182860 855476 18% /boot
tmpfs tmpfs 81048 0 81048 0% /run/user/0
//-i 查看磁盘块inode使用情况
[root@localhost ~]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
devtmpfs 96860 368 96492 1% /dev
tmpfs 101311 1 101310 1% /dev/shm
tmpfs 101311 556 100755 1% /run
tmpfs 101311 17 101294 1% /sys/fs/cgroup
/dev/mapper/rhel-root 26214400 33435 26180965 1% /
/dev/mapper/rhel-home 23617536 3 23617533 1% /home
/dev/sda1 524288 301 523987 1% /boot
tmpfs 101311 5 101306 1% /run/user/0
df命令查看磁盘,下面分别介绍每列什么含义:
列名 | 含义 |
---|---|
Filesystem | 设备名称 |
Size | 磁盘大小 |
Used | 已用大小 |
Avail | 可用大小 |
Use% | 使用百分比 |
Mounted on | 挂载点 |
lsblk 查看分区情况
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part
├─rhel-root 253:0 0 50G 0 lvm /
├─rhel-swap 253:1 0 4G 0 lvm [SWAP]
└─rhel-home 253:2 0 45G 0 lvm /home
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 7.9G 0 rom
lsblk命令查看分区情况,下面分别介绍每列什么含义:
列名 | 含义 |
---|---|
NAME | 设备名称 |
MAJ | 主要设备号 |
MIN | 次要设备号,0表示磁盘本身,1表示第一个分区,每一个硬盘能用16个分区 |
RM | 表明设备是否可移动设备,1表示是,0表示否 |
SIZE | 设备的容量大小信息 |
RO | 表明设备是否为只读,1表示是,0表示否 |
TYPE | 显示块设备是否是磁盘或磁盘上的一个分区 |
MOUNTPOINT | 设备挂载的挂载点 |
磁盘分区
分区之前, 需要先给虚拟机添加磁盘
分区工具有fdisk和gdisk,当硬盘小于2T的时候我们应该用fdisk来分区,而当硬盘大于2T的时候则应用gdisk来进行分区
fdisk分区
//-l 查看系统磁盘信息
[root@localhost ~]# fdisk -l
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: dos
Disk identifier: 0x732e9f62
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 209715199 207616000 99G 8e Linux LVM
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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/sdc: 20 GiB, 21474836480 bytes, 41943040 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/sdd: 20 GiB, 21474836480 bytes, 41943040 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/rhel-root: 50 GiB, 53687091200 bytes, 104857600 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/rhel-swap: 4 GiB, 4240441344 bytes, 8282112 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/rhel-home: 45 GiB, 48368713728 bytes, 94470144 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
给sdb进行fdisk分区
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.32.1).
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.
Created a new DOS disklabel with disk identifier 0x572ffbac.
Command (m for help): m //输入m列出常用的命令
Help:
DOS (MBR)
a toggle a bootable flag //切换分区启动标记
b edit nested BSD disklabel //编辑sdb磁盘标签
c toggle the dos compatibility flag //切换dos兼容模式
Generic
d delete a partition //删除分区
F list free unpartitioned space //列出可用的未分区空间
l list known partition types //列出已知分区类型
n add a new partition //新建分区
p print the partition table //打印分区表
t change a partition type //更改分区类型
v verify the partition table //检验分区表
i print information about a partition //打印有关分区的信息
Misc
m print this menu //显示帮助菜单
u change display/entry units //修改容量单位,磁柱或扇区
x extra functionality (experts only) //拓展功能
Script
I load disk layout from sfdisk script file //从sfdisk脚本文件加载磁盘布局
O dump disk layout to sfdisk script file //转储到磁盘布局脚本
Save & Exit
w write table to disk and exit //保存退出
q quit without saving changes //不保存退出
Create a new label
g create a new empty GPT partition table //创建一个新的空GPT分区表
G create a new empty SGI (IRIX) partition table //创建一个新的空SGI(IRIX)分区表
o create a new empty DOS partition table //新建一个空的DOS分区表
s create a new empty Sun partition table //创建一个新的空Sun分区表
//创建主分区
Command (m for help): n //新建分区
Partition type
p primary (0 primary, 0 extended, 4 free) //主分区
e extended (container for logical partitions) //扩展分区
Select (default p): p //选择分区,默认主分区
Partition number (1-4, default 1): //选择分区号,默认1
First sector (2048-41943039, default 2048): //开始扇区,默认2048
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +200M
//结束扇区,如果要分配200M,直接输入+200M
Created a new partition 1 of type 'Linux' and of size 200 MiB.
//查看分区创建
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x572ffbac
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 411647 409600 200M 83 Linux
//新建扩展分区
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): e
Partition number (2-4, default 2):
First sector (411648-41943039, default 411648):
Last sector, +sectors or +size{K,M,G,T,P} (411648-41943039, default 41943039):
//直接回车,默认全部分配给扩展分区
Created a new partition 2 of type 'Extended' and of size 19.8 GiB.
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x572ffbac
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 411647 409600 200M 83 Linux
/dev/sdb2 411648 41943039 41531392 19.8G 5 Extended
//新建逻辑分区
Command (m for help): n
All space for primary partitions is in use. //由于没有空间分给主分区了,默认是逻辑分区
Adding logical partition 5
First sector (413696-41943039, default 413696):
Last sector, +sectors or +size{K,M,G,T,P} (413696-41943039, default 41943039): +500M
Created a new partition 5 of type 'Linux' and of size 500 MiB.
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x572ffbac
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 411647 409600 200M 83 Linux
/dev/sdb2 411648 41943039 41531392 19.8G 5 Extended
/dev/sdb5 413696 1437695 1024000 500M 83 Linux
//保存退出
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
//刷新分区表
[root@localhost ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part
├─rhel-root 253:0 0 50G 0 lvm /
├─rhel-swap 253:1 0 4G 0 lvm [SWAP]
└─rhel-home 253:2 0 45G 0 lvm /home
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 200M 0 part
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 500M 0 part
sdc 8:32 0 20G 0 disk
sdd 8:48 0 20G 0 disk
sr0 11:0 1 7.9G 0 rom
小扩展:当我们使用fidsk给sdb(20G)分区后,主分区分了200M,剩余的全给了扩展分区,逻辑分区分了500M,请问还能用fdisk给sdb分主分区和扩展分区吗?
答:不能分主分区和扩展分区,因为剩余的全给了扩展分区,已经没有空间在分给主分区和扩展分区了,但是能在扩展分区下分逻辑分区。
gdisk分区(需要安装)
//安装gdisk工具
[root@localhost ~]# yum -y install gdisk
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Last metadata expiration check: 0:00:23 ago on Thu 03 Dec 2020 05:29:08 PM CST.
Dependencies resolved.
===========================================================================================================
Package Architecture Version Repository Size
===========================================================================================================
Installing:
gdisk x86_64 1.0.3-6.el8 BaseOS 239 k
Transaction Summary
===========================================================================================================
Install 1 Package
Total size: 239 k
Installed size: 840 k
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : gdisk-1.0.3-6.el8.x86_64 1/1
Running scriptlet: gdisk-1.0.3-6.el8.x86_64 1/1
Verifying : gdisk-1.0.3-6.el8.x86_64 1/1
Installed products updated.
Installed:
gdisk-1.0.3-6.el8.x86_64
Complete!
给sdc进行gdisk分区
[root@localhost ~]# gdisk /dev/sdc
GPT fdisk (gdisk) version 1.0.3
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries.
Command (? for help): ?
b back up GPT data to a file
c change a partition's name
d delete a partition
i show detailed information on a partition
l list known partition types
n add a new partition
o create a new empty GUID partition table (GPT)
p print the partition table
q quit without saving changes
r recovery and transformation options (experts only)
s sort partitions
t change a partition's type code
v verify disk
w write table to disk and exit
x extra functionality (experts only)
? print this menu
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): L
0700 Microsoft basic data 0c01 Microsoft reserved 2700 Windows RE
3000 ONIE boot 3001 ONIE config 3900 Plan 9
4100 PowerPC PReP boot 4200 Windows LDM data 4201 Windows LDM metadata
4202 Windows Storage Spac 7501 IBM GPFS 7f00 ChromeOS kernel
7f01 ChromeOS root 7f02 ChromeOS reserved 8200 Linux swap
8300 Linux filesystem 8301 Linux reserved 8302 Linux /home
8303 Linux x86 root (/) 8304 Linux x86-64 root (/ 8305 Linux ARM64 root (/)
8306 Linux /srv 8307 Linux ARM32 root (/) 8400 Intel Rapid Start
8e00 Linux LVM a000 Android bootloader a001 Android bootloader 2
a002 Android boot a003 Android recovery a004 Android misc
a005 Android metadata a006 Android system a007 Android cache
a008 Android data a009 Android persistent a00a Android factory
a00b Android fastboot/ter a00c Android OEM a500 FreeBSD disklabel
a501 FreeBSD boot a502 FreeBSD swap a503 FreeBSD UFS
a504 FreeBSD ZFS a505 FreeBSD Vinum/RAID a580 Midnight BSD data
a581 Midnight BSD boot a582 Midnight BSD swap a583 Midnight BSD UFS
a584 Midnight BSD ZFS a585 Midnight BSD Vinum a600 OpenBSD disklabel
a800 Apple UFS a901 NetBSD swap a902 NetBSD FFS
a903 NetBSD LFS a904 NetBSD concatenated a905 NetBSD encrypted
a906 NetBSD RAID ab00 Recovery HD af00 Apple HFS/HFS+
af01 Apple RAID af02 Apple RAID offline af03 Apple label
Press the <Enter> key to see more codes:
af04 AppleTV recovery af05 Apple Core Storage af06 Apple SoftRAID Statu
af07 Apple SoftRAID Scrat af08 Apple SoftRAID Volum af09 Apple SoftRAID Cache
b300 QNX6 Power-Safe bc00 Acronis Secure Zone be00 Solaris boot
bf00 Solaris root bf01 Solaris /usr & Mac Z bf02 Solaris swap
bf03 Solaris backup bf04 Solaris /var bf05 Solaris /home
bf06 Solaris alternate se bf07 Solaris Reserved 1 bf08 Solaris Reserved 2
bf09 Solaris Reserved 3 bf0a Solaris Reserved 4 bf0b Solaris Reserved 5
c001 HP-UX data c002 HP-UX service e100 ONIE boot
e101 ONIE config ea00 Freedesktop $BOOT eb00 Haiku BFS
ed00 Sony system partitio ed01 Lenovo system partit ef00 EFI System
ef01 MBR partition scheme ef02 BIOS boot partition f800 Ceph OSD
f801 Ceph dm-crypt OSD f802 Ceph journal f803 Ceph dm-crypt journa
f804 Ceph disk in creatio f805 Ceph dm-crypt disk i fb00 VMWare VMFS
fb01 VMWare reserved fc00 VMWare kcore crash p fd00 Linux RAID
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
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): B0E52D19-968D-414A-9A7E-AFF88E812881
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 40918973 sectors (19.5 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 1026047 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@localhost ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part
├─rhel-root 253:0 0 50G 0 lvm /
├─rhel-swap 253:1 0 4G 0 lvm [SWAP]
└─rhel-home 253:2 0 45G 0 lvm /home
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 200M 0 part
├─sdb2 8:18 0 512B 0 part
└─sdb5 8:21 0 500M 0 part
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 500M 0 part
sdd 8:48 0 20G 0 disk
sr0 11:0 1 7.9G 0 rom
小扩展:当我们使用fidsk给sdb(20G)分区后,只用了700M还剩19.3G,请问能在用gfidk给sdb分区吗?
答:不能,因为用fdisk给sdb分区后格式为MBR,再用gdisk分区会使格式变为GPT,MBR与GPT之间互相转换会导致数据丢失。
磁盘格式化与挂载
mkfs (make file system) 用于在特定的分区上建立Linux文件系统
文件格式:
- NTFS 单个文件的大小能够超过4G(Windows用的比较多)
- fat32 单个文件的大小不能够超过4G
- exfat 拥有NTFS格式的优点,同时可以跨平台 (U盘、移动硬盘最好用这个)
- ext2
- ext3
- ext4 第四代扩展文件系统(Linux用的比较多)
- xfs 高性能的日志文件系统(Linux用的比较多)
//可以用mkfs把磁盘格式化成这些格式
[root@localhost ~]# mkfs.
mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.minix mkfs.xfs
//格式化sdb1格式化为ext4文件系统
[root@localhost ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=12800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=51200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=1368, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mkdir /resource1
//挂载sdb1到/resource1
[root@localhost ~]# mount /dev/sdb1 /resource1
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 379M 0 379M 0% /dev
tmpfs tmpfs 396M 0 396M 0% /dev/shm
tmpfs tmpfs 396M 5.6M 391M 2% /run
tmpfs tmpfs 396M 0 396M 0% /sys/fs/cgroup
/dev/mapper/rhel-root xfs 50G 1.7G 49G 4% /
/dev/mapper/rhel-home xfs 46G 354M 45G 1% /home
/dev/sda1 xfs 1014M 179M 836M 18% /boot
tmpfs tmpfs 80M 0 80M 0% /run/user/0
/dev/sdb1 xfs 195M 12M 184M 6% /resource1
//未格式化的分区无法挂载
[root@localhost ~]# mkdir /resource2
[root@localhost ~]# mount /dev/sdb5 /resource2
mount: /resource2: wrong fs type, bad option, bad superblock on /dev/sdb5, missing codepage or helper program, or other error.
[root@localhost ~]# mkfs.ext4 /dev/sdb5
mke2fs 1.45.4 (23-Sep-2019)
Creating filesystem with 512000 1k blocks and 128016 inodes
Filesystem UUID: 82d23739-cd01-4f61-99e4-07fced1607e7
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# mount /dev/sdb5 /resource2
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 379M 0 379M 0% /dev
tmpfs tmpfs 396M 0 396M 0% /dev/shm
tmpfs tmpfs 396M 5.6M 391M 2% /run
tmpfs tmpfs 396M 0 396M 0% /sys/fs/cgroup
/dev/mapper/rhel-root xfs 50G 1.7G 49G 4% /
/dev/mapper/rhel-home xfs 46G 354M 45G 1% /home
/dev/sda1 xfs 1014M 179M 836M 18% /boot
tmpfs tmpfs 80M 0 80M 0% /run/user/0
/dev/sdb1 xfs 195M 12M 184M 6% /resource1
/dev/sdb5 ext4 477M 2.3M 445M 1% /resource2
永久挂载磁盘
自动挂载的配置文件在/etc/fstab
注意:磁盘设备名称可能会发生改变,导致启动时发生错误,所以要用唯一标识符UUID来自动挂载
//查看sdb1的UUID
[root@localhost ~]# blkid /dev/sdb1
/dev/sdb1: UUID="570eb7cc-26bf-4564-81cf-5c43962a1ebf" TYPE="xfs" PARTUUID="572ffbac-01"
[root@localhost ~]#
//写配置文件
[root@localhost ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Nov 24 15:23:15 2020
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rhel-root / xfs defaults 0 0
UUID=c7689f18-6e83-495f-8162-c4a3a98b97c5 /boot xfs defaults 0 0
/dev/mapper/rhel-home /home xfs defaults 0 0
/dev/mapper/rhel-swap swap swap defaults 0 0
UUID="570eb7cc-26bf-4564-81cf-5c43962a1ebf" /resource1 xfs defaults 0 0
//重新读取/etc/fstab配置文件的所有分区
[root@localhost ~]# mount -a
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 379M 0 379M 0% /dev
tmpfs tmpfs 396M 0 396M 0% /dev/shm
tmpfs tmpfs 396M 5.6M 391M 2% /run
tmpfs tmpfs 396M 0 396M 0% /sys/fs/cgroup
/dev/mapper/rhel-root xfs 50G 1.7G 49G 4% /
/dev/mapper/rhel-home xfs 46G 354M 45G 1% /home
/dev/sda1 xfs 1014M 179M 836M 18% /boot
tmpfs tmpfs 80M 0 80M 0% /run/user/0
/dev/sdb1 xfs 195M 12M 184M 6% /resource1
/dev/sdb5 ext4 477M 2.3M 445M 1% /resource2
fstab配置文件用于自动挂载,下面分别介绍每列什么含义:
分区标识(UUID或设备名) 挂载点 文件类型 挂载参数 不检查 不备份
UUID="570eb7cc-26bf-4564-81cf-5c43962a1ebf" /db1 xfs defaults 0 0
卸载挂载磁盘
//方法一:通过UUID卸载
[root@localhost ~]# umount UUID="570eb7cc-26bf-4564-81cf-5c43962a1ebf"
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 379M 0 379M 0% /dev
tmpfs tmpfs 396M 0 396M 0% /dev/shm
tmpfs tmpfs 396M 5.6M 391M 2% /run
tmpfs tmpfs 396M 0 396M 0% /sys/fs/cgroup
/dev/mapper/rhel-root xfs 50G 1.7G 49G 4% /
/dev/mapper/rhel-home xfs 46G 354M 45G 1% /home
/dev/sda1 xfs 1014M 179M 836M 18% /boot
tmpfs tmpfs 80M 0 80M 0% /run/user/0
/dev/sdb5 ext4 477M 2.3M 445M 1% /resource2
//方法二:通过挂载点卸载
[root@localhost ~]# umount /resource2
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 379M 0 379M 0% /dev
tmpfs tmpfs 396M 0 396M 0% /dev/shm
tmpfs tmpfs 396M 5.6M 391M 2% /run
tmpfs tmpfs 396M 0 396M 0% /sys/fs/cgroup
/dev/mapper/rhel-root xfs 50G 1.7G 49G 4% /
/dev/mapper/rhel-home xfs 46G 354M 45G 1% /home
/dev/sda1 xfs 1014M 179M 836M 18% /boot
tmpfs tmpfs 80M 0 80M 0% /run/user/0
//方法三:通过设备名称卸载
[root@localhost ~]# mount -a
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 379M 0 379M 0% /dev
tmpfs tmpfs 396M 0 396M 0% /dev/shm
tmpfs tmpfs 396M 5.6M 391M 2% /run
tmpfs tmpfs 396M 0 396M 0% /sys/fs/cgroup
/dev/mapper/rhel-root xfs 50G 1.7G 49G 4% /
/dev/mapper/rhel-home xfs 46G 354M 45G 1% /home
/dev/sda1 xfs 1014M 179M 836M 18% /boot
tmpfs tmpfs 80M 0 80M 0% /run/user/0
/dev/sdb1 xfs 195M 12M 184M 6% /resource1
[root@localhost ~]# umount /dev/sdb1
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 379M 0 379M 0% /dev
tmpfs tmpfs 396M 0 396M 0% /dev/shm
tmpfs tmpfs 396M 5.6M 391M 2% /run
tmpfs tmpfs 396M 0 396M 0% /sys/fs/cgroup
/dev/mapper/rhel-root xfs 50G 1.7G 49G 4% /
/dev/mapper/rhel-home xfs 46G 354M 45G 1% /home
/dev/sda1 xfs 1014M 179M 836M 18% /boot
tmpfs tmpfs 80M 0 80M 0% /run/user/0
强制卸载挂载
当占用设备挂载的挂载点时,卸载挂载磁盘会显示目标繁忙,无法卸载,此时可以强制卸载挂载
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 379M 0 379M 0% /dev
tmpfs tmpfs 396M 0 396M 0% /dev/shm
tmpfs tmpfs 396M 5.6M 391M 2% /run
tmpfs tmpfs 396M 0 396M 0% /sys/fs/cgroup
/dev/mapper/rhel-root xfs 50G 1.7G 49G 4% /
/dev/mapper/rhel-home xfs 46G 354M 45G 1% /home
/dev/sda1 xfs 1014M 179M 836M 18% /boot
tmpfs tmpfs 80M 0 80M 0% /run/user/0
/dev/sdb1 xfs 195M 12M 184M 6% /resource1
[root@localhost ~]# cd /resource1
[root@localhost resource1]# umount /resource1
umount: /resource1: target is busy.
//-lf 强制卸载挂载
[root@localhost resource1]# umount -lf /resource1
[root@localhost resource1]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 379M 0 379M 0% /dev
tmpfs tmpfs 396M 0 396M 0% /dev/shm
tmpfs tmpfs 396M 5.6M 391M 2% /run
tmpfs tmpfs 396M 0 396M 0% /sys/fs/cgroup
/dev/mapper/rhel-root xfs 50G 1.7G 49G 4% /
/dev/mapper/rhel-home xfs 46G 354M 45G 1% /home
/dev/sda1 xfs 1014M 179M 836M 18% /boot
tmpfs tmpfs 80M 0 80M 0% /run/user/0
交换分区SWAP
交换分区SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用。
交换分区一般指定虚拟内存的大小为实际内存的1.5~2倍。如果实际内存超过8GB,可以直接划分16GB给虚拟内存即可,如果虚拟内存不够用的情况,须增加一个虚拟磁盘,由于不能给原有的磁盘重新分区,所以可以选择新建。
小扩展:如果真实内存是16GB,那SWAP分区应该是多少?
答:SWAP空间是在内存不够用的时候才需要分的,所以当内存为16GB时,SWAP分区应该用0GB。
检测当前swap分区情况
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 791 194 95 1 501 460
Swap: 4043 54 3989
创建swapfile
[root@localhost ~]# dd if=/dev/zero of=/opt/myswap bs=1024M count=2
2+0 records in
2+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 24.955 s, 86.1 MB/s
[root@localhost ~]# du -sh /opt/myswap
2.0G /opt/myswap
格式化swap分区
[root@localhost ~]# mkswap -f /opt/myswap
mkswap: /opt/myswap: insecure permissions 0644, 0600 suggested.
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=989ff785-d020-496d-ba1e-3bbdd7e900b2
开启新建的SWAP分区
[root@localhost ~]# swapon /opt/myswap
swapon: /opt/myswap: insecure permissions 0644, 0600 suggested.
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 791 152 559 0 79 539
Swap: 6091 84 6007
让swap在下次启动的时候生效
[root@localhost ~]# vi /etc/fstab
/opt/myswap swap swap defaults 0 0
关闭新建的swap分区
[root@localhost ~]# swapoff /opt/myswap
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 791 151 552 0 87 536
Swap: 4043 84 3959
创建swap分区并且持久化使用
//第一步:分一个给swap用的区,这里分了2G
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part
├─rhel-root 253:0 0 50G 0 lvm /
├─rhel-swap 253:1 0 4G 0 lvm [SWAP]
└─rhel-home 253:2 0 45G 0 lvm /home
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 200M 0 part
├─sdb2 8:18 0 512B 0 part
└─sdb5 8:21 0 500M 0 part
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 500M 0 part /resource3
sdd 8:48 0 20G 0 disk
sr0 11:0 1 7.9G 0 rom
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x572ffbac
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 411647 409600 200M 83 Linux
/dev/sdb2 411648 41943039 41531392 19.8G 5 Extended
/dev/sdb5 413696 1437695 1024000 500M 83 Linux
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 6
First sector (1439744-41943039, default 1439744):
Last sector, +sectors or +size{K,M,G,T,P} (1439744-41943039, default 41943039): +2G
Created a new partition 6 of type 'Linux' and of size 2 GiB.
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x572ffbac
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 411647 409600 200M 83 Linux
/dev/sdb2 411648 41943039 41531392 19.8G 5 Extended
/dev/sdb5 413696 1437695 1024000 500M 83 Linux
/dev/sdb6 1439744 5634047 4194304 2G 83 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 or 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 ea Rufus alignment
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto
1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT
//把sdb6更改类型为swap格式
Command (m for help): t
Partition number (1,2,5,6, default 6):
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x572ffbac
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 411647 409600 200M 83 Linux
/dev/sdb2 411648 41943039 41531392 19.8G 5 Extended
/dev/sdb5 413696 1437695 1024000 500M 83 Linux
/dev/sdb6 1439744 5634047 4194304 2G 82 Linux swap / Solaris
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
//刷新分区表
[root@localhost ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part
├─rhel-root 253:0 0 50G 0 lvm /
├─rhel-swap 253:1 0 4G 0 lvm [SWAP]
└─rhel-home 253:2 0 45G 0 lvm /home
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 200M 0 part
├─sdb2 8:18 0 1K 0 part
├─sdb5 8:21 0 500M 0 part
└─sdb6 8:22 0 2G 0 part
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 500M 0 part /resource3
sdd 8:48 0 20G 0 disk
sr0 11:0 1 7.9G 0 rom
//格式化sdb6成swap空间(分区不用加-f)
[root@localhost ~]# mkswap /dev/sdb6
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=74969c3a-af5f-40b6-8060-ca653a6b951a
//查看UUID
[root@localhost ~]# blkid /dev/sdb6
/dev/sdb6: UUID="74969c3a-af5f-40b6-8060-ca653a6b951a" TYPE="swap" PARTUUID="572ffbac-06"
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 791 157 524 0 109 519
Swap: 4043 82 3961
//开启swap空间
[root@localhost ~]# swapon UUID="74969c3a-af5f-40b6-8060-ca653a6b951a"
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 791 159 522 0 109 518
Swap: 6091 82 6009
//让swap在下次启动的时候生效
[root@localhost ~]# vi /etc/fstab
UUID="74969c3a-af5f-40b6-8060-ca653a6b951a" swap swap defaults 0 0
查看swap空间组成
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 4141052 82016 -2
/dev/sdb6 partition 2097148 0 -3
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 4141052 82016 -2
/dev/sdb6 partition 2097148 0 -3
[root@localhost ~]# swapon /opt/myswap
swapon: /opt/myswap: insecure permissions 0644, 0600 suggested.
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 4141052 82016 -2
/dev/sdb6 partition 2097148 0 -3
/opt/myswap file 2097148 0 -4
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 791 163 507 1 120 508
Swap: 8139 80 8059