【CentOS基础篇】之磁盘分区
分区的作用
优化I/O 性能
实现磁盘空间配额限制
高修复速度
隔离系统和程序
安装多个OS(操作系统)
采用不同文件系统
分区的方式(MBR、GPT)
MBR分区
MBR:Master Boot Record,使用32位表示扇区数,分区不超过2T
MBR按柱面分区,硬盘主引导记录MBR由4个部分组成,3主分区+1扩展(N个逻辑分区)
GPT分区
GPT:GUID(Globals Unique Identifiers)partition table 支持128个分区,使用64位,支持8Z(512Byte/block )64Z (4096Byte/block)
使用128位UUID(Universally Unique Identifier) 表示磁盘和分区GPT分区表自动备份在头和尾两份,并有CRC校验位
UEFI (统一扩展固件接口)硬件支持GPT,使操作系统启动
创建分区
命令
列出块设备 lsblk
fdisk创建MBR分区
gdisk创建GPT分区
parted高级分区操作
partprobe-重新设置内存中的内核分区表版本
创建MBR分区
fdisk /dev/sdb 管理分区
fdisk -l [-u] [device...] 查看分区
子命令:
p 分区列表
n 创建新分区
d 删除分区
w 保存并退出
q 不保存并退出
[root@yi ~]# fdisk /dev/sdb-----------------------------管理MBR分区
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x35d223ff.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
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---------------------------------n:新建分区
Command action
e extended
p primary partition (1-4)
p-------------------------------------------------------p:创建主分区
Partition number (1-4): 1-------------------------------1:第一个分区
First cylinder (1-6527, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): +10G
-------------------------------------------------------增加大小为10G的分区
Command (m for help): n--------------------------------同理,新建分区
Command action
e extended
p primary partition (1-4)
p------------------------------------------------------p:创建主分区
Partition number (1-4): 2------------------------------1:第二个分区
First cylinder (1307-6527, default 1307):
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-6527, default 6527): +20G
Command (m for help): p--------------------------------查看分区详情
Disk /dev/sdb: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0x35d223ff
Device Boot Start End Blocks Id System
/dev/sdb1 1 1306 10490413+ 83 Linux
/dev/sdb2 1307 3918 20980890 83 Linux
Command (m for help): w---------------------------------保存分区
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@yi ~]# fdisk -l ----------------------------------查看所有分区
Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 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: 0x000b97d1
Device Boot Start End Blocks Id System
/dev/sda1 * 1 128 1024000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 128 6502 51200000 83 Linux
/dev/sda3 6502 11601 40960000 83 Linux
/dev/sda4 11601 26109 116530176 5 Extended
/dev/sda5 11602 12124 4194304 82 Linux swap / Solaris
Disk /dev/sdb: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0x35d223ff
Device Boot Start End Blocks Id System
/dev/sdb1 1 1306 10490413+ 83 Linux
/dev/sdb2 1307 3918 20980890 83 Linux
创建GPT分区
gdisk /dev/sdb 管理分区
fdisk -l [-u] [device...] 查看分区
子命令:
p 分区列表
n 创建新分区
d 删除分区
w 保存并退出
q 不保存并退出
[root@centos7 ~]#gdisk /dev/sdb---------------------------管理GPT分区
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): 1--------------------第一个分区
First sector (34-104857566, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-104857566, default = 104857566) or {+-}size{KMGTP}: +10G
----------------------------------------------------------分区大小为10G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): --------默认linux分区格式
Changed type of partition to 'Linux filesystem'
Command (? for help): n-----------------------------------新建分区
Partition number (2-128, default 2): 2--------------------第二个分区
First sector (34-104857566, default = 20973568) or {+-}size{KMGTP}:
Last sector (20973568-104857566, default = 104857566) or {+-}size{KMGTP}: +20G
----------------------------------------------------------分区大小为20G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):---------默认linux分区格式
Changed type of partition to 'Linux filesystem'
Command (? for help): p-----------------------------------查看分区详情
Disk /dev/sdb: 104857600 sectors, 50.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 55C722D0-5CD4-4E0F-9D9A-47984D9B0A84
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 104857566
Partitions will be aligned on 2048-sector boundaries
Total free space is 41942973 sectors (20.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 20973567 10.0 GiB 8300 Linux filesystem
2 20973568 62916607 20.0 GiB 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/sdb.
The operation has completed successfully.
[root@centos7 ~]#fdisk -l---------------------------------查看所有分区
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x00050c91
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1953791 975872 83 Linux
/dev/sda2 1953792 99608575 48827392 83 Linux
/dev/sda3 99608576 197263359 48827392 83 Linux
/dev/sda4 197263360 419430399 111083520 5 Extended
/dev/sda5 197265408 205076479 3905536 82 Linux swap / Solaris
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase.
Use at your own discretion.
Disk /dev/sdb: 53.7 GB, 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 label type: gpt
# Start End Size Type Name
1 2048 20973567 10G Linux filesyste Linux filesystem
2 20973568 62916607 20G Linux filesyste Linux filesystem
***GPT分区和MBR分区不能转换
***扩展分区如果被删除,所有逻辑分区都被删除
同步分区表
查看内核是否已经识别新的分区:
cat /proc/partations
centos6 通知内核重新读取 硬盘分区表
新增分区用
partx -a /dev/DEVICE (CentOS6中使用)
kpartx -a /dev/DEVICE -f: force
删除分区用
partx -d --nr M-N /dev/DEVICE (M-N #-#指定一个范围,单独数字也可)
CentOS 5 与7:使用partprobe
partprobe在CentOS6系统上兼容性不好
partprobe [/dev/DEVICE]