回忆之城
生命在于折腾
posts - 575,comments - 9,trackbacks - 0
这几天一直在琢磨GPT磁盘的分区表备份的问题,因为打算买个大点的硬盘来存资料。又因为担心数据安全的问题,有必要研究一下GPT磁盘的分区表备份和恢复。google了半天也没有个结果,就在virtualbox里面做了个试验。
思路是先建立一个GPT分区,备份分区表后拷点东西进去,然后破坏掉分区表,再恢复,看看里面的东西还在不在。系统就用刚出的中标麒麟6.0了(下来一天才弄下来的,官方没给HASH值,要吐槽一下)
1. [root@localhost ~]# parted /dev/sdb //用virtualbox虚拟的磁盘sdb来做试验
mklabel //建立分区表
gpt //使用GPT分区表
unit //选择读取方式
s //使用扇区来表示
mkpart //建立一个分区
分区名称? []? 1 //简单些就叫做1
文件系统类型 ? [ext2]? //我输入了ext4,结果没正确建立起来
起始点?2048 //从1M处开始,据说这样可以避免4k问题
结束点?-1 //所有磁盘分一个区
警告: You requested a partition from 2048s to 1023999s. 
The closest location we can manage is 2048s to 1023966s.
Is this still acceptable to you?
是/Yes/否/No? y 
(parted) p //查看一下,已经是GPT了,只是没格式化成我指定的ext4 
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 1024000s
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name 标志
1 2048s 1023966s 1021919s 1
(parted) q //退出parted

2. [root@localhost ~]# mkfs.ext4 /dev/sdb1 //格式化成ext4

mke2fs 1.41.10 (10-Feb-2009)
文件系统标签=
操作系统:Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
128016 inodes, 510956 blocks
25547 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=67633152
63 block groups
8192 blocks per group, 8192 fragments per group
2032 inodes per group
Superblock backups stored on blocks: 
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

正在写入inode表: 完成 
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

3. [root@localhost ~]# dd if=/dev/sdb of=/home/gg/sdb.img bs=512 skip=1 count=32 //备份分区表,跳过前面512字节的保护mbr,不知道这样是不是正确的
记录了32+0 的读入
记录了32+0 的写出
16384字节(16 kB)已复制,0.00189832 秒,8.6 MB/秒
4. [root@localhost ~]# mount /dev/sdb1 /home/gg/gz //然后把磁盘挂起来
5. [root@localhost ~]# cp /usr/share/sounds/K* /home/gg/gz/ //拷些系统自带的音频过去
6. [root@localhost ~]# umount /home/gg/gz //卸载掉
7. [root@localhost ~]# dd if=/home/gg/sdb.img of=/dev/sdb bs=512 count=32 seek=1 //把刚才备份的分区表恢复过去
记录了32+0 的读入
记录了32+0 的写出
16384字节(16 kB)已复制,0.00485221 秒,3.4 MB/秒
8. [root@localhost ~]# mount /dev/sdb1 /home/gg/gz //挂上去,果然文件都还能用,能放出声音来
9. [root@localhost ~]# dd if=/dev/urandom of=/dev/sdb bs=512 count=33 //把前面保护mbr和分区表都填了
记录了33+0 的读入
记录了33+0 的写出
16896字节(17 kB)已复制,0.00560489 秒,3.0 MB/秒
10. [root@localhost ~]# mount /dev/sdb1 /home/gg/gz //挂回去,奇怪数据还在,也能用
11. [root@localhost ~]# umount /home/gg/gz //卸载掉
12. [root@localhost ~]# parted /dev/sdb //再用parted看看
GNU Parted 2.1
(parted) p 
错误: /dev/sdb: unrecognised disk label //已经找不到了 
(parted) unit //换成扇区
Unit? [compact]? s 
(parted) p //还是找不到 
错误: /dev/sdb: unrecognised disk label 
(parted) q 
13.[root@localhost ~]# dd if=/dev/zero of=/dev/sdb bs=512 count=1 //做个填零
记录了1+0 的读入
记录了1+0 的写出
512字节(512 B)已复制,0.00410604 秒,125 kB/秒
14. [root@localhost ~]# parted /dev/sdb //再看,还是不认
GNU Parted 2.1
(parted) p 
错误: /dev/sdb: unrecognised disk label 
(parted) q
15. [root@localhost ~]# dd if=/home/gg/sdb.img of=/dev/sdb bs=512 count=32 seek=1 //写回备份的分区表,但是破坏时前面用了count=33,所以估计还是有错
记录了32+0 的读入
记录了32+0 的写出
16384字节(16 kB)已复制,0.00359624 秒,4.6 MB/秒
16. [root@localhost ~]# parted /dev/sdb //再看,已经发现GPT分区,但是没有保护MBR,就是差的那一块,转成扇区查看也是一样的结果
GNU Parted 2.1
(parted) p 
警告: /dev/sdb contains GPT signatures, indicating that it has a GPT table. However, it does not have a valid
fake msdos partition table, as it should. Perhaps it was corrupted -- possibly by a program that doesn't
understand GPT partition tables. Or perhaps you deleted the GPT table, and are now using an msdos partition
table. Is this a GPT partition table?
是/Yes/否/No? yes 
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 524MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name 标志
1 1049kB 524MB 523MB ext4 1

(parted) uint
align-check TYPE N check partition N for TYPE(min|opt) alignment
check NUMBER do a simple check on the file system
cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER copy file system to another partition
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkfs NUMBER FS-TYPE make a FS-TYPE file system on partition NUMBER
mkpart PART-TYPE [FS-TYPE] START END make a partition
mkpartfs PART-TYPE FS-TYPE START END make a partition with a file system
move NUMBER START END move partition NUMBER
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found
partitions, or a particular partition
quit exit program
rescue START END rescue a lost partition near START and END
resize NUMBER START END resize partition NUMBER and its file system
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU Parted
(parted) unit 
Unit? [compact]? s 
(parted) p //提示大概是已经识别GPT,但是没有找到保护MBR,就是前面多填的那块。 
警告: /dev/sdb contains GPT signatures, indicating that it has a GPT table. However, it does not have a valid
fake msdos partition table, as it should. Perhaps it was corrupted -- possibly by a program that doesn't
understand GPT partition tables. Or perhaps you deleted the GPT table, and are now using an msdos partition
table. Is this a GPT partition table?
是/Yes/否/No? yes 
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 1024000s
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name 标志
1 2048s 1023966s 1021919s ext4 1

(parted) mklabel //重新建一次 
新的磁盘标签类型? gpt 
警告: 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 
警告: /dev/sdb contains GPT signatures, indicating that it has a GPT table. However, it does not have a valid
fake msdos partition table, as it should. Perhaps it was corrupted -- possibly by a program that doesn't
understand GPT partition tables. Or perhaps you deleted the GPT table, and are now using an msdos partition
table. Is this a GPT partition table?
是/Yes/否/No? yes 
(parted) p 
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 524MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name 标志

(parted) unit
Unit? [compact]? s 
(parted) p 
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 1024000s
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name 标志

(parted) q 
信息: You may need to update /etc/fstab. 

17. [root@localhost ~]# dd if=/home/gg/sdb.img of=/dev/sdb bs=512 count=32 seek=1 //重新写回备份的分区表
记录了32+0 的读入
记录了32+0 的写出
16384字节(16 kB)已复制,0.00257806 秒,6.4 MB/秒
18.[root@localhost ~]# parted //再看,结果从分区角度看已经正常了
GNU Parted 2.1
使用 /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) unit 
Unit? [compact]? s 
(parted) select 
新设备? [/dev/sda]? /dev/sdb 
使用 /dev/sdb
(parted) p 
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 1024000s
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name 标志
1 2048s 1023966s 1021919s ext4 1

(parted) q 
19. [root@localhost ~]# mount /dev/sdb1 /home/gg/gz //准备挂回去,怎么没发现文件系统类型,要求要指定
mount: you must specify the filesystem type
20. [root@localhost ~]# mount -t ext4 /dev/sdb1 /home/gg/gz //指定了要挂回去,还是不行
mount: special device /dev/sdb1 does not exist
21. [root@localhost ~]# partprobe //让系统自己再识别一次
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (设备或资源忙). As a result, it may not reflect all of your changes until after reboot.
22. [root@localhost ~]# mount /dev/sdb1 /home/gg/gz //这次直接挂上去了,进去看看,所有文件都正常使用。

问题:不知道这种方法是不是正确的,主要是dd命令上有没有错误。
posted on 2014-04-07 20:17 回忆之城 阅读(569) 评论(0)  编辑 收藏 引用 所属分类: unix/linux
只有注册用户登录后才能发表评论。