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

你可能感兴趣的:(8月4日 磁盘分区管理)