磁盘分区与管理
1、Linux系统设备文件
crw-rw----. 1 root root 253, 0 Apr 18 15:31 rtc0 #字符文件lrwxrwxrwx. 1 root root 3 Apr 18 15:31 scd0 -> sr0brw-rw----. 1 root disk 8, 0 Apr 18 15:31 sda #设备文件brw-rw----. 1 root disk 8, 1 Apr 18 15:31 sda1brw-rw----. 1 root disk 8, 2 Apr 18 15:31 sda2brw-rw----. 1 root disk 8, 3 Apr 18 15:31 sda3crw-rw----. 1 root disk 21, 0 Apr 18 15:31 sg0
(1)设备类型:
1)块设备:block,存取单位“块”,磁盘
2)字符设备:char,存取单位“字符”,键盘
(2)设备文件:关联至一个设备驱动程序,进而能够跟与之对应硬件设备进行通信
(3)设备号码:
1)主设备号:major number, 标识设备类型
2)次设备号:minor number, 标识同一类型下的不同设备
2、磁盘分区
(1)磁盘分区的作用:优化I/O性能、实现磁盘空间配额限制、提高修复速度、隔离系统和程序、安装多个OS、采用不同文件系统
(2)磁盘分区原理——MBR分区
1)磁盘分区就是对0磁道0扇区的前446字节后面接下来的64字节的分区表进行设置,
2)一块硬盘的分区表仅有64bytes大小,仅支持4个分区(主分区+扩展分区)
3)磁盘分区的最小单位为柱面
4)扩展分区不能直接使用,还需在扩展分区的基础上创建逻辑分区才行,
5)扩展分区有自己的分区表,因此,扩展分区可以有多个
[root@dayi123-6 ~]$hexdump -C /dev/sda -n 512–v #查看磁盘分区表前512字节00000000 eb 48 90 10 8e d0 bc 00 b0 b8 0000 8e d8 8e c0 |.H..............|……000001f0 00 00 00 00 00 00 00 00 00 00 0000 00 00 55 aa |..............U.|00000200[root@dayi123-6 ~]$fdisk –l #查看磁盘分区…… Device Boot Start End Blocks Id System/dev/sda1 * 1 26 204800 83 Linux……[root@dayi123-6 ~]$dd if=/dev/sda of=mbr bs=1count=512 #备份分区表512+0 records in512+0 records out512 bytes (512 B) copied, 0.00286277 s, 179kB/s[root@dayi123-6 ~]$dd if=/dev/zeroof=/dev/sda bs=1 count=512 #破坏分区表512+0 records in512+0 records out512 bytes (512 B) copied, 0.0781299 s, 6.6kB/s[root@dayi123-6 ~]$hexdump -C /dev/sda -n 512–v #分区表信息已被破坏00000000 00 00 00 00 00 00 00 00 00 00 0000 00 00 00 00 |................|00000010 00 00 00 00 00 00 00 00 00 00 0000 00 00 00 00 |................|……[root@dayi123-6 ~]$fdisk –l #看不到分区信息Disk /dev/sda: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280bytesSector size (logical/physical): 512 bytes /512 bytesI/O size (minimum/optimal): 512 bytes / 512bytesDisk identifier: 0x00000000[root@dayi123-6 ~]$dd if=mbr of=/dev/sda #恢复分区表信息1+0 records in1+0 records out512 bytes (512 B) copied, 0.00351096 s, 146kB/s
(3)磁盘分区原理——GPT分区
1)GPT:GUID(Globals Unique Identifiers)partition table 支持128个分区,使用64位,支持8Z(512Byte/block)64Z (4096Byte/block)
2)使用128位UUID(UniversallyUnique Identifier) 表示磁盘和分区GPT分区表自动备份在头和尾两份,并有CRC校验位
3)UEFI (统一扩展固件接口)硬件支持GPT,使操作系统启动
(3)磁盘分区工具——GPT分区
1)gnome-disks(一种图形界面的分区工具,在图形界面终端中输入”gnome-disks”命令即可打开)
2)fdisk分区:
分区方法:fdisk /dev/sda(设备名称)
作用:MBR分区工具
[root@dayi123 ~]$lsblk #查看现有的磁盘信息NAME MAJ:MIN RM SIZE RO TYPEMOUNTPOINTsda 8:0 0 20G 0disk├─sda1 8:1 0 200M 0 part /boot├─sda2 8:2 0 2G 0 part [SWAP]└─sda3 8:3 0 17.8G 0 part /sr0 11:0 1 3.7G 0rom /mnt/cdrom #如果新添加的磁盘在虚拟机上识别不到,可用下面命令进行扫描识别 [root@dayi123 ~]$echo '- - -'>/sys/class/scsi_host/host0/scan[root@dayi123 ~]$lsblkNAME MAJ:MIN RM SIZE RO TYPEMOUNTPOINTsda 8:0 0 20G 0disk├─sda1 8:1 0 200M 0 part /boot├─sda2 8:2 0 2G 0 part [SWAP]└─sda3 8:3 0 17.8G 0 part /sr0 11:0 1 3.7G 0rom /mnt/cdromsdb 8:16 0 20G 0disk[root@dayi123 ~]$fdisk /dev/sdb……Command (m for help): mCommand action a toggle a bootable flag #设置可引导标识 b edit bsd disklabel #编辑bsd磁盘标签 c toggle the dos compatibilityflag #设置dos系统兼容标记 d delete a partition #删除一个分区 l list known partition types #显示已知Linux磁盘分区类型,83为Linux分区 m print this menu #显示帮助信息 n add a new partition #创建一个新的分区 o create a new empty DOSpartition table p print the partition table # 打印现有磁盘分区信息 q quit without savingchanges #退出 s create a new empty Sundisklabel #新建空的sun磁盘标签 t change a partition's system id #改变一个分区的系统ID u change display/entry units #改变显示记录单位 v verify the partition table #验证分区表 w write table to disk and exit #保存退出 x extra functionality (expertsonly) #附加功能Command (m for help): n #创建一个分区Command action e extended p primary partition (1-4)e #选择扩展分区(选择主分区或扩展分区,p为主分区)Partition number (1-4): 4 #磁盘号选择4First cylinder (1-2610, default 1): #选择开始柱面(默认从1开始)Using default value 1Last cylinder, +cylinders or +size{K,M,G}(1-2610, default 2610): +10G#分区大小 Command (m for help): n #继续分区Command action l logical (5 or over) p primary partition (1-4)l #此时只能选择扩展分区和逻辑分区First cylinder (1-1306, default 1):Using default value 1Last cylinder, +cylinders or +size{K,M,G}(1-1306, default 1306): +5G Command (m for help): P #打印当前的磁盘分区情况 Disk /dev/sdb: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280bytesSector size (logical/physical): 512 bytes /512 bytesI/O size (minimum/optimal): 512 bytes / 512bytesDisk identifier: 0xe5e9eac4 Device Boot Start End Blocks Id System/dev/sdb4 1 1306 10490413+ 5 Extended/dev/sdb5 1 654 5253192 83 LinuxCommand (m for help): w #选择w保存退出
3)gdisk分区工具
分区方法:fdisk /dev/sda(设备名称)
作用:类fdisk 的GPT分区工具
[root@dayi123 ~]# gdisk /dev/sdbGPT fdisk (gdisk) version 0.8.6 Partition table scan: MBR:not present BSD:not present APM:not present GPT:not present Command (? for help): ?b back up GPT data to a filec change a partition's named delete a partitioni show detailed information on a partitionl list known partition typesn add a new partitiono create a new empty GUID partition table (GPT)p print the partition tableq quit without saving changesr recovery and transformation options (experts only)s sort partitionst change a partition's type codev verify diskw write table to disk and exitx extra functionality (experts only)? print this menuCommand (? for help): n #创建一个分区Partition number (1-128, default 1): 1 #选择分区号First sector (34-41943006, default = 2048) or{+-}size{KMGTP}: #选择开始扇区Last sector (2048-41943006, default =41943006) or {+-}size{KMGTP}: +2G #大小Current type is 'Linux filesystem'Hex code or GUID (L to show codes, Enter =8300):Changed type of partition to 'Linuxfilesystem' Command (? for help): P #打印当前分区信息…… Number Start (sector) End(sector) Size Code Name 1 2048 4196351 2.0 GiB 8300 Linux filesystem
4)parted分区工具
作用:磁盘分区管理工具,比fdisk更加灵活,功能更加丰富,同时支持GUID分区表,支持交互模式和非交互模式,除了能够进行分区的添加,删除操作外,还可以移动分区,制作文件系统,调整文件系统大小,复制文件系统。
用法:parted [选项]... [设备[命令[参数]...]...]
非交互模式分区:
用法:
parted /dev/sdb mklabel gpt|msdos #标记磁盘分区格式
parted/dev/sdb print #打印当前磁盘分区
parted/dev/sdb mkpart primary 1 200 (默认M)#分区
parted /dev/sdb rm 1 #删除分区
parted –l
[root@dayi123 ~]# parted /dev/sdb mklabelgpt #将/dev/sdb标记为gpt分区 Warning: The existing disk label on /dev/sdbwill be destroyed and all data on this disk will be lost. Do you want tocontinue?Yes/No? yes Information: You may need to update/etc/fstab. [root@dayi123 ~]# parted /dev/sdb mkpart primary1 200 #创建一个200M的分区 Information: You may need to update/etc/fstab. [root@dayi123 ~]# parted /dev/sdb print #打印当前的分区 Model: VMware, VMware Virtual S (scsi)Disk /dev/sdb: 21.5GBSector size (logical/physical): 512B/512BPartition Table: gptDisk Flags: Number Start End Size File system Name Flags 1 1049kB 200MB 199MB primary [root@dayi123 ~]# parted /dev/sdb rm 1 #删除第一块分区Information: You may need to update/etc/fstab.
交互模式分区(同fdisk):
[root@dayi123 ~]# parted /dev/sdb #使用parted交互式分区GNU Parted 3.1Using /dev/sdbWelcome to GNU Parted! Type 'help' to view alist of commands.(parted) help #显示提示信息 align-check TYPE N check partition N for TYPE(min|opt) alignment help[COMMAND] printgeneral help, or help on COMMAND mklabel,mktable LABEL-TYPE create a new disklabel (partition table) mkpart PART-TYPE [FS-TYPE] START END make a partition nameNUMBER NAME namepartition 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 rmNUMBER delete partition NUMBER select DEVICE choose the device to edit disk_set FLAG STATE change the FLAG on selected device disk_toggle [FLAG] toggle the state of FLAG on selected device setNUMBER FLAG STATE change the FLAG on partition NUMBER toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER unitUNIT set the default unit to UNIT version display the version number and copyright information of GNU Parted(parted) mktable gpt #将此硬盘标记为gpt分区(parted) mkpart primary 1 200 #创建一个200M的分区 (parted) print #打印创建的分区 Model: VMware, VMware Virtual S (scsi)Disk /dev/sdb: 21.5GBSector size (logical/physical): 512B/512BPartition Table: gptDisk Flags: Number Start End Size File system Name Flags 1 1049kB 200MB 199MB primary(parted) rm 1 #删除一个分区,rm后面跟分区号(parted) quit #parted创建分区时实时生效的,不需要保存退出即可
3、同步分区表
(1)查看分区表
1)查看内核内核是否已经识别新的分区:cat /proc/partations
2)查看块文件(加载到内存中的分区信息):lsblk
3)查看磁盘中的分区信息:fdisk –l
parted–l
(2)有些时候分区完时不能立即查看内存中及内核中的分区信息,需要重新读取硬盘分区表
1)centos6重新读取硬盘分区表
新增分区用:
partx -a/dev/DEVICE
删除分区用:
partx -d –nr M-N/dev/DEVICE
2)centos7重读分区表:partprobe
partprobe[/dev/DEVICE]
[root@dayi123 ~]$partx -a /dev/sdb #重读分区表BLKPG: Device or resource busyerror adding partition 4BLKPG: Device or resource busyerror adding partition 5[root@dayi123 ~]$cat /proc/partitions/dev/sdb #查看加载到内核中分区major minor #blocks name 8 0 20971520 sda 8 1 204800 sda1 8 2 2097152 sda2 8 3 18668544 sda3 8 16 20971520 sdb 8 20 1 sdb4 8 21 5253192 sdb5[root@dayi123 ~]$fdisk -l /dev/sdb #查看硬盘分区…… Device Boot Start End Blocks Id System/dev/sdb4 1 1306 10490413+ 5 Extended/dev/sdb5 1 654 5253192 83 Linux[root@dayi123 ~]$parted -l /dev/sdb #查看硬盘分区 ……Number Start End Size Type File system Flags 1 1049kB 211MB 210MB primary ext4 boot 2 211MB 2358MB 2147MB primary linux-swap(v1) 3 2358MB 21.5GB 19.1GB primary ext4……Number Start End Size Type File system Flags 4 32.3kB 10.7GB 10.7GB extended 5 64.5kB 5379MB 5379MB logical