准备环境,先创建2块0.1g的磁盘
磁盘分区知识点
查看系统中有几个磁盘
[root@oldboyedu ~]# fdisk -l|grep 'sd[a-z]:'
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 sectors
Disk /dev/sdc: 106 MB, 106954752 bytes, 208896 sectors
[root@oldboyedu ~]#
一、MBR
含义:Master Boot Record 主引导记录
位置:磁盘的开头(0磁头 0磁道 1扇区 512字节中的前446字节)
二、分区类型
1、主分区(primary)
1个主分区要占用16个字节的分区表
在1个磁盘中最多4个主分区
一般必须都要有
2、扩展分区(extended)
磁盘最大只能有4个主分区
扩展分区无法直接使用
1个扩展分区要占用16个字节的分区表
1个磁盘中只能创建1个扩展分区
数据必须放在扩展分区中的逻辑分区
3、逻辑分区(logical)
需要先创建扩展分区
存放数据
最多11个
三、分区的命令规则
1、硬盘名字
物理服务器:sata/sas/scsi这些磁盘接口,硬盘的名字是/ sd??开头,都放放在/dev下,如:
第一块硬盘 /dev/sda
第四块硬盘 /dev/sdd
云服务器(阿里云)
/dev/vda
/dev/vdb
2、分区名字
1>主分区或扩展分区
范围:1-4
如:/dev/sda1.../dev/sda4
2>逻辑分区
范围:从5开始(>=5)
如:/dev/sda5(第一个逻辑分区)
习题:
第3块sata磁盘的第2个主分区
/dev/sdc2
第6块sas磁盘的第2个逻辑分区
/dev/sdf6
四、分区实战※※※
1、分区命令
1>fdisk命令
参数:-l(小写字母L):查看磁盘分区信息
fdisk内部命令:
p:print 显示磁盘分区信息
n:new 创建分区
d:delete删除磁盘分区
w:write 保存并退出
q或Ctrl+c:退出不保存
t:改变分区类型
2>parted命令
无需保存,实时生效
功能:
p:print 显示磁盘信息
mktable:(make partition table) 创建磁盘分区表 MBR GPT
mkpart:make partition 创建磁盘分区
rm:删除磁盘分区
q:quit 退出磁盘分区
2、实例练习:
1>fdisk创建分区实例
创建2个分区 1个10MB的主分区 创建1个使用所有剩余空间的扩展 分区 1个20MB的逻辑分区
#开始创建分区
[root@oldboyedu ~]# fdisk /dev/sdb
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): p #查看该磁盘下的分区
Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 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: 0x6e4c6459
Device Boot Start End Blocks Id System
#n 创建分区,第一个创建主分区
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): 1 #分区编号,默认从1开始
First sector (2048-208895, default 2048): #开始大小,默认
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-208895, default 208895): +10M #结束,这里给10M
Partition 1 of type Linux and of size 10 MiB is set
Command (m for help): p #查看
Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 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: 0x6e4c6459
Device Boot Start End Blocks Id System
/dev/sdb1 2048 22527 10240 83 Linux
#创建1个使用所有剩余空间的扩展分区
Command (m for help): n #创建1个使用所有剩余空间的扩展分区
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e #创建扩展分区
Partition number (2-4, default 2): 默认
First sector (22528-208895, default 22528): 默认
Using default value 22528
Last sector, +sectors or +size{K,M,G} (22528-208895, default 208895): 默认
Using default value 208895
Partition 2 of type Extended and of size 91 MiB is set
Command (m for help): p
Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 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: 0x6e4c6459
Device Boot Start End Blocks Id System
/dev/sdb1 2048 22527 10240 83 Linux
/dev/sdb2 22528 208895 93184 5 Extended
#创建一个20M的逻辑分区,一个磁盘只能有一个扩展分区
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5) #逻辑分区
Select (default p): l
Adding logical partition 5
First sector (24576-208895, default 24576):
Using default value 24576
Last sector, +sectors or +size{K,M,G} (24576-208895, default 208895): +20M
Partition 5 of type Linux and of size 20 MiB is set
Command (m for help): p
Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 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: 0x6e4c6459
Device Boot Start End Blocks Id System
/dev/sdb1 2048 22527 10240 83 Linux
/dev/sdb2 22528 208895 93184 5 Extended
/dev/sdb5 24576 65535 20480 83 Linux
#w 保存并退出;q 退出不保存
Command (m for help): w #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@oldboyedu ~]# fdisk -l|grep 'sdb[0-9]'
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
/dev/sdb1 2048 22527 10240 83 Linux
/dev/sdb2 22528 208895 93184 5 Extended
/dev/sdb5 24576 65535 20480 83 Linux
[root@oldboyedu ~]#
删除fdisk创建的分区
[root@oldboyedu ~]# fdisk /dev/sdb
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): d #删除分区
Partition number (1,2,5, default 5): 5 #先删除逻辑分区
Partition 5 is deleted
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted
Command (m for help): d
Selected partition 1
Partition 1 is deleted
Command (m for help): p
Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 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: 0x6e4c6459
Device Boot Start End Blocks Id System
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.
#创建1个使用磁盘所有容量的分区,并挂载到/mnt上※※※
第1个里程碑-创建分区
[root@oldboyedu ~]# fdisk /dev/sdb
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): p
Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 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: 0x6e4c6459
Device Boot Start End Blocks Id System
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):
First sector (2048-208895, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-208895, default 208895):
Using default value 208895
Partition 1 of type Linux and of size 101 MiB is set
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.
第2个里程碑-格式化 创建文件系统 mkfs make filesystem
[root@oldboyedu ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=6464 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=25856, 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
第3个里程碑-挂载
[root@oldboyedu ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 9.9G 8.0G 56% /
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 2.0G 12M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda1 197M 105M 93M 54% /boot
tmpfs 394M 0 394M 0% /run/user/0
[root@oldboyedu ~]# mount /dev/sdb1 /mnt
[root@oldboyedu ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 9.9G 8.0G 56% /
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 2.0G 12M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda1 197M 105M 93M 54% /boot
tmpfs 394M 0 394M 0% /run/user/0
/dev/sdb1 98M 5.3M 93M 6% /mnt
[root@oldboyedu ~]#
第4个里程碑-永久挂载
1./etc/rc.local
[root@oldboyedu ~]# file /etc/rc.d/rc.local
/etc/rc.d/rc.local: Bourne-Again shell script, ASCII text executable
[root@oldboyedu ~]# shell脚本,可执行
2./etc/fstab
/etc/fstab名一列表示的含义
[root@oldboyedu~]# cat /etc/fstab
UUID=1e827748-cc43-4b1d-8bbc-9a85c239782d / xfs defaults 0 0
UUID=29a4ef75-7502-48df-b31c-c7c10264a7c8 /boot xfs defaults 0 0
UUID=60c45f5e-c78d-41fe-aa92-93d10de6def3 swap swap defaults 0 0
第1列:UUID或设备名称
第2列:挂载点(入口)
第3列:文件系统类型
第4列:挂载参数
第5列:是否进行备份
第6列:是否开机自动检查
查询设备上所使用的文件系统类型、UUID等信息(blkid)
[root@oldboyedu ~]# blkid
/dev/sda1: UUID="21560efb-d3ed-4970-9792-e8786cec18a8" TYPE="xfs"
/dev/sda2: UUID="5a66c37c-1a78-4781-af81-1e7001bcae62" TYPE="swap"
/dev/sda3: UUID="9c1f4cad-aa3f-412d-996e-22e3cf7c8dc1" TYPE="xfs"
/dev/sdb1: UUID="3977bf63-261d-4312-a301-5b6a53a8e8cc" TYPE="xfs"
/dev/sdc1: PARTLABEL="primary" PARTUUID="a8de4ec3-90c7-4877-9f24-14ff65229bb6"
/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
[root@oldboyedu ~]#
添加要挂载的磁盘
[root@oldboyedu ~]# tail -1 /etc/fstab
/dev/sdb1 /data/mnt xfs defaults 0 0
[root@oldboyedu ~]#
CentOS7中默认的是xfs
CentOS6 ext系列
2>parted创建1个主分区 10M
[root@oldboyedu ~]# parted /dev/sdc
(parted) p
Error: /dev/sdc: unrecognised disk label #无法识别的
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 107MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown #未知
Disk Flags:
(parted) mktable gpt #创建磁盘分区表 MBR GPT
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 107MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
(parted) mkpart primary 0 10 #创建磁盘分区 ,设置磁盘大小
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? i
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 107MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 17.4kB 10.0MB 9983kB primary
(parted) q #退出磁盘分区
Information: You may need to update /etc/fstab.
fdisk与parted的共同点和区别
fdisk | parted | |
---|---|---|
共同点 | 磁盘分区 | 磁盘分区 |
区别 | 支持2TB以内硬盘 | 大于2TB磁盘 |
支持MBR分区表 | MBR GPT(支持更大的硬盘和更多的主分 |
primary 主分区
extended 扩展分区
logical 逻辑分区
partition 分区
partition table 分区表