centos7 gpt分区 centos7

分区怎么gpt分区格式化linux命令格式化教程

1.前言本案例假设Linux主机添加一块40G硬盘,目标分3个区,详细目标如下:

分区

容量

分区格式

挂载点

vdc1

20G

EXT4

/mnt/vdc1

vdc2

16G

LVM

合并入原来的根分区中,假设根分区是LVM

vdc3

4G

swap

swap(原来已经有4G)

2.使用fdisk分区

先使用fdisk-l查看分区情况

[root@zcwyou~]#fdisk-l输出如下,系统语言为中文:

磁盘/dev/vda:85.9 GB, 85899345920字节,167772160个扇区

Units=扇区 of 1* 512= 512 bytes

扇区大小(逻辑/物理):512字节/ 512字节

I/O大小(最小/最佳):512字节/ 512字节

磁盘标签类型:dos

磁盘标识符:0x000a718d

设备 Boot Start End Blocks Id System

/dev/vda1* 2048 1026047 512000 83 Linux

/dev/vda2 1026048 167772159 83373056 8e Linux LVM

磁盘/dev/mapper/centos-swap:4177 MB, 4177526784字节,8159232个扇区

Units=扇区 of 1* 512= 512 bytes

扇区大小(逻辑/物理):512字节/ 512字节

I/O大小(最小/最佳):512字节/ 512字节

磁盘/dev/mapper/centos-root:124.1 GB, 124138815488字节,242458624个扇区

Units=扇区 of 1* 512= 512 bytes

扇区大小(逻辑/物理):512字节/ 512字节

I/O大小(最小/最佳):512字节/ 512字节

磁盘/dev/vdb:42.9 GB, 42949672960字节,83886080个扇区

Units=扇区 of 1* 512= 512 bytes

扇区大小(逻辑/物理):512字节/ 512字节

I/O大小(最小/最佳):512字节/ 512字节

磁盘标签类型:dos

磁盘标识符:0x5c574df3

设备 Boot Start End Blocks Id System

/dev/vdb1 2048 83886079 41942016 8e Linux LVM

磁盘/dev/vdc:42.9 GB, 42949672960字节,83886080个扇区

Units=扇区 of 1* 512= 512 bytes

扇区大小(逻辑/物理):512字节/ 512字节

I/O大小(最小/最佳):512字节/ 512字节

可以看到目标磁盘大小为42.9GB,这是本次进行操作的目标磁盘。

使用查看分区信息

开始对/dev/vdc进行分区

[root@zcwyou~]#fdisk/dev/vdc操作过程如下:

欢迎使用 fdisk(util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。

使用写入命令前请三思。

Device does not contain a recognized partition table

使用磁盘标识符 0x02c834c1创建新的 DOS磁盘标签。

命令(输入 m获取帮助):m(这里输入m查看帮助)

命令操作

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 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)

使用fdisk分区

命令(输入 m获取帮助):n(输入n表示新建分区)

Partition type:

p primary(0 primary, 0 extended, 4 free)

e extended

Select(default p):默认是主分区,回车就行。

Using default response p

分区号(1-4,默认 1):默认创建分区,第1个。

起始扇区(2048-83886079,默认为 2048):

将使用默认值 2048

Last扇区,+扇区 or+size{K,M,G}(2048-83886079,默认为 83886079):+20G这里输入大小,即20G

分区 1已设置为 Linux类型,大小设为 20 GiB

接下来创建第2个分区,16G

命令(输入 m获取帮助):n(输入n表示新建分区)

Partition type:

p primary(1 primary, 0 extended, 3 free)

e extended

Select(default p):默认是主分区,回车就行。

Using default response p

分区号(2-4,默认 2):默认创建分区,第2个。

起始扇区(41945088-83886079,默认为 41945088):

将使用默认值 41945088

Last扇区,+扇区 or+size{K,M,G}(41945088-83886079,默认为 83886079):+16G这里输入大小,即16G

分区 2已设置为 Linux类型,大小设为 16 GiB

接下来创建第3个分区,剩下的所有空间

命令(输入 m获取帮助):n(输入n表示新建分区)

Partition type:

p primary(2 primary, 0 extended, 2 free)

e extended

Select(default p):

Using default response p

分区号(3,4,默认 3):创建第3个分区

起始扇区(75499520-83886079,默认为 75499520):

将使用默认值 75499520

Last扇区,+扇区 or+size{K,M,G}(75499520-83886079,默认为 83886079):不输入数字表示使用剩下所有的空间。

将使用默认值 83886079

分区 3已设置为 Linux类型,大小设为 4 GiB

接下来修改分区类型标记:

命令(输入 m获取帮助):t

分区号(1-3,默认 3):1

Hex代码(输入 L列出所有代码):83

已将分区“Linux”的类型更改为“Linux”

命令(输入 m获取帮助):t

分区号(1-3,默认 3):2

Hex代码(输入 L列出所有代码):8e

已将分区“Linux LVM”的类型更改为“Linux LVM”

命令(输入 m获取帮助):t

分区号(1-3,默认 3):3

Hex代码(输入 L列出所有代码):82

已将分区“Linux swap/ Solaris”的类型更改为“Linux swap/ Solaris”

83表示Linux分区,8e表示LVM分区,82表示Linux swap分区,标记而已。

使用w保存分区信息表,正式生效。

命令(输入 m获取帮助):w

The partition table has been altered!

Calling ioctl() to re-read partition table.

正在同步磁盘。

检查:

[root@zcwyou~]#lsblk/dev/vdc输出:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

vdc 252:32 0 40G 0 disk

├─vdc1 252:33 0 20G 0 part

├─vdc2 252:34 0 16G 0 part

└─vdc3 252:35 0 4G 0 part

fdisk分区常用参数

可以看到/dev/vdc的分区情况。

3.启用/mnt/vdc1的空间3.1格式化分区

[root@zcwyou~]#mkfs.ext4/dev/vdc1输出结果:

mke2fs 1.42.9(28-Dec-2013)

文件系统标签=

OS type: Linux

块大小=4096(log=2)

分块大小=4096(log=2)

Stride=0 blocks, Stripe width=0 blocks

1310720 inodes, 5242880 blocks

262144 blocks(5.00%) reserved for the super user

第一个数据块=0

Maximum filesystem blocks=2153775104

160 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,

4096000

Allocating group tables:完成

正在写入inode表:完成

Creating journal(32768 blocks):完成

Writing superblocks and filesystem accounting information:完成

当然,你可以使用mkfs.btrfs、mkfs.cramfs、mkfs.ext2、mkfs.ext3、mkfs.ext4、mkfs.minix、mkfs.xfs命令创建各种文件系统格式。

3.2创建挂载点

[root@zcwyou~]#mkdir-p/mnt/vdc13.3挂载文件系统到/mnt/vdc1/[root@zcwyou~]#mount/dev/vdc1/mnt/vdc1/检查:

[root@zcwyou~]#df-TH/mnt/vdc1文件系统类型容量已用可用已用%挂载点

/dev/vdc1 ext4 22G 47M 20G 1%/mnt/vdc1

3.4开机自动挂载

[root@zcwyou~]#vi/etc/fstab添加以下内容:

/dev/vdc1/mnt/vdc1 ext4 defaults 0 0

4.启用/dev/vdc2的空间

假设原来/分区空间不足,想把vdc2的16G加入根分区。前提条件是原来的根分区为LVM分区,可扩展。

这里/只使用了1%,假设已经快用完了需要扩展

检查目前根分区的使用情况

[root@zcwyou~]#df-TH输出结果:

文件系统类型容量已用可用已用%挂载点

/dev/mapper/centos-root xfs 125G 915M 124G 1%/

扫描当前pv,查看PV组的成员以及VG组的名字

[root@localhost~]#pvscanNo device found for PV Gtn22T-3yPk-zEIO-dlQe-TOv3-P67t-6Diyh1.

PV/dev/vda2 VG centos lvm2 [79.51 GiB/ 0 free]

PV/dev/vdb1 VG centos lvm2 [40.00 GiB/ 0 free]

Total: 2 [119.50 GiB]/ in use: 2 [119.50 GiB]/ in no VG: 0 [0 ]

扫描vg,看到vg组名字为centos

[root@zcwyou~]#vgscanReading all physical volumes. This may take a while…

Found volume group"centos" using metadata type lvm2

把vdc2加入PV组

[root@zcwyou~]#pvcreate/dev/vdc2No device found for PV Gtn22T-3yPk-zEIO-dlQe-TOv3-P67t-6Diyh1.

Physical volume"/dev/vdc2" successfully created

检查:

[root@zcwyou~]#pvscanNo device found for PV Gtn22T-3yPk-zEIO-dlQe-TOv3-P67t-6Diyh1.

PV/dev/vda2 VG centos lvm2 [79.51 GiB/ 0 free]

PV/dev/vdb1 VG centos lvm2 [40.00 GiB/ 0 free]

PV/dev/vdc2 lvm2 [16.00 GiB]

Total: 3 [135.50 GiB]/ in use: 2 [119.50 GiB]/ in no VG: 1 [16.00 GiB]

[root@zcwyou~]#pvdisplayNo device found for PV Gtn22T-3yPk-zEIO-dlQe-TOv3-P67t-6Diyh1.

--- Physical volume---

PV Name/dev/vda2

VG Name centos

PV Size 79.51 GiB/ not usable 3.00 MiB

Allocatable yes(but full)

PE Size 4.00 MiB

Total PE 20354

Free PE 0

Allocated PE 20354

PV UUID mGz4YB-CEvI-nf7k-bci4-R1RU-hRHH-xDyI4A

--- Physical volume---

PV Name/dev/vdb1

VG Name centos

PV Size 40.00 GiB/ not usable 3.00 MiB

Allocatable yes(but full)

PE Size 4.00 MiB

Total PE 10239

Free PE 0

Allocated PE 10239

PV UUID Y6eUTq-vl91-PQEH-QOfF-UBub-7LKE-6NoA6i

"/dev/vdc2" is a new physical volume of"16.00 GiB"

--- NEW Physical volume---

PV Name/dev/vdc2

VG Name

PV Size 16.00 GiB

Allocatable NO

PE Size 0

Total PE 0

Free PE 0

Allocated PE 0

PV UUID 4UwyQi-53dn-fO37-VEkp-tIte-mvEZ-CaZE3Q

把新分区空间加入到vg组

[root@zcwyou~]#vgextendcentos/dev/vdc2No device found for PV Gtn22T-3yPk-zEIO-dlQe-TOv3-P67t-6Diyh1.

Volume group"centos" successfully extended

检查vg组。

[root@zcwyou~]#vgdisplay--- Volume group---

VG Name centos

System ID

Format lvm2

Metadata Areas 3

Metadata Sequence No 6

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 2

Open LV 2

Max PV 0

Cur PV 3

Act PV 3

VG Size 135.50 GiB

PE Size 4.00 MiB

Total PE 34688

Alloc PE/ Size 30593/ 119.50 GiB

Free PE/ Size 4095/ 16.00 GiB

VG UUID 0UQsAO-01Ma-pJ1d-Fpfy-2bg8-jq9c-wVAVaw

看到vg组有16G空间空闲。

查看lv组空间,看到目前的lv root组为115.61g

[root@zcwyou~]#lvsLV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert

root centos-wi-ao---- 115.61g

swap centos-wi-ao---- 3.89g

扩展lv卷,把vg组空闲的16G空间合并到当前lv中。

[root@zcwyou~]#lvresize-l+100%FREE/dev/centos/rootExtending logical volume root to 131.61 GiB

Logical volume root successfully resized

再次查看当前vg组

[root@zcwyou~]#vgdisplay--- Volume group---

VG Name centos

System ID

Format lvm2

Metadata Areas 3

Metadata Sequence No 7

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 2

Open LV 2

Max PV 0

Cur PV 3

Act PV 3

VG Size 135.50 GiB

PE Size 4.00 MiB

Total PE 34688

Alloc PE/ Size 34688/ 135.50 GiB

Free PE/ Size 0/ 0

VG UUID 0UQsAO-01Ma-pJ1d-Fpfy-2bg8-jq9c-wVAVaw

看到空闲空间为0

查看lv大小,可以看到,lv root已经扩展到131.6g

[root@zcwyou~]# lvs

LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert

root centos-wi-ao---- 131.61g

swap centos-wi-ao---- 3.89g

再次核实:

[root@zcwyou~]#df-TH文件系统类型容量已用可用已用%挂载点

/dev/mapper/centos-root xfs 125G 915M 124G 1%/

5.启用/dev/vdc3的空间

查看当前swap的大小

[root@zcwyou~]#free-mtotal used free shared buffers cached

Mem: 3792 263 3528 8 1 99

-/+ buffers/cache: 162 3629

Swap: 3983 0 3983

可以看到当前的swap空间接近4G。

把/dev/sdc3格式化为swap

[root@zcwyou~]#mkswap/dev/vdc3正在设置交换空间版本 1,大小= 4193276 KiB

无标签,UUID=77d67410-8767-4404-895d-63110afaaba4

挂载空间到swap

[root@zcwyou~]#swapon/dev/vdc3没有信息输出即表示成功。

检查结果:

[root@zcwyou~]#free-mtotal used free shared buffers cached

Mem: 3792 266 3525 8 1 99

-/+ buffers/cache: 165 3626

Swap: 8078 0 8078

可以看到swap分区已经扩展到了8G。

修改/etc/fstab,下次开机自动挂载。

[root@zcwyou~]#vi/etc/fstab/dev/vdc3 swap defaults 0 0

修改fstab实现自动开机挂载

6.总结

fdisk是最传统分区管理工具,基本可以实现日常的使用需求。

linux系统的GPT分区

硬盘的分区格式主要有MBR和GPT两种。在Linux系统中,GPT分区相较于MBR分区具有以下优点:

(1)分区数量限制更低。GPT分区表中最多支持128个主分区。

(2)单个分区的容量几乎不受限制。单个分区最大支持1EB容量。

本文将使用“parted”工具进行GPT分区,该工具既能进行MBR分区,也能进行GPT分区。

1.输入“fdisk-l”查看硬盘识别情况,如“/dev/sdc”是新磁盘且未分区。

2.输入“parted/dev/sdc”启动parted工具,或者输入“parted”,然后“select/dev/sdc”。

3.输入“mklabel gpt”切换到GPT分区模式,再输入“print”查看是否切换成功。

4.输入“mkpart”或“mkpart分区名文件类型起始点结束点”,例如“mkpart test ext4 1 3000”。

5.输入“print”查看分区是否已存在。

6.输入“quit”退出parted,与MBR不同,需输入“w”写入保存分区表。

7.输入“ls/dev/硬盘名称”,输入“fdisk-l”不能看到GPT分区,fdisk只能看到msdos分区。

8.格式化刚新建的分区“mkfs.ext4/dev/sdc1”。

9.创建目录“mkdir/test”,挂载分区“mount/dev/sdc1/test/”。

10.设置开机自动挂载,可参考linux的分区实战。

默认是:

补充:

parted命令安装:

Debian:apt-get install parted

Ubuntu:apt-get install parted

Alpine:apk add parted

Arch Linux:pacman-S parted

Kali Linux:apt-get install parted

CentOS:yum install parted

Fedora:dnf install parted

Raspbian:apt-get install parted

Docker:docker run cmd.cat/parted parted

CentOS6.5安装的UEFI-GPT回退为MBR引导详解

我自己在服务器上装了centos 6.5系统,默认用的就是UEFI,没有注意可以只用GRUB,接着问题就出现了,我在服务器内安装了另外一块带grub启动系统的硬盘,

这个硬盘启动系统后,如果去掉此块硬盘,再重新启动,总是识别不出来centos 6.5的UEFI,这个可以通过启动主板的UEFI,然后用UEFI的命令:

mount fs0

fs0:

cd EFI

cd redhat

grub.efi

文件夹名称可能不准,大概是这样就可以手动启动系统了,另外还可以通过插入系统盘,upgrade一下就可以了,不用每次手动,也不会丢失数据,但是总觉得

这样不是很好。所以我就有了将UEFI-GPT回退为MBR的想法。

谷歌总是很牛x,所以我得到的结果是用gdisk,我启动另一个系统,挂载上centos 6.5的硬盘,然后按照google上的步骤:

1

Click Applications. Point to Accessories, then click Terminal. Input the following into Terminal: sudo fdisk-l

2

Press Enter to see the disk devices connected to the computer. The device with the asterisk next to it is the boot device. Write down the filename(for example,/dev/sda) assigned to the disk.

3

Input the following into Terminal: sudo apt-get install gdisk. Press Enter to install the partitioning tool to Ubuntu.

4

Input sudo gdisk into Terminal, then press Enter to run gdisk as a superuser. Input the name assigned to the boot device, then press Enter again.

5

Press r, then hit Enter, to switch to the recovery and transformation options. Press g, then press Enter, to select the option to convert GPT to MBR on Linux.

6

Press w, then press Enter, to convert GPT to MBR on the primary partitions. Press y, then Enter, to finalize and exit.

操作完成后,重启发现系统起不来,手动引导UEFI也不行了。

人类一思考,上帝就发笑,可是俺思考后发现还是能拯救的,

先提前说明,centos 6.5安装的时候,是分了三个区的,sda1-- UEFI所在区,是fat32,sda2--在fdisk-l下显示是NTFS(但实际上是ext4)的存储实际系统和数据的分区,

sda3-- swap分区。

我进入U盘启动的系统后,把sda1分区挂载到mnt目录内,发现在efi-redhat目录内,存在一个文件grub.conf,相比经常把玩系统的人都知道这个是干嘛的。就是有了这个,我才有了希望。

下面就是拯救的具体步骤:

1、mount上sda1,然后拷贝出grub.conf留作后用,切忌!然后umount

2、使用fdisk将sda1修改为83-linux,sda2也修改为83-linux,并且把sda1设置为启动分区。

大致命令流程如下: fdisk/dev/sda t 1 83 t 2 83 a 1

3、将sda1做成ext3,因为他是fat32

命令:mkfs.ext3/dev/sda1

4、mount上sda1和sda2,我这里sda1在/mnt,sda2在/sda2,给sda1安装grub,grub-install--root-directory=/mnt/dev/sda,这样grub就装好了

5、把备份的grub.conf拷贝到/mnt/boot/grub目录下,并且做一个软连接,menu.lst指向grub.conf,命令是:ln-s grub.conf menu.lst

6、将/sda2/boot/目录下在grub.conf内用到的文件,一个是kernel会用到的,一个是initrd会用到的,拷贝到/mnt/boot目录下

7、修改/mnt/boot/grub/grub.conf,把root(hd0, 1)这个改为root(hd0,0)

8、保存,sync,umount,卸载硬盘,重新启动就可以了。

阅读剩余
THE END