一. 什么是磁盘以及MBR和GPT的区别
1. Linux所有设备都被抽象为一个文件,保存在/dev目录下,IDE设备的设备名称一般为hd[a-z],SATA、SCSI、USB等设备为sd[a-z]。
2. 分区
不同的分区用:设备名称+分区号方式如:sda1、sdb1等,主流的分区机制分为MBR和GPT两种。
3. MBR(Master Boot Record):主引导记录,应用于绝大多数使用BIOS的PC设备
其特点是:(1)支持32/64bit;(2)支持分区的数量有限;(3)只支持不超过2T的硬盘,大于2T的硬盘只能使用2T空间;(4)占用最开始的512字节,最后两个字节一定是55h、AAh(表示可引导)。
主分区:最多能创建4个
扩展分区:会占用一个主分区位置,本身不能使用,需要创建逻辑分区进行使用
逻辑分区:最多支持63个IDE分区和15个SCSI分区,前提是创建好扩展分区
4. GPT(GUIDE Partition Table):全局唯一标识分区表,解决了MBR的很多缺点
(1)支持超过2T的磁盘
(2)向后兼容MBR
(3)必须在支持UEFI的硬件上才能使用
(4)必须使用64bit的操作系统
(5)Mac、linux系统都能支持GPT,Windows7 64位,WindowsServer2008 64位支持GPT
二.磁盘查看命令
fdisk -l 系统中的所有磁盘设备
df -TH 系统正在挂载的磁盘设备
blkid 系统可以挂载的磁盘设备id
三. 分区划分
fdisk /dev/vdb
按m enter 进入帮助
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition 删除分区
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types 列出系统可用分区类型
m print this menu
n add a new partition 新建分区
o create a new empty DOS partition table21
p print the partition table 显示分区
q quit without saving changes 退出
s create a new empty Sun disklabel
t change a partition's system id 修改分区功能id
u change display/entry units
v verify the partition table
w write table to disk and exit 保存更改到分区表中
x extra functionality (experts only)
新建分区 fdisk /dev/vdb ====> n
Command (m for help): n 新建分区
Partition type:
p primary (1 primary, 0 extended, 3 free) 分区类型为主分区
e extended 分区类型为扩展分区
Select (default p):
Partition number (1-4, default 1): 1 主分区id
First sector (2048-20971519, default 2048): 此分区的起始位置
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +1G 分区大小
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): wq ##保存并退出,只输入q则表示放弃更改退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
partprobe 同步分区表
mkfs.xfs /dev/vdb1 格式化
mount /dev/vdb1 /mnt 临时挂载
vim /etc/fstab 永久挂载
mount -a 让 /etc/fstab挂载生效
删除分区
fdisk /dev/vdb
先使用umount命令解除挂载
当因为正在使用都无法解除挂载的时候 先用 fuser -kvm 结束使用的进程 然后再解除挂载 然后用 fdisk /dev/vdb 指令删除分区
设置分区方式 mkdocs 与 gpt 互相转换
添加swap分区
[root@localhost Desktop]# fdisk /dev/vdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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): n
Partition number (1-128, default 1): 2
First sector (34-20971486, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971486, default 20971486): +4G
Created partition 2
Command (m for help): p
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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
2 2048 8390655 4G Linux filesyste
Command (m for help): t
Selected partition 2
Partition type (type L to list all types): l
1 EFI System C12A7328-F81F-11D2-BA4B-00A0C93EC93B
2 MBR partition scheme 024DEE41-33E7-11D3-9D69-0008C781F39F
3 BIOS boot partition 21686148-6449-6E6F-744E-656564454649
4 Microsoft reserved E3C9E316-0B5C-4DB8-817D-F92DF00215AE
5 Microsoft basic data EBD0A0A2-B9E5-4433-87C0-68B6B72699C7
6 Microsoft LDM metadata 5808C8AA-7E8F-42E0-85D2-E1E90434CFB3
7 Microsoft LDM data AF9B60A0-1431-4F62-BC68-3311714A69AD
8 Windows recovery evironmnet DE94BBA4-06D1-4D40-A16A-BFD50179D6AC
9 IBM General Parallel Fs 37AFFC90-EF7D-4E96-91C3-2D7AE055B174
10 HP-UX data partition 75894C1E-3AEB-11D3-B7C1-7B03A0000000
11 HP-UX service partition E2A1E728-32E3-11D6-A682-7B03A0000000
12 Linux filesystem 0FC63DAF-8483-4772-8E79-3D69D8477DE4
13 Linux RAID A19D880F-05FC-4D3B-A006-743F0F84911E
14 Linux swap 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F
15 Linux LVM E6D6D379-F507-44C2-A23C-238F2A3DF928
16 Linux reserved 8DA63339-0007-60C0-C436-083AC8230908
17 FreeBSD data 516E7CB4-6ECF-11D6-8FF8-00022D09712B
18 FreeBSD boot 83BD6B9D-7F41-11DC-BE0B-001560B84F0F
19 FreeBSD swap 516E7CB5-6ECF-11D6-8FF8-00022D09712B
20 FreeBSD UFS 516E7CB6-6ECF-11D6-8FF8-00022D09712B
21 FreeBSD ZFS 516E7CBA-6ECF-11D6-8FF8-00022D09712B
22 FreeBSD Vinum 516E7CB8-6ECF-11D6-8FF8-00022D09712B
23 Apple HFS/HFS+ 48465300-0000-11AA-AA11-00306543ECAC
24 Apple UFS 55465300-0000-11AA-AA11-00306543ECAC
25 Apple RAID 52414944-0000-11AA-AA11-00306543ECAC
26 Apple RAID offline 52414944-5F4F-11AA-AA11-00306543ECAC
27 Apple boot 426F6F74-0000-11AA-AA11-00306543ECAC
28 Apple label 4C616265-6C00-11AA-AA11-00306543ECAC
29 Apple TV recovery 5265636F-7665-11AA-AA11-00306543ECAC
30 Apple Core storage 53746F72-6167-11AA-AA11-00306543ECAC
31 Solaris boot 6A82CB45-1DD2-11B2-99A6-080020736631
32 Solaris root 6A85CF4D-1DD2-11B2-99A6-080020736631
33 Solaris /usr & Apple ZFS 6A898CC3-1DD2-11B2-99A6-080020736631
34 Solaris swap 6A87C46F-1DD2-11B2-99A6-080020736631
35 Solaris backup 6A8B642B-1DD2-11B2-99A6-080020736631
36 Solaris /var 6A8EF2E9-1DD2-11B2-99A6-080020736631
37 Solaris /home 6A90BA39-1DD2-11B2-99A6-080020736631
38 Solaris alternate sector 6A9283A5-1DD2-11B2-99A6-080020736631
39 Solaris reserved 1 6A945A3B-1DD2-11B2-99A6-080020736631
40 Solaris reserved 2 6A9630D1-1DD2-11B2-99A6-080020736631
41 Solaris reserved 3 6A980767-1DD2-11B2-99A6-080020736631
42 Solaris reserved 4 6A96237F-1DD2-11B2-99A6-080020736631
43 Solaris reserved 5 6A8D2AC7-1DD2-11B2-99A6-080020736631
44 NetBSD swap 49F48D32-B10E-11DC-B99B-0019D1879648
45 NetBSD FFS 49F48D5A-B10E-11DC-B99B-0019D1879648
46 NetBSD LFS 49F48D82-B10E-11DC-B99B-0019D1879648
47 NetBSD concatenated 2DB519C4-B10E-11DC-B99B-0019D1879648
48 NetBSD encrypted 2DB519EC-B10E-11DC-B99B-0019D1879648
49 NetBSD RAID 49F48DAA-B10E-11DC-B99B-0019D1879648
50 ChromeOS kernel FE3A2A5D-4F32-41A7-B725-ACCC3285A309
51 ChromeOS root fs 3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC
52 ChromeOS reserved 2E0A753D-9E48-43B0-8337-B15192CB1B5E
53 MidnightBSD data 85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7
54 MidnightBSD boot 85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7
55 MidnightBSD swap 85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7
56 MidnightBSD UFS 0394Ef8B-237C-11E1-B4B3-E89A8F7FC3A7
57 MidnightBSD ZFS 85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7
58 MidnightBSD Vinum 85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7
Partition type (type L to list all types): 14
Changed type of partition 'Linux filesystem' to 'Linux swap'
Command (m for help): p
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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
2 2048 8390655 4G Linux swap
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost Desktop]#
更改gpt下的swap 为mkdos下的swap
用文件来添加swap
dd if=/dev/zero of=/swapfile bs=1M count=1000 创建一个1G大小的文件
mkswap /swapfile 格式化为swap类型
swapon -a /swapfile 临时添加到swap里
-p + 数字 更改优先级
vim /etc/fstab 永久添加swap分区
类型:
/swapfile swap swap defaults,pri=1 0 0 pri优先级
删除swap
vim /etc/fstab 删除此文件中添加的swap行
swapoff /swapfile 断开swap文件链接
swapoff /dev/vdb1 断开swap磁盘链接
rm -rf /swapfile 删除文件
fdisk /dev/vdb 删除磁盘分区
partprobe 同步分区表