一、磁盘分区方案 1.1 MBR与GPT介绍 Linux中磁盘分区有两种方案
**MBR分区方案 **的特点(支持的磁盘容量<=2T):
最多支持四个主分区
在Linux上使用扩展分区和逻辑分区最多可以创建15个分区,
由于分区中的数据以32位存储,使用MBR分区是最大支持2T空间。
用fdisk管理工具来创建MBR分区
**GPT分区方案 **的特点(支持的磁盘容量>2T):
是UEFI标准的一部分,主板必须要支持UEFI标准
GPT分区列表支持最大128PB(1PB=1024TB)
可以定义128个分区
没有主分区,扩展分区和逻辑分区的概念,所有分区都能格式化
gdisk和parted管理工具可以创建GPT分区
1.2 MBR与GTP的区别
对比项
MBR
GPT
新旧度
旧
新
支持的硬盘容量大小
最大2TB
支持2T以上
分区计数
最多支持4个主分区或3个主分区一个扩展分区
最多支持128个分区,部分主分区、扩展分区
安全功能
不提供任何安全功能
支持CRC32校验和机制
二、分区工具使用 2.1 MBR分区工具fdisk 2.2 GPT分区工具gdisk 2.3 GPT分区工具parted parted用于对磁盘(或RAID磁盘)进行分区及管理,与fdisk分区工具相比,支持2TB以上的磁盘分区,并且允许调整分区的大小,支持命令行方式或交互式方式 ,常用命令如下:
parted交互命令
说 明
check NUMBER
做一次简单的文件系统检测
cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER
复制文件系统到另一个分区
help [COMMAND]
显示所有的命令帮助
mklabel,mktable LABEL-TYPE
创建新的磁盘卷标(分区表)
mkfs NUMBER FS-TYPE
在分区上建立文件系统
mkpart PART-TYPE [FS-TYPE] START END
创建一个分区
mkpartfs PART-TYPE FS-TYPE START END
创建分区,并建立文件系统
move NUMBER START END
移动分区
name NUMBER NAME
给分区命名
print [devices|free|list,all|NUMBER]
显示分区表、活动设备、空闲空间、所有分区
quit
退出
rescue START END
修复丢失的分区
resize NUMBER START END
修改分区大小
rm NUMBER
删除分区
select DEVICE
选择需要编辑的设备
set NUMBER FLAG STATE
改变分区标记
toggle [NUMBER [FLAG]]
切换分区表的状态
unit UNIT
设置默认的单位
Version
显示版本
关于mkpart与PART-TYPE(FS-TYPE)的解释:
Make a part-type partition for filesystem fs-type (if specified), beginning at start and ending at end (by default in megabytes). fs-type can be one of “btrfs”, “ext2”, “ext3”, “ext4”, “fat16”, “fat32”, “hfs”, “hfs+”, “linux-swap”, “ntfs”, “reiserfs”, or “xfs”. part-type should be one of “primary”, “logical”, or “extended”.
2.3.1 使用parted 2.3.1.1 列出所有块设备分区情况 # 查看当前有哪些块设备
[root@lb01 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 47G 0 part /
sdb 8:16 0 10G 0 disk
sr0 11:0 1 792M 0 rom
# 查看分区情况
[root@lb01 ~]# parted -l
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1075MB 1074MB primary xfs boot
2 1075MB 3223MB 2149MB primary linux-swap(v1)
3 3223MB 53.7GB 50.5GB primary xfs
Error: /dev/sdb: unrecognised disk label # sdb还没有分区
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
Warning: 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: 830MB
Sector size (logical/physical): 2048B/2048B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
2 885kB 37.7MB 36.8MB primary
2.3.1.2 创建分区
案例:在/dev/sdb中创建一个1GB的分区
a、命令行方式,创建一个主分区
# 转换为MBR分区方案msdos, GPT方案为gpt
[root@lb01 ~]# parted /dev/sdb mklabel msdos
Information: You may need to update /etc/fstab.
# 创建主分区
[root@lb01 ~]# parted /dev/sdb mkpart primary ext4 0GB 1GB # 把1GB换成100%就是分完剩余空间
Information: You may need to update /etc/fstab.
[root@lb01 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 47G 0 part /
sdb 8:16 0 10G 0 disk
└─sdb1 8:17 0 953M 0 part
sr0 11:0 1 792M 0 rom
b、再用交互方式,继续创建一个主分区
[root@lb01 ~]# parted
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) select /dev/sdb # 选择块设备
Using /dev/sdb
(parted) mkpart # 新建分区
Partition type? primary/extended? primary
File system type? [ext2]? ext4
Start? 1GB
End? 2GB
c、扩展分区与逻辑分区的创建
扩展分区最多只有一个,再创建会提示选择primary/logical
(parted) select /dev/sdb
Using /dev/sdb
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1000MB 999MB primary ext4
2 1000MB 4000MB 3000MB extended lba
# 给扩展分区2创建一个逻辑分区(使用范围只能在扩展分区2的范围内)
(parted) mkpart logical 1GB 2GB
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1000MB 999MB primary ext4
2 1000MB 4000MB 3000MB extended lba
5 1001MB 2000MB 998MB logical
# 给扩展分区2再创建一个逻辑分区
(parted) mkpart logical 2GB 3GB
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1000MB 999MB primary ext4
2 1000MB 4000MB 3000MB extended lba
5 1001MB 2000MB 998MB logical
6 2001MB 3000MB 999MB logical
2.3.1.3 查看分区 (parted) print # 打印分区
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1000MB 999MB primary
2 1000MB 2000MB 999MB primary
(parted) print free # 查看剩余空间
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
32.3kB 1049kB 1016kB Free Space
1 1049kB 1000MB 999MB primary
2 1000MB 2000MB 999MB primary
2000MB 10.7GB 8738MB Free Space
2.3.1.4 调整分区大小
将分区2从1GB扩展到2GB
PS:v2.4之后的版本,resize命令已经移除,更新到3.1-32版本有resizepart
[root@lb01 ~]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1000MB 999MB primary
2 1000MB 2000MB 999MB primary
# 调整分区2的大小为2GB
[root@lb01 ~]# parted /dev/sdb resizepart 2 3GB (3GB是指END位置,即使用1GB-3GB的空间,共2GB)
Information: You may need to update /etc/fstab.
[root@lb01 ~]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1000MB 999MB primary
2 1000MB 3000MB 2000MB primary
# 如果已经格式化并挂载使用,需要重新调整文件系统大小
[root@lb01 ~]# resize2fs /dev/sdb2
缩小分区大小会怎样?将提示有数据丢失的风险
[root@lb01 ~]# parted /dev/sdb resizepart 2 2GB
Warning: Shrinking a partition can cause data loss, are you sure you want to continue?
Yes/No?
2.3.1.5 删除分区 [root@lb01 ~]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1000MB 999MB primary
2 1000MB 4000MB 3000MB primary ext4
[root@lb01 ~]# parted /dev/sdb rm 2 // 删除2号分区,因为已挂载,会提示要先卸载
Error: Partition /dev/sdb2 is being used. You must unmount it before you modify it with Parted.
[root@lb01 ~]# umount /dev/sdb2
[root@lb01 ~]# parted /dev/sdb rm 2 // 删除成功
Information: You may need to update /etc/fstab.
[root@lb01 ~]# print
-bash: print: command not found
[root@lb01 ~]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1000MB 999MB primary
2.3.1.6 设置/更改分区标志 [root@lb01 ~]# parted /dev/sdb set 1 lvm on
Information: You may need to update /etc/fstab.
[root@lb01 ~]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1000MB 999MB primary lvm
查看有哪些标志可以设置
[root@lb01 ~]# parted
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help set
set NUMBER FLAG STATE change the FLAG on partition NUMBER
NUMBER is the partition number used by Linux. On MS-DOS disk labels, the primary partitions number from 1 to 4, logical partitions from 5 onwards.
FLAG is one of: boot, root, swap, hidden, raid, lvm, lba, hp-service, palo, prep, msftres, bios_grub, atvrecv, diag, legacy_boot
STATE is one of: on, off
2.3.16 GPT分区方案下的NAME标签 GPT分区的print
[root@lb01 ~]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
MBR分区的print
[root@lb01 ~]# parted /dev/sdb mklabel msdos
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? Yes
Information: You may need to update /etc/fstab.
[root@lb01 ~]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
可见GPT标志中多了一个Name标签
使用的时候GPT分区方式可以指定分区名,不用指定主分区、扩展分区这些类型
[root@lb01 ~]# parted /dev/sdb mkpart BOOT 0GB 1GB
Information: You may need to update /etc/fstab.
[root@lb01 ~]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 1000MB 999MB xfs BOOT
但是MBR就不能
[root@lb01 ~]# parted /dev/sdb mkpart BOOT 0GB 1GB
parted: invalid token: BOOT
Partition type? primary/extended?
[root@lb01 ~]# parted /dev/sdb mkpart primary BOOT 0GB 1GB
parted: invalid token: BOOT
File system type? [ext2]?
三、磁盘挂载 1、开机自动挂载
开机自动执行:/etc/rc.local
chmod +x /etc/ec.local
echo "mount /dev/sdc1 /root/test" >> /etc/rc.local
2、修改配置文件 /etc/fstab
/dev/sdc100 /opt xfs defaults 0 0
设备 挂载点 文件系统类型 挂载类型 是否备份 是否检测
UUID=1327e665-44b3-4223-a93b-69c36ec602f9 /root/oldboy xfs defaults 0 0
/dev/sdc100 等价于 UUID
3、查询设备UUID
[root@localhost ~]# blkid
/dev/sdc1: UUID="b5797ad6-9b98-452c-8962-fa12f6590fa6" TYPE="xfs" PARTLABEL="Linux filesystem"
四、创建SWAP分区 1、创建一个硬盘分区
2、制作swap分区
mkswap /dev/sdb3
3、激活swap
swapon /dev/sdb3
4、关闭swap
swapoff
5、自动挂载
5.1、
echo "mkswap /dev/sdb3" >> /etc/rc.local
echo "swapon /dev/sdb3" >> /etc/rc.local
5.2、
echo "/dev/sdb3 swap swap defaults 0 0" >> /etc/fstab