8月4日 磁盘分区管理

1、如何复制设备文件

设备号码:
主设备号:major number, 标识设备类型
次设备号:minor number, 标识同一类型下的不同设备

[root@centos6 app]#cp -a /dev/zero .
[root@centos6 app]#ll
total 0
crw-rw-rw-. 1 root root 1, 5 Jul 31 13:25 zero
[root@centos6 app]#ll /dev/zero
crw-rw-rw-. 1 root root 1, 5 Jul 31 13:25 /dev/zero
[root@centos6 app]#mknod /app/zero2 c 1 5  ---c表示字符设备 1和5分别表示主设备号和次设备号
[root@centos6 app]#ll
total 0
crw-rw-rw-. 1 root root 1, 5 Jul 31 13:25 zero
crw-r--r--. 1 root root 1, 5 Jul 31 17:06 zero2
[root@centos6 app]#ll /dev/sda1
brw-rw----. 1 root disk 8, 1 Jul 31 13:25 /dev/sda1
[root@centos6 app]#mknod /app/sda1 b 8 1
[root@centos6 app]#ll
total 0
brw-r--r--. 1 root root 8, 1 Jul 31 17:15 sda1
crw-rw-rw-. 1 root root 1, 5 Jul 31 13:25 zero
crw-r--r--. 1 root root 1, 5 Jul 31 17:06 zero2
[root@centos6 app]#mount /app/sda1 /mnt/sda
[root@centos6 app]#df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2       50264772 4783956  42920816  11% /
tmpfs             502056     224    501832   1% /dev/shm
/dev/sda3       40185208   49016  38088192   1% /app
/dev/sda1         991512   34904    905408   4% /boot
/dev/sr0         3878870 3878870         0 100% /media/CentOS_6.9_Final
/app/sda1         991512   34904    905408   4% /mnt/sda
[root@centos6 app]#cd /mnt/sda/
[root@centos6 sda]#ls
config-2.6.32-696.el6.x86_64  grub                                 lost+found                        System.map-2.6.32-696.el6.x86_64
efi                           initramfs-2.6.32-696.el6.x86_64.img  symvers-2.6.32-696.el6.x86_64.gz  vmlinuz-2.6.32-696.el6.x86_64

总结:复制设备文件可以用cp-a,也可以用mknod,复制分区后可以用于挂载,进入挂载的目录,看到的内容和复制的分区内容是一样的。

2、parted 管理分区

  • 创建gpt分区
[root@centos6 ~]#parted /dev/sdb mklabel gpt  ---声明是要创建gpt分区
[root@centos6 ~]#parted /dev/sdb mkpart primary 1 1024 ---创建分区
Information: You may need to update /etc/fstab.                           
[root@centos6 ~]#parted /dev/sdb print  ---打印分区
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number  Start   End     Size    File system  Name     Flags
 1      1049kB  1024MB  1023MB               primary
[root@centos6 ~]#parted /dev/sdb mkpart primary 1025 2048  ---创建第二个分区
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? ignore
Information: You may need to update /etc/fstab.                                                                            
[root@centos6 ~]#parted /dev/sdb print  ---打印分区
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number  Start   End     Size    File system  Name     Flags
 1      1049kB  1024MB  1023MB               primary
 2      1025MB  2048MB  1023MB               primary
root@centos6 ~]#dd if=/dev/zero of=/dev/sdb bs=1 count=512   ---破坏gtp分区的前512个字节
512+0 records in
512+0 records out
512 bytes (512 B) copied, 0.00419461 s, 122 kB/s
[root@centos6 ~]#parted /dev/sdb print   ---打印分区后仍然有分区表
Warning: /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: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number  Start   End     Size    File system  Name     Flags
 1      1049kB  1024MB  1023MB               primary
 2      1025MB  2048MB  1023MB               primary

总结:gpt分区破坏前512个字节后仍然有分区表,因为gpt分区后面还要备份的分区表,并不是都在0扇区内,因此gpt分区比较安全。

  • 创建MBR分区
[root@centos6 ~]#parted /dev/sdb mklabel msdos  ---指定是要创建MBR分区
[root@centos6 ~]#parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos   ---显示标签为mbr分区
Number  Start  End  Size  Type  File system  Flags
[root@centos6 ~]#parted /dev/sdb mkpart extend 1 1024 ---创建一个扩展分区
Information: You may need to update /etc/fstab.                           
[root@centos6 ~]#parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1024MB  1023MB  extended               lba                                                            
[root@centos6 ~]#parted /dev/sdb mkpart primary 1025 2048 ---创建一个主分区
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore                                                     
Information: You may need to update /etc/fstab.                           
[root@centos6 ~]#parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1024MB  1023MB  extended               lba
 2      1025MB  2048MB  1023MB  primary
[root@centos6 ~]#parted /dev/sdb mkpart logical 5 500 ---创建一个逻辑分区
Information: You may need to update /etc/fstab.                           
[root@centos6 ~]#parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1024MB  1023MB  extended               lba
 5      5243kB  500MB   495MB   logical
 2      1025MB  2048MB  1023MB  primary
[root@centos6 ~]#dd if=/dev/zero of=/dev/sdb bs=1 count=64 skip=446 seek=446  ---破坏分区表的64个字节
64+0 records in
64+0 records out
64 bytes (64 B) copied, 0.0014034 s, 45.6 kB/s
[root@centos6 ~]#fdisk -l /dev/sdb  ---此时已经没有分区表信息
Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e89e3 ---仍然显示有分区。只是不显示分区表
   Device Boot      Start         End      Blocks   Id  System
[root@centos6 ~]#hexdump -C /dev/sdb -n 512  ---55A标记位还有
00000000  fa b8 00 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |................|
00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00  |...|.........!..|
00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75  |....8.u........u|
00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 01 8b  |.........|...t..|
00000040  4c 02 cd 13 ea 00 7c 00  00 eb fe 00 00 00 00 00  |L.....|.........|
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  e3 89 0e 00 00 00 00 00  |................|
000001c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200
[root@centos6 ~]#dd if=/dev/zero of=/dev/sdb bs=1 count=66 skip=446 seek=446  ---破坏66个字节后55A标记位也被破坏
66+0 records in
66+0 records out
66 bytes (66 B) copied, 0.00180802 s, 36.5 kB/s
[root@centos6 ~]#hexdump -C /dev/sdb -n 512
00000000  fa b8 00 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |................|
00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00  |...|.........!..|
00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75  |....8.u........u|
00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 01 8b  |.........|...t..|
00000040  4c 02 cd 13 ea 00 7c 00  00 eb fe 00 00 00 00 00  |L.....|.........|
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  e3 89 0e 00 00 00 00 00  |................|
000001c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000200
[root@centos6 ~]#fdisk -l /dev/sdb  ---此时不显示分区信息
Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e89e3
[root@centos6 ~]#parted /dev/sdb rm 5  ---删除分区

总结:MBR分区破坏分区表后就无法显示分区表,分区就被破坏,但仍然显示有分区。只有破坏55A标记位后才彻底显示没有分区了。
parted对gpt和mbr分区都可以进行管理和创建,但的操作都是实时生效的,小心使用。

3、复制一个磁盘的分区给另外一个磁盘

[root@centos6 ~]#lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sr0     11:0    1   3.7G  0 rom  /media/CentOS_6.9_Final
sda      8:0    0   200G  0 disk 
├─sda1   8:1    0  1000M  0 part /boot
├─sda2   8:2    0  48.8G  0 part /
├─sda3   8:3    0  39.1G  0 part /app
├─sda4   8:4    0     1K  0 part 
└─sda5   8:5    0     2G  0 part [SWAP]
sdb      8:16   0   100G  0 disk 
├─sdb1   8:17   0   976M  0 part 
├─sdb2   8:18   0 975.6M  0 part 
├─sdb3   8:19   0   1.9G  0 part 
└─sdb4   8:20   0   1.8G  0 part 
sdc      8:32   0   150G  0 disk 
sdd      8:48   0    80G  0 disk 
[root@centos6 ~]#dd if=/dev/sda of=mbr bs=1 count=512 ---备份/dev/sda 的分区表信息
512+0 records in
512+0 records out
512 bytes (512 B) copied, 0.00149843 s, 342 kB/s
[root@centos6 ~]#ls
acl.txt          Documents  f3                  mbr       Public
anaconda-ks.cfg  Downloads  install.log         Music     Templates
Desktop          f1         install.log.syslog  Pictures  Videos
[root@centos6 ~]#dd if=mbr of=/dev/sdb ---将/dev/sda分区表信息拷贝到/dev/sdb
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.00501896 s, 102 kB/s
[root@centos6 ~]#fdisk -l /dev/sdb   ---可以看出只复制了四个分区,第五个逻辑分区并没有覆盖
Warning: invalid flag 0x0000 of partition table 5 will be corrected by w(rite)
Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000838ae
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1         128     1024000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sdb2             128        6502    51200000   83  Linux
/dev/sdb3            6502       11601    40960000   83  Linux
/dev/sdb4           11601       26109   116530176    5  Extended

总结:MBR分区的0磁道0扇区只保存主分区和扩展分区的分区表,逻辑分区的分区表在扩展分区的0扇区,不在前512的字节内,每一个逻辑分区的前面都有一个分区表,用来指明逻辑分区从哪到哪。

4、fdisk管理MBR分区

[root@centos6 ~]#fdisk /dev/sdb
Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   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管理MBR分区很方便,根据提示一步一步的完成就可以,如果记不住可以输入m看帮助信息。

5、gdisk管理gpt分区

[root@centos6 ~]#gdisk /dev/sdc
GPT fdisk (gdisk) version 0.8.10

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

Creating new GPT entries.

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): 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容

  • 1 概述 本文通过对讲解了磁盘的基本概念,分区,创建文件系统,挂载,卸载等步骤进行讲解。使得用户能够创建基本的磁盘...
    ghbsunny阅读 1,417评论 0 0
  • 目录磁盘结构磁盘分区磁盘分区管理文件系统管理挂载 一、磁盘结构 (一)设备文件 设备文件:linux系统下一切皆文...
    哈喽别样阅读 943评论 0 0
  • 导读目录 硬盘的组成 硬盘的物理结构主要针对的是机械硬盘及其内部的结构加以介绍,以下内容可能不是硬盘内部全部的部件...
    香吉矢阅读 3,910评论 0 12
  • 1. 磁盘分区信息存储的两种形式 常见磁盘分区存储形式类型有两种:MBR(MSDOS)和GPT。 1.1 什么是M...
    天之蓝色阅读 3,832评论 0 1
  • 理论部分 镜像 一、镜像的百科定义 1、维基百科 ISO映像是一种光盘的存档文件(英语:archive file)...
    幻影翔阅读 1,708评论 4 3