Linux系統下磁盤分區與管理

磁盤分區與管理

1、Linux系統設備文件

crw-rw----. 1 root root    253,  0 Apr 18 15:31 rtc0   #字符文件

lrwxrwxrwx. 1 root root           3 Apr 18 15:31 scd0 -> sr0

brw-rw----. 1 root disk      8,  0 Apr 18 15:31 sda    #設備文件

brw-rw----. 1 root disk      8,  1 Apr 18 15:31 sda1

brw-rw----. 1 root disk      8,  2 Apr 18 15:31 sda2

brw-rw----. 1 root disk      8,  3 Apr 18 15:31 sda3

crw-rw----. 1 root disk     21,   0 Apr 18 15:31 sg0

(1)設備類型:

    1)塊設備:block,存取單位“塊”,磁盤

    2)字符設備:char,存取單位“字符”,鍵盤

(2)設備文件:關聯至一個設備驅動程序,進而能夠跟與之對應硬件設備進行通信

(3)設備號碼:

    1)主設備號:major number, 標識設備類型

    2)次設備號:minor number, 標識同一類型下的不同設備

wKioL1j5_AXR8psSAAB41im4Up4590.jpg-wh_50

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 in

512+0 records out

512 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 in

512+0 records out

512 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 bytes

255 heads, 63 sectors/track, 2610 cylinders

Units = cylinders of 16065 * 512 = 8225280bytes

Sector size (logical/physical): 512 bytes /512 bytes

I/O size (minimum/optimal): 512 bytes / 512bytes

Disk identifier: 0x00000000

[root@dayi123-6 ~]$dd if=mbr of=/dev/sda  #恢復分區表信息

1+0 records in

1+0 records out

512 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”命令即可打開)

wKiom1j5_MXw2F7bAAC8UyycTUY966.png-wh_50

   2)fdisk分區:

    分區方法:fdisk /dev/sda(設備名稱)

    作用:MBR分區工具

[root@dayi123 ~]$lsblk     #查看現有的磁盤信息

NAME  MAJ:MIN RM  SIZE RO TYPEMOUNTPOINT

sda     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 ~]$lsblk

NAME  MAJ:MIN RM  SIZE RO TYPEMOUNTPOINT

sda     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

sdb     8:16   0   20G  0disk

[root@dayi123 ~]$fdisk /dev/sdb

……

Command (m for help): m

Command 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     #磁盤號選擇4

First cylinder (1-2610, default 1):  #選擇開始柱面(默認從1開始)

Using default value 1

Last 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 1

Last cylinder, +cylinders or +size{K,M,G}(1-1306, default 1306): +5G   

 

Command (m for help): P      #打印當前的磁盤分區情況

 

Disk /dev/sdb: 21.5 GB, 21474836480 bytes

255 heads, 63 sectors/track, 2610 cylinders

Units = cylinders of 16065 * 512 = 8225280bytes

Sector size (logical/physical): 512 bytes /512 bytes

I/O size (minimum/optimal): 512 bytes / 512bytes

Disk identifier: 0xe5e9eac4

 

  Device Boot      Start         End      Blocks  Id  System

/dev/sdb4               1        1306   10490413+   5  Extended

/dev/sdb5               1         654    5253192   83  Linux

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

    3)gdisk分區工具

    分區方法:fdisk /dev/sda(設備名稱)

    作用:類fdisk 的GPT分區工具

[root@dayi123 ~]# gdisk /dev/sdb

GPT 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 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): 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.5GB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

Disk 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.1

Using /dev/sdb

Welcome 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.5GB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

Disk 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 busy

error adding partition 4

BLKPG: Device or resource busy

error 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


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