一、分区类型
两种分区方式: MBR, GPT
MBR:Master Boot Record, 1982年, 使用32位表示扇区数, 分区不超过2T
-
0磁道0扇区: 512bytes
- 446bytes: boot loader 系统启动引导程序
- 64bytes:分区表,16bytes标识一个分区,因此最多只能划分主分或者3个主分区,一个扩展分区
- 2bytes:55AA标志着已划分分区
GPT:
查看分区命令:
- cat /proc/partitions
- ls /dev/s*
- lsblk
- fdisk -l
fdisk 查看的是磁盘,其他三条命令看的是内存。
扫描新硬盘
当添加新硬盘后,不重启系统,使用以下命令:
echo "- - -" > /sys/class/scsi_host/host0/scan
二、管理分区
1、MBR分区:fdisk
SYNOPSIS fdisk [-uc] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device
使用fdisk命令对硬盘/dev/sdc划分分区:
(1)fdisk /dev/sdc
管理硬盘sdc,进入子命令模式
[root@CentOS6 ~]#fdisk /dev/sdc
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):
(2)输入子命令m
,查看命令帮助
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
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
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
(3)输入子命令n
,新增分区
Command (m for help): n
Command action
e extended
p primary partition (1-4)
(4)选择创建主分区,输入"p
"和分区号“1
”
p
Partition number (1-4): 1
(5)输入1
和+10G
选择分区从硬盘第1个柱面开始,以及分区大小为10G
First cylinder (1-13054, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): +10G
(6)输入子命令w
,保存退出
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
删除、修改等操作,可以参考上面第二步的帮助。
删除主分区,不会影响其他分区。删除逻辑分区,后面的逻辑分区名会依次向前变更。
(2)GPT分区:gdisk
SYNOPSIS gdisk [ -l ] device
gdisk命令用于管理gpt分区,使用方式类似于gdisk:
[root@CentOS6 ~]#gdisk /dev/sdc
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
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):
···略
3、高级分区操作:parted
当我们想更换分区类型时,可以使用parted命令。
这里我们用一个GPT分区的硬盘sdc,更换为MBR分区:
(1)fdisk
查看硬盘信息,可以看到系统会提示该硬盘式gpt分区,建议我们使用parted命令。
[root@CentOS6 ~]#fdisk -l /dev/sdc
WARNING: GPT (GUID Partition Table) detected on '/dev/sdc'! The util fdisk doesn't support GPT. Use GNU Parted.
Disk /dev/sdc: 107.4 GB, 107374182400 bytes
256 heads, 63 sectors/track, 13003 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sdc1 1 13004 104857599+ ee GPT
(2)使用parted
命令更换分区类型
[root@CentOS6 ~]#parted /dev/sdc
GNU Parted 2.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel
New disk label type? msdos
Warning: The existing disk label on /dev/sdc will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? Yes
(parted)
(parted) q
Information: You may need to update /etc/fstab.
mbr转换成gpt也是类似的操作。
当然,也可以直接用gdisk命令把mbr分区换成gpt分区,这里不多说了。