gdisk_常用操作

前言

传统的老牌的分区工具是fdisk,但是fdisk出道太早,只支持MBR(Master Boot Record),并不支持GPT(GUID Partition Table),无法操作超过2T的磁盘,因此gdisk parted等分区工具横空出世。

gdisk和parted都曾经用过,但是我更喜欢gdisk,因为使用上,gdisk 上承fdisk,没有太多的学习负担。另外我们QA测出过,parted方式分区,在某些使用上会有问题。

使用方法

首先,我有一个sdd,作为我操作的对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
root@test:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 30.5M 0 part
├─sda2 8:2 0 488.3M 0 part
├─sda3 8:3 0 31.1G 0 part /
├─sda4 8:4 0 8G 0 part [SWAP]
└─sda5 8:5 0 387.8M 0 part
sdb 8:16 0 50G 0 disk
├─sdb1 8:17 0 4G 0 part
└─sdb2 8:18 0 46G 0 part /data/osd.4
sdc 8:32 0 50G 0 disk
├─sdc1 8:33 0 4G 0 part
└─sdc2 8:34 0 46G 0 part /data/osd.5
sdd 8:48 0 500G 0 disk
sr0 11:0 1 1.6G 0 rom

查看help信息

进入交互模式之后,输入h可以查看帮助信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
root@test:~# gdisk /dev/sdd
GPT fdisk (gdisk) version 0.8.1

Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): h
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):

查看分区表信息

很明显,做出没有分区,所以分区表为空,后面创建分区之后,可以通过p命令查看分区信息
1
2
3
4
5
6
7
8
9
10
11
12
Command (? for help): p
Disk /dev/sdd: 1048576000 sectors, 500.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): D3328858-7A3F-4A64-BC46-A7040F306F33
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 1048575330 sectors (500.0 GiB)

Number Start (sector) End (sector) Size Code Name

Command (? for help):

创建分区

打算将500G的空间划分成2个分区,分别是100G和400G: 创建分区是用命令n,你需要选择

1.分区number
2.起始扇区
3.结束扇区

起始扇区可以敲回车选择默认值,而结束扇区用+100G这种方式来决定分区大小为100G 分区后,可以用p命令查看最新的分区表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Command (? for help): n    
Partition number (1-128, default 1): 1
First sector (34-1048575660, default = 34) or {+-}size{KMGTP}:
Information: Moved requested sector from 34 to 2048 in
order to align on 2048-sector boundaries.
Use 'l' on the experts' menu to adjust alignment
Last sector (2048-1048575660, default = 1048575660) or {+-}size{KMGTP}: +100G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdd: 1048576000 sectors, 500.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): D3328858-7A3F-4A64-BC46-A7040F366F33
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 83886013 sectors (40.0 GiB)

Number Start (sector) End (sector) Size Code Name
1 2048 20973567 100.0 GiB 8300 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}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdd: 1048576000 sectors, 500.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): D3328858-7A3F-4A64-BC46-A7040F366F33
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 1048575660
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number Start (sector) End (sector) Size Code Name
1 2048 209735677 100.0 GiB 8300 Linux filesystem
2 209735678 1048575660 400.0 GiB 8300 Linux filesystem

Command (? for help):

设置分区标签信息partlabel

有些时候,磁盘的盘符会漂移,使用sdx这种方式来分辨磁盘是不靠谱,使用lable这种方式是可靠的。那么如何创建分区标签呢?
c 是用来设置partlable的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Command (? for help): c 
Partition number (1-2): 1
Enter name: test1

Command (? for help): c
Partition number (1-2): 2
Enter name: test2

Command (? for help): p
Disk /dev/sdd: 1048576000 sectors, 500.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): D3328858-7A3F-4A64-BC46-A7040F366F33
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 1048575660
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number Start (sector) End (sector) Size Code Name
1 2048 209735677 100.0 GiB 8300 bean_part1
2 209735678 1048575660 400.0 GiB 8300 bean_part2

保存分区信息

我们将磁盘分成了2个区,给每一个分区贴上了partlabel,但是退出gdisk之前必须保存,否则前功尽弃。
1
2
3
4
5
6
7
8
9
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).
The operation has completed successfully.
root@test:~#

效果

愉快地查看分区效果吧:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
root@test:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 30.5M 0 part
├─sda2 8:2 0 488.3M 0 part
├─sda3 8:3 0 31.1G 0 part /
├─sda4 8:4 0 8G 0 part [SWAP]
└─sda5 8:5 0 387.8M 0 part
sdb 8:16 0 50G 0 disk
├─sdb1 8:17 0 4G 0 part
└─sdb2 8:18 0 46G 0 part /data/osd.4
sdc 8:32 0 50G 0 disk
├─sdc1 8:33 0 4G 0 part
└─sdc2 8:34 0 46G 0 part /data/osd.5
sdd 8:48 0 500G 0 disk
├─sdd1 8:49 0 100G 0 part
└─sdd2 8:50 0 400G 0 part
sr0 11:0 1 1.6G 0 rom
root@test:~# ll /dev/disk/by-partlabel/
total 0
drwxr-xr-x 2 root root 180 Apr 1 22:23 ./
drwxr-xr-x 9 root root 180 Apr 1 21:34 ../
lrwxrwxrwx 1 root root 10 Apr 1 22:23 test1 -> ../../sdd1
lrwxrwxrwx 1 root root 10 Apr 1 22:23 test2 -> ../../sdd2
...
-------------本文结束感谢您的阅读-------------
0%