磁盘管理
一、磁盘类型
SATA,SCSI,SAS类型的磁盘,在Linux中用sd来表示。
其中第一块硬盘为sda,第二块二sdb,以此类推。
第一块硬盘的第一个分区为sda1。
nvme类型的磁盘,在Linux中使用nvmeXnYpZ进行表示。
X:数字,表示第几通道的NVME类型,第一个通道用0表示
Y:数字,表示第几块硬盘,第一个硬盘用1表示
Z:数字,表示第几个分区,第一个分区用1表示
virtio-block类型:
在Linux中用vd表示,第一块硬盘为vda,第一个硬盘的第一个分区为vda1
划分多个分区的目的:
1、保证数据的安全性(全部划分到一个分区,只要这个分区出问题了,那么就都出问题了)
2、数据分类存储(不同分区存储不同类型的数据)
3、查找数据更快速
磁盘------分区------文件系统(格式化,记录数据具体位置)
两种分区方案:
MBR分区方案(Master Boot Record 主引导记录,Linux默认分区方案,存在一些局限性)
1、支持最大硬盘:2TB
2、分区的个数有限制:16个
3、分区表没有备份
主分区 扩展分区 逻辑分区
主分区号+扩展分区号(1-4),一块硬盘中扩展分区只能为0个或者1个,主分区最多4个
磁盘中的第一个扇区就是MBR,也叫做主引导扇区。大小为512个字节。
其中主引导记录(启动代码)占用其中的446个字节,分区表占用了64个字节(每个分区定义的是16个字节),
结束标志占用2个字节。
所以最多4个分区:64/16=4
扩展分区不能直接用来存储数据,必须从扩展分区中划分出逻辑分区来进行存储(第一个逻辑分区号从5开始)
GPT分区方案(GUID Partition Table–全局唯一表示分区表)
1、支持2TB以上的大硬盘
2、每个磁盘的分区个数几乎没有限制(最多只允许划分128个分区)
3、分区大小几乎没有限制,8ZB
4、分区表自带备份。在磁盘的首尾部分分别保存了一份相同的分区表。其中一份被破坏后,
可以通过另外一份恢复
没有主分区、扩展分区、逻辑分区的概念
fdisk -l --查看并确认磁盘的设备类型。
[root@stw ~]# fdisk -lDisk /dev/sda: 107.4 GB, 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
Disk label type: dos
Disk identifier: 0x000eba9bDevice Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 106956799 52428800 83 Linux
/dev/sda3 106956800 115345407 4194304 82 Linux swap / Solaris
Device: 分区的设备文件名称。
Boot:是否是引导分区。如果是,则要有 “*”标志。
Start:该分区在磁盘中的起始位置(柱面数)。
End:该分区在磁盘中的结束位置(柱面数)。
Blocks:End-Start再换算之后的值(换算到块当中)
sectors: 扇区,该分区的柱面数之和
size:大小
Id:分区对应的系统ID号,83表示Linux中的分区,82为swap分区。
type: 分区类型。
fdisk 创建分区
[root@stw ~]# fdisk /dev/sda
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.Command (m for help): m
Command actiona toggle a bootable flagb edit bsd disklabelc toggle the dos compatibility flagd delete a partitiong create a new empty GPT partition tableG create an IRIX (SGI) partition tablel list known partition typesm print this menun add a new partitiono create a new empty DOS partition tablep print the partition tableq quit without saving changess create a new empty Sun disklabelt change a partition's system idu change display/entry unitsv verify the partition tablew write table to disk and exitx extra functionality (experts only)Command (m for help): p //查看Disk /dev/sda: 107.4 GB, 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
Disk label type: dos
Disk identifier: 0x000eba9bDevice Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 106956799 52428800 83 Linux
/dev/sda3 106956800 115345407 4194304 82 Linux swap / SolarisCommand (m for help): n //新建分区
Partition type:p primary (3 primary, 0 extended, 1 free)e extended
Select (default e): e //选择扩展分区
Selected partition 4
First sector (115345408-209715199, default 115345408):
Using default value 115345408
Last sector, +sectors or +size{K,M,G} (115345408-209715199, default 209715199):
Using default value 209715199
Partition 4 of type Extended and of size 45 GiB is setCommand (m for help): p Disk /dev/sda: 107.4 GB, 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
Disk label type: dos
Disk identifier: 0x000eba9bDevice Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 106956799 52428800 83 Linux
/dev/sda3 106956800 115345407 4194304 82 Linux swap / Solaris
/dev/sda4 115345408 209715199 47184896 5 ExtendedCommand (m for help): n //再次新建分区(逻辑分区)
All primary partitions are in use
Adding logical partition 5
First sector (115347456-209715199, default 115347456):
Using default value 115347456
Last sector, +sectors or +size{K,M,G} (115347456-209715199, default 209715199): +500M //给500M的大小
Partition 5 of type Linux and of size 500 MiB is setCommand (m for help): pDisk /dev/sda: 107.4 GB, 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
Disk label type: dos
Disk identifier: 0x000eba9bDevice Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 106956799 52428800 83 Linux
/dev/sda3 106956800 115345407 4194304 82 Linux swap / Solaris
/dev/sda4 115345408 209715199 47184896 5 Extended
/dev/sda5 115347456 116371455 512000 83 LinuxCommand (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.
[root@stw ~]# fdisk -lDisk /dev/sda: 107.4 GB, 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
Disk label type: dos
Disk identifier: 0x000eba9bDevice Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 106956799 52428800 83 Linux
/dev/sda3 106956800 115345407 4194304 82 Linux swap / Solaris
/dev/sda4 115345408 209715199 47184896 5 Extended
/dev/sda5 115347456 116371455 512000 83 Linux
[root@stw ~]# partprobe /dev/sda //同步到硬盘(MBR扇区)
默认文件系统类型xfs
[root@stw ~]# mkfs.
mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat
mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs
[root@stw ~]# mkfs.xfs /dev/sda5 //格式化(设置文件系统)
meta-data=/dev/sda5 isize=512 agcount=4, agsize=32000 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=128000, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=855, version=2= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@stw ~]# vim /etc/fstab
手动挂载–临时挂载
mount 设备 挂载点
永久挂载—必须把挂载信息写入到/etc/fstab文件中。
注意:该文件中的挂载信息不能写错,一旦写错,系统重启后将无法
正常进入操作系统。
/etc/fstab文件中每个字段的意思如下:
第1字段:设备名或者是设备的UUID(建议使用UUID)
第2字段:挂载点(如果设备为交换分区时,此处应写为swap)
第3字段:文件系统类型,如xfs、ext4、swap等
第4字段:挂载参数,即mount -o 选项后可以使用的参数。
如:defaults(默认参数)、rw(可写)、ro(只读)、noexec(禁用执行程序)
第5字段:表示文件系统是否需要dump备份。(dump是一个备份工具),1为需要备份,
0为不需要备份,即被dump所忽略。
第6字段:该数字用户决定系统在启动时是否使用fsck命令,以进行磁盘检查的顺序。
0表示不检查,1表示优先检查,2表示其次检查。
xfs的文件系统不适用fsck检查,即可设置为0.
[root@stw ~]# blkid //查询UUID,要先格式化才能查询
/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="efbd9c63-21d7-4829-ae74-fa642a28640a" TYPE="xfs"
/dev/sda2: UUID="236d2c7d-6940-4d45-914d-a06afa4c5709" TYPE="xfs"
/dev/sda3: UUID="80552482-4dff-460e-a6f9-b14cd3618248" TYPE="swap"
/dev/sda5: UUID="d5095b72-8cfd-4fbb-8e34-a8e1ebcf5f5e" TYPE="xfs"
[root@stw ~]# mkdir /test1 //创建挂载点
[root@stw ~]# vim /etc/fstab
[root@stw ~]# mount -a //重新加载/etc/fstab文件里的数据
[root@stw ~]# df -Th //重新查看挂载情况
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 xfs 50G 3.5G 47G 7% /
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 13M 2.0G 1% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 163M 852M 17% /boot
tmpfs tmpfs 394M 12K 394M 1% /run/user/42
tmpfs tmpfs 394M 0 394M 0% /run/user/0
/dev/sda5 xfs 497M 26M 472M 6% /test1
[root@stw ~]# cd /test1
//可以往test1中存放数据了
parted 创建分区
[root@stw test1]# parted -l //查看
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system Flags1 1049kB 1075MB 1074MB primary xfs boot2 1075MB 54.8GB 53.7GB primary xfs3 54.8GB 59.1GB 4295MB primary linux-swap(v1)4 59.1GB 107GB 48.3GB extended5 59.1GB 59.6GB 524MB logical xfsWarning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0
has been opened read-only.
Model: NECVMWar VMware IDE CDR10 (scsi)
Disk /dev/sr0: 4589MB
Sector size (logical/physical): 2048B/2048B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system Flags2 4907kB 40.6MB 35.7MB primary
重新添加一块硬盘
一直下一步添加,然后reboot重启,完成硬盘的添加
[root@stw ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
align-check help mktable quit resizepart set version
disk_set mklabel name rescue rm toggle
disk_toggle mkpart print resize select unit
(parted) p //查看
Error: /dev/sdb: unrecognised disk label
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
(parted) mklabel //设置标签,设置为gpt类型分区
New disk label type?
aix amiga bsd dvh gpt loop mac msdos pc98 sun
New disk label type? gpt
(parted) p //查看
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt //已经设置为gpt分区类型
Disk Flags: Number Start End Size File system Name Flags(parted) unit //设置单位
Unit? [compact]?
% chs cyl GiB kiB MiB TB
B compact GB kB MB s TiB
Unit? [compact]? MiB //一般设置MiB(兆)为单位
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name Flags(parted)
align-check help mktable quit resizepart set version
disk_set mklabel name rescue rm toggle
disk_toggle mkpart print resize select unit
(parted) mkpart //创建分区
Partition name? []?
File system type? [ext2]? //因为后面要格式化所以不用设置类型
Start? 10MiB //设置分区大小500MiB
End? 510MiB
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name Flags1 10.0MiB 510MiB 500MiB(parted) quit //保存退出
Information: You may need to update /etc/fstab.[root@stw ~]# udevadm settle //同步到硬盘
[root@stw ~]# mkfs.ext4 /dev/sdb1 //格式化
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
128016 inodes, 512000 blocks
25600 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=34078720
63 block groups
8192 blocks per group, 8192 fragments per group
2032 inodes per group
Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done [root@stw ~]# blkid /dev/sdb1
/dev/sdb1: UUID="ea4a90c9-77f6-48b8-8d2c-f34c90062db9" TYPE="ext4" PARTUUID="247ce484-6caa-4b5d-9bb5-5a78c0a7fcdf"
[root@stw ~]# mkdir /test2
[root@stw ~]# vim /etc/fstab
[root@stw ~]# mount -a //重新加载/etc/fstab文件里的数据
[root@stw ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 xfs 50G 3.5G 47G 7% /
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 13M 2.0G 1% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda5 xfs 497M 26M 472M 6% /test1
/dev/sda1 xfs 1014M 163M 852M 17% /boot
tmpfs tmpfs 394M 12K 394M 1% /run/user/42
tmpfs tmpfs 394M 0 394M 0% /run/user/0
/dev/sdb1 ext4 477M 2.3M 445M 1% /test2
增大虚拟内存swap
[root@stw ~]# free -mtotal used free shared buff/cache available
Mem: 3932 393 3151 14 386 3246
Swap: 4095 0 4095
[root@stw ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) unit //先设置单位
Unit? [compact]? MiB
(parted) p //查看
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name Flags1 10.0MiB 510MiB 500MiB ext4(parted) mkpart //新建分区
Partition name? []?
File system type? [ext2]?
Start? 520MiB //间隔10MiB设置
End? 1020MiB //划分500M内存
(parted) p //查看
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name Flags1 10.0MiB 510MiB 500MiB ext42 520MiB 1020MiB 500MiB(parted) quit //保存退出
Information: You may need to update /etc/fstab.[root@stw ~]# udevadm settle //同步到硬盘
[root@stw ~]# mkfs. //mkfs.命令没有swap文件系统
mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat
mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs
[root@stw ~]# mkswap /dev/sdb2 //swap特有的格式化命令
Setting up swapspace version 1, size = 511996 KiB
no label, UUID=30ddd5fc-2369-4006-b8f7-8eb5a448efde
[root@stw ~]# vim /etc/fstab
[root@stw ~]# swapon -a //mount没有swap的挂载情况,启用所有交换分区,swapoff -a关闭所有交换分区
[root@stw ~]# free -mtotal used free shared buff/cache available
Mem: 3932 394 3146 14 391 3246
Swap: 4595 0 4595