Linux运维*一.Linux基础---13、Linux磁盘管理

一、磁盘管理基础

01、设备文件:关联至一个设备驱动程序的文件,进而能够跟与之关联的邮件设备进行通信

 

02、设备文件类型:

块设备:block,通信存取单位为块(如硬盘)

字符设备:char,通信存取单位为字节(如键盘)

 

03、设备文件路径:/dev/*

 

04、设备号码:

主设备号码:major number,标识设备类型

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

示例:

[root@localhost user01]# ll /dev/

total 0

crw-rw----. 1 root video 10, 175 Feb 24 12:05 agpgart

crw-------. 1 root root 10, 235 Feb 24 12:05 autofs

agpgart的主设备号为10,次设备号为175;

autofs的主设备号为10,次设备号为235;

agpgart与autofs主设备一致,为同一种设备类型

 

05、磁盘的接口类型:

  并行接口:

SCSI(640MB/s)工业级磁盘,寿命长

IDE(133MB/s)

 

  串行接口:

USB(480MB/s)

SAS(大于6Gbps)

SATA(6Gbps=(6000/8)MB/s)

串行接口速率大于并行接口,接口速率:SAS>SATA>SCSI>IDE 

 

06、磁盘设备文件的命名:

/dev/sd:(SAS、SATA、SCSI、USB、IDE)

有的系统会将IDE接口的文件表示为:/dev/hd

不同设备:

/dev/sda 、 /dev/sdb 、/dev/sdc ...

同一磁盘设备的不同分区:以sdb设备的不同分区为例

/dev/sdb1 、 /dev/sdb2 、 /dev/sdb3...

 

07、磁盘构造:机械马达+转子,马达带动固定在转子上的多片磁盘转动

   磁片:固定在转子上的带磁性的盘片,双面都有磁道,用于存储数据

(一般一个转子上有多片磁片)

   磁道(track):磁片上的同轴圆道、用于储存数据,并且从内到外有编号

   柱面(cylinder):不同磁片或不同盘面上相同编号的磁道组成的圆柱形柱面

   扇区(sector):每个磁道划分出的扇形区域

(扇区有固定的储存字节:512bytes,新型磁盘更大)

  

08、0磁道0扇区(MBR):有512bytes,不允许被分区

                    446bytes存放引导加载项(boot loader)

                    64bytes存放分区表(16bytes表示一个分区,所以早期一个磁盘只能分为4个主分区)

                    2bytes存MBR的有效性标识(4位十六进制数,55AA,表示MBR正常)

 

 

09、BIOS、UEFI及MBR、GPT

   BIOS及UEFI都是基本的输入输出系统,计算机上电时,由BIOS管控硬件,当Kernel加载后才由kernel接管硬件,UEFI是BIOS的升级版本

   MBR及GPT是磁盘引导系统,MBR只支持2T以下的磁盘而GPT支持8ET的磁盘,UEFI支持GPT磁盘引导而BIOS只支持MBR磁盘引导

 

 

 

二、磁盘分区:分区的实质是将不同的连续的柱面划分为不同的分区

    1、扩展分区:扩展分区不当作储存空间,而是指向其它的逻辑分区,用于挂载逻辑分区

(一个扩展分区可以包含多个逻辑分区)

  

2、 磁盘分区的方案:

4个主分区

                   3个主分区+1个扩展分区(N个逻辑分区)

                   2个主分区+2个扩展分区(2N个逻辑分区)

                   1个主分区+3个扩展分区(3N个逻辑分区)

 

3、磁盘及磁盘分区的设备文件:

不同物理磁盘设备:/dev/sda 、/dev/sdb 、/dev/sdc...

                            同一物理磁盘的不同主分区:/dev/sda1 、/dev/sda2 、/dev/sda3 、/dev/sda4 

(只能有4个主分区,主分区用1~4表示)

                            同一物理磁盘的不同逻辑分区:/dev/sda5 、/dev/sda6 、/dev/sda7... 

(逻辑分区用5+表示)

 

 

 

 

三、磁盘分区管理工具

fdisk:对於单块硬盘,fdisk只能管理15个分区

fdisk -l /dev/指定磁盘: 查看指定磁盘的分区情况(不给指定磁盘则显示所有磁盘分区)

[root@localhost ~]# fdisk -l /dev/sda

 

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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

Disk label type: dos

Disk identifier: 0x000ec556

 

Device Boot Start End Blocks Id System

/dev/sda1 * 2048 2099199 1048576 83 Linux

/dev/sda2 2099200 41943039 19921920 8e Linux LVM

 

 

fdisk /dev/指定设备 :进入交互界面对指定设备进行分区管理

交互界面子命令:

p : 显示已有分区

n : 新建分区

d : 删除分区

w: 保存分区设置并退出

q:放弃分区设置并退出

m:获取帮助

l :列出所有分区id(id表示用于那种系统的文件系统)

t : 调整分区id

 

示例:

[root@localhost ~]# fdisk /dev/sda #对/dev/sda进行分区管理

Welcome to fdisk (util-linux 2.23.2).

 

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.

 

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

g create a new empty GPT partition table

G create an IRIX (SGI) partition table

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)

 

Command (m for help): l # 列出分区id

0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris

1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-

2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-

3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-

4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx

5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data

6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .

7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility

8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt

9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access

a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O

b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor

c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs

e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT

f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/

10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b

11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor

12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor

14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary

16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS

17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE

18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto

1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep

1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT

1e Hidden W95 FAT1 80 Old Minix

 

Command (m for help): p #列出已有分区

 

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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

Disk label type: dos

Disk identifier: 0x000ec556

 

Device Boot Start End Blocks Id System

/dev/sda1 * 2048 2099199 1048576 83 Linux

/dev/sda2 2099200 41943039 19921920 8e Linux LVM

 

Command (m for help): d #删除分区

Partition number (1,2, default 2): 2 #删除2号主分区

Partition 2 is deleted

 

Command (m for help): p # 显示已有分区

 

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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

Disk label type: dos

Disk identifier: 0x000ec556

 

Device Boot Start End Blocks Id System

/dev/sda1 * 2048 2099199 1048576 83 Linux

 

Command (m for help): n #新建分区

Partition type:

p primary (1 primary, 0 extended, 3 free) # 新建主分区(已经有1个主分区,还可以新建3个)

e extended # 新建扩展分区

Select (default p): e

Partition number (2-4, default 2): 2

First sector (2099200-41943039, default 2099200):

Using default value 2099200

Last sector, +sectors or +size{K,M,G} (2099200-41943039, default 41943039): +10G # 在默认起始扇区+10分配给新建分区

Partition 2 of type Extended and of size 10 GiB is set

 

Command (m for help): p #显示以后分区

 

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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

Disk label type: dos

Disk identifier: 0x000ec556

 

Device Boot Start End Blocks Id System

/dev/sda1 * 2048 2099199 1048576 83 Linux

/dev/sda2 2099200 23070719 10485760 5 Extended

 

Command (m for help): w #保存退出

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

 

RNING: Re-reading the partition table failed with error 16: Device or resource busy.

The kernel still uses the old table. The new table will be used at

the next reboot or after you run partprobe(8) or kpartx(8)

Syncing disks. #警告设备忙,内核仍使用旧分区,下一次重启使用新的分区

 

 

cat /proc/partitions 查看内核分区使用情况(分区是否被使用以该命令结果为准)

[root@localhost ~]# cat /proc/partitions

major minor #blocks name

 

8 0 20971520 sda

8 1 1048576 sda1

8 2 19921920 sda2

11 0 4365312 sr0

253 0 17821696 dm-0

253 1 2097152 dm-1

 

 

partx -a /dev/指定设备 #让内核重新扫描硬盘,设备新分区,不指定磁盘,则扫描所有磁盘

kpartx -a /dev/指定设备 #让内核重新扫描硬盘,设备新分区(-f 强制执行,存在风险)

示例:

新的磁盘分区情况:

[root@localhost ~]# fdisk -l /dev/sda

 

Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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

Disk label type: dos

Disk identifier: 0x000ba415

 

Device Boot Start End Blocks Id System

/dev/sda1 * 2048 2099199 1048576 83 Linux

/dev/sda2 2099200 41943039 19921920 8e Linux LVM

/dev/sda3 41943040 44040191 1048576 83 Linux

/dev/sda4 44040192 54525951 5242880 5 Extended

/dev/sda5 44042240 46139391 1048576 83 Linux

/dev/sda6 46141440 48238591 1048576 83 Linux

 

查看内核是否使用了新的分区:未使用

[root@localhost ~]# cat /proc/partitions

major minor #blocks name

 

11 0 4365312 sr0

8 0 31457280 sda

8 1 1048576 sda1

8 2 19921920 sda2

253 0 17821696 dm-0

253 1 2097152 dm-1

 

partx -a使内核重新扫描加载磁盘分区

[root@localhost ~]# partx -a /dev/sda

partx: /dev/sda: error adding partitions 1-2

[root@localhost ~]# partx -a /dev/sda

partx: /dev/sda: error adding partitions 1-6

 

查看内核是否使用了新的分区:已使用

[root@localhost ~]# cat /proc/partitions

major minor #blocks name

 

11 0 4365312 sr0

8 0 31457280 sda

8 1 1048576 sda1

8 2 19921920 sda2

8 3 1048576 sda3

8 4 1 sda4

8 5 1048576 sda5

8 6 1048576 sda6

253 0 17821696 dm-0

253 1 2097152 dm-1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章