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

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