GPT分区相关的操作

查看一下gpt分区相关的操作. 这里只是记录, 不做详细说明.

!!!
注意!! 如果不明白命令的功能. 千万不要执行下面提到的命令!!!
执行相关命令的后果自负!!!
!!!

*本文全部做了假设.认为设备是512个字节/扇区.
*没心思排版. 看着可能是惨点儿…

################## 我准备的磁盘
dev="/dev/vdb"
#读出/proc/partitions里的块设备大小.计算共多少字节大.
#size= ( ( 3221225472 ∗ 1024 ) ) s e c t o r = ((3221225472*1024)) sector= ((32212254721024))sector=((3221225472*2))

####################软件环境.
debian10

SMP Debian 4.19.67-2+deb10u2 (2019-11-11) x86_64 GNU/Linux
fdisk 2.33.1-0.1
parted 3.2-25
bvi 1.4.0-1+b3
diffutils 1:3.7-3
file 1:5.35-4+deb10u1
gdisk 1.0.3-1.1

################### 按这样先分个区
Created a new GPT disklabel (GUID: 3F180574-1456-8549-8263-270DAA0EC382).

Partition number (1-128, default 1):
First sector (2048-6442450910, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-6442450910, default 6442450910): +1T
Created a new partition 1 of type ‘Linux filesystem’ and of size 1 TiB.

Partition number (2-128, default 2):
First sector (2147485696-6442450910, default 2147485696):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2147485696-6442450910, default 6442450910):
Changed type of partition ‘Linux filesystem’ to ‘Linux swap’.

ls -l /dev/disk/by-partuuid/
lrwxrwxrwx 1 root root 10 Oct 18 14:57 c0758af6-2493-644c-946c-90207dd6046b -> …/…/vdb1
lrwxrwxrwx 1 root root 10 Oct 18 14:57 a02f40da-1e0a-2b40-ac87-0ff70cc67bf2 -> …/…/vdb2

################### 我先把几个部分读出来.

================
读mbr兼容部分.
#dd if=$dev of=0.sec bs=512 count=1

#fdisk -l ./0.sec
Disk ./0.sec: 512 B, 512 bytes, 1 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
Disklabel type: dos
Disk identifier: 0x00000000

Device Boot Start End Sectors Size Id Type
./0.sec1 1 4294967295 4294967295 2T ee GPT

file 0.sec

0.sec: DOS/MBR boot sector, extended partition table (last)

这个文件应该是通用的.应该没有特殊的数据.

====================
读gpt头上的lba1
#dd if=$dev of=1.sec bs=512 count=1 skip=1

file 1.sec

1.sec: GPT data structure (nonstandard: at LBA 0), version 1.0, GUID: 3f180574-1456-8549-8263-270daa0ec382, disk size: 6442450944 sectors (sector size unknown)

====================
读gtp分区表 每扇区4个. 32*4=128个?
dd if=$dev of=2_33.sec bs=512 count=32 skip=2

在头上可以找到上面分的两个 分区uuid 和 类型uuid. 能判断读出的内窬是不是正确.

====================
读lba -1
dd if= d e v o f = f 1. s e c b s = 512 c o u n t = 1 s k i p = dev of=f1.sec bs=512 count=1 skip= devof=f1.secbs=512count=1skip=(($sector-1))

值得注意的是. -1和1是不同的. 原因见这个扇区的各字段说明.
有本扇区位置.另一个复本扇区位置. crc校验.

====================
读lba -33到-2 这部分可以用来恢复2-33? 目前感觉是可以.

diff 2_33.sec f2_f33.sec

相同

#################### debian 10中 fdisk中列出的 分区类型 uuid

1 EFI System C12A7328-F81F-11D2-BA4B-00A0C93EC93B
2 MBR partition scheme 024DEE41-33E7-11D3-9D69-0008C781F39F
3 Intel Fast Flash D3BFE2DE-3DAF-11DF-BA40-E3A556D89593
4 BIOS boot 21686148-6449-6E6F-744E-656564454649
5 Sony boot partition F4019732-066E-4E12-8273-346C5641494F
6 Lenovo boot partition BFBFAFE7-A34F-448A-9A5B-6213EB736C22
7 PowerPC PReP boot 9E1A2D38-C612-4316-AA26-8B49521E5A8B
8 ONIE boot 7412F7D5-A156-4B13-81DC-867174929325
9 ONIE config D4E6E2CD-4469-46F3-B5CB-1BFF57AFC149
10 Microsoft reserved E3C9E316-0B5C-4DB8-817D-F92DF00215AE
11 Microsoft basic data EBD0A0A2-B9E5-4433-87C0-68B6B72699C7
12 Microsoft LDM metadata 5808C8AA-7E8F-42E0-85D2-E1E90434CFB3
13 Microsoft LDM data AF9B60A0-1431-4F62-BC68-3311714A69AD
14 Windows recovery environment DE94BBA4-06D1-4D40-A16A-BFD50179D6AC
15 IBM General Parallel Fs 37AFFC90-EF7D-4E96-91C3-2D7AE055B174
16 Microsoft Storage Spaces E75CAF8F-F680-4CEE-AFA3-B001E56EFC2D
17 HP-UX data 75894C1E-3AEB-11D3-B7C1-7B03A0000000
18 HP-UX service E2A1E728-32E3-11D6-A682-7B03A0000000
19 Linux swap 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F
20 Linux filesystem 0FC63DAF-8483-4772-8E79-3D69D8477DE4
21 Linux server data 3B8F8425-20E0-4F3B-907F-1A25A76F98E8
22 Linux root (x86) 44479540-F297-41B2-9AF7-D131D5F0458A
23 Linux root (ARM) 69DAD710-2CE4-4E3C-B16C-21A1D49ABED3
24 Linux root (x86-64) 4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709
25 Linux root (ARM-64) B921B045-1DF0-41C3-AF44-4C6F280D3FAE
26 Linux root (IA-64) 993D8D3D-F80E-4225-855A-9DAF8ED7EA97
27 Linux reserved 8DA63339-0007-60C0-C436-083AC8230908
28 Linux home 933AC7E1-2EB4-4F13-B844-0E14E2AEF915
29 Linux RAID A19D880F-05FC-4D3B-A006-743F0F84911E
30 Linux extended boot BC13C2FF-59E6-4262-A352-B275FD6F7172
31 Linux LVM E6D6D379-F507-44C2-A23C-238F2A3DF928
32 FreeBSD data 516E7CB4-6ECF-11D6-8FF8-00022D09712B
33 FreeBSD boot 83BD6B9D-7F41-11DC-BE0B-001560B84F0F
34 FreeBSD swap 516E7CB5-6ECF-11D6-8FF8-00022D09712B
35 FreeBSD UFS 516E7CB6-6ECF-11D6-8FF8-00022D09712B
36 FreeBSD ZFS 516E7CBA-6ECF-11D6-8FF8-00022D09712B
37 FreeBSD Vinum 516E7CB8-6ECF-11D6-8FF8-00022D09712B
38 Apple HFS/HFS+ 48465300-0000-11AA-AA11-00306543ECAC
39 Apple UFS 55465300-0000-11AA-AA11-00306543ECAC
40 Apple RAID 52414944-0000-11AA-AA11-00306543ECAC
41 Apple RAID offline 52414944-5F4F-11AA-AA11-00306543ECAC
42 Apple boot 426F6F74-0000-11AA-AA11-00306543ECAC
43 Apple label 4C616265-6C00-11AA-AA11-00306543ECAC
44 Apple TV recovery 5265636F-7665-11AA-AA11-00306543ECAC
45 Apple Core storage 53746F72-6167-11AA-AA11-00306543ECAC
46 Solaris boot 6A82CB45-1DD2-11B2-99A6-080020736631
47 Solaris root 6A85CF4D-1DD2-11B2-99A6-080020736631
48 Solaris /usr & Apple ZFS 6A898CC3-1DD2-11B2-99A6-080020736631
49 Solaris swap 6A87C46F-1DD2-11B2-99A6-080020736631
50 Solaris backup 6A8B642B-1DD2-11B2-99A6-080020736631
51 Solaris /var 6A8EF2E9-1DD2-11B2-99A6-080020736631
52 Solaris /home 6A90BA39-1DD2-11B2-99A6-080020736631
53 Solaris alternate sector 6A9283A5-1DD2-11B2-99A6-080020736631
54 Solaris reserved 1 6A945A3B-1DD2-11B2-99A6-080020736631
55 Solaris reserved 2 6A9630D1-1DD2-11B2-99A6-080020736631
56 Solaris reserved 3 6A980767-1DD2-11B2-99A6-080020736631
57 Solaris reserved 4 6A96237F-1DD2-11B2-99A6-080020736631
58 Solaris reserved 5 6A8D2AC7-1DD2-11B2-99A6-080020736631
59 NetBSD swap 49F48D32-B10E-11DC-B99B-0019D1879648
60 NetBSD FFS 49F48D5A-B10E-11DC-B99B-0019D1879648
61 NetBSD LFS 49F48D82-B10E-11DC-B99B-0019D1879648
62 NetBSD concatenated 2DB519C4-B10E-11DC-B99B-0019D1879648
63 NetBSD encrypted 2DB519EC-B10E-11DC-B99B-0019D1879648
64 NetBSD RAID 49F48DAA-B10E-11DC-B99B-0019D1879648
65 ChromeOS kernel FE3A2A5D-4F32-41A7-B725-ACCC3285A309
66 ChromeOS root fs 3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC
67 ChromeOS reserved 2E0A753D-9E48-43B0-8337-B15192CB1B5E
68 MidnightBSD data 85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7
69 MidnightBSD boot 85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7
70 MidnightBSD swap 85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7
71 MidnightBSD UFS 0394EF8B-237E-11E1-B4B3-E89A8F7FC3A7
72 MidnightBSD ZFS 85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7
73 MidnightBSD Vinum 85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7
74 Ceph Journal 45B0969E-9B03-4F30-B4C6-B4B80CEFF106
75 Ceph Encrypted Journal 45B0969E-9B03-4F30-B4C6-5EC00CEFF106
76 Ceph OSD 4FBD7E29-9D25-41B8-AFD0-062C0CEFF05D
77 Ceph crypt OSD 4FBD7E29-9D25-41B8-AFD0-5EC00CEFF05D
78 Ceph disk in creation 89C57F98-2FE5-4DC0-89C1-F3AD0CEFF2BE
79 Ceph crypt disk in creation 89C57F98-2FE5-4DC0-89C1-5EC00CEFF2BE
80 VMware VMFS AA31E02A-400F-11DB-9590-000C2911D1B8
81 VMware Diagnostic 9D275380-40AD-11DB-BF97-000C2911D1B8
82 VMware Virtual SAN 381CFCCC-7288-11E0-92EE-000C2911D0B2
83 VMware Virsto 77719A0C-A4A0-11E3-A47E-000C29745A24
84 VMware Reserved 9198EFFC-31C0-11DB-8F78-000C2911D1B8
85 OpenBSD data 824CC7A0-36A8-11E3-890A-952519AD3F61
86 QNX6 file system CEF5A9AD-73BC-4601-89F3-CDEEEEE321A1
87 Plan 9 partition C91818F9-8025-47AF-89D2-F030D7000C2C

===============================

磁盘前面分区数据破坏后. 利用gdisk用备份恢复分区信息.

dd if=/dev/zero of=$dev bs=512 count=34


fdisk $dev

Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.


gdisk $dev

GPT fdisk (gdisk) version 1.0.3

Caution: invalid main GPT header, but valid backup; regenerating main header
from backup!

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

Found invalid MBR and corrupt GPT. What do you want to do? (Using the
GPT MAY permit recovery of GPT data.)
1 - Use current GPT
2 - Create blank GPT

Your answer: 1

Command (? for help): p
Disk /dev/vdb: 6442450944 sectors, 3.0 TiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 3F180574-1456-8549-8263-270DAA0EC382
Partition table holds up to 128 entries
Main partition table begins at sector 2016 and ends at sector 2047
First usable sector is 2048, last usable sector is 6442450910
Partitions will be aligned on 2048-sector boundaries
Total free space is 0 sectors (0 bytes)

Number Start (sector) End (sector) Size Code Name
1 2048 2147485695 1024.0 GiB 8300
2 2147485696 6442450910 2.0 TiB 8200

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) to /dev/vdb.
The operation has completed successfully.


fdisk -l $dev

Disk /dev/vdb: 3 TiB, 3298534883328 bytes, 6442450944 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
Disklabel type: gpt
Disk identifier: 3F180574-1456-8549-8263-270DAA0EC382

Device Start End Sectors Size Type
/dev/vdb1 2048 2147485695 2147483648 1T Linux filesystem
/dev/vdb2 2147485696 6442450910 4294965215 2T Linux swap

这只是一个简便的操作.有恢复命令可用. 继续看下面.
####################
使用gdisk 用恢复命令 重建 backup GPT

dd if=/dev/zero of= d e v b s = 512 c o u n t = 33 s e e k dev bs=512 count=33 seek devbs=512count=33seek(($sector-33))


fdisk -l $dev

The backup GPT table is corrupt, but the primary appears OK, so that will be used.
Disk /dev/vdb: 3 TiB, 3298534883328 bytes, 6442450944 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
Disklabel type: gpt
Disk identifier: 3F180574-1456-8549-8263-270DAA0EC382

Device Start End Sectors Size Type
/dev/vdb1 2048 2147485695 2147483648 1T Linux filesystem
/dev/vdb2 2147485696 6442450910 4294965215 2T Linux swap

partprobe

Error: The backup GPT table is corrupt, but the primary appears OK, so that will be used.

gdisk $dev

GPT fdisk (gdisk) version 1.0.3

Caution: invalid backup GPT header, but valid main header; regenerating
backup header from main header.

Warning! Main and backup partition tables differ! Use the ‘c’ and ‘e’ options
on the recovery & transformation menu to examine the two tables.

Warning! One or more CRCs don’t match. You should repair the disk!

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


Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk
verification and recovery are STRONGLY recommended.


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

Recovery/transformation command (? for help): ?
b use backup GPT header (rebuilding main)
c load backup partition table from disk (rebuilding main)
d use main GPT header (rebuilding backup)
e load main partition table from disk (rebuilding backup)
f load MBR and build fresh GPT from it
g convert GPT into MBR and exit
h make hybrid MBR
i show detailed information on a partition
l load partition data from a backup file
m return to main menu
o print protective MBR data
p print the partition table
q quit without saving changes
t transform BSD disklabel partition
v verify disk
w write table to disk and exit
x extra functionality (experts only)
? print this menu

Recovery/transformation command (? for help): d

Recovery/transformation 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) to /dev/vdb.
The operation has completed successfully.
root@ldap:/home/gpt# fdisk -l $dev
Disk /dev/vdb: 3 TiB, 3298534883328 bytes, 6442450944 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
Disklabel type: gpt
Disk identifier: 3F180574-1456-8549-8263-270DAA0EC382

Device Start End Sectors Size Type
/dev/vdb1 2048 2147485695 2147483648 1T Linux filesystem
/dev/vdb2 2147485696 6442450910 4294965215 2T Linux swap

####################
一切都这么顺利!!!
如果需要这些操作. 我们也许只是需要一个好的 live linux了.

你可能感兴趣的:(迷糊地,linux)