vmware做Centos磁盤分區管理實驗

    在linux運維工作中,熟練掌握磁盤管理相關技能是對運維人員最基本的要求。最近在學習系統管理的內容,而磁盤分區是其中比較重要的一部分內容。本實驗所用linux系統爲Centos6.6,涉及到的知識爲linux磁盤分區。

 

一、在實驗前必須先要了解磁盤管理的基本知識

1MBRMaster Boot Record(主引導記錄)wKiom1XepPPSlPV8AAHbG2Vwguw008.jpg


         MBR爲磁盤第0磁道第0扇區,共512字節,從上圖可以知道,Bootloader佔據446字節,而Partition分區表佔據了64字節,每個分區需要16字節,所以分區表只能標識4個分區的信息。那要想建立多餘4個分區怎麼辦呢?辦法就是3+1(N),即建立3個主分區(Primary )和一個擴展分區(extended),而擴展分區裏劃分了N個邏輯分區(logical),擴展分區本身並不可用,裏面包含的邏輯分區纔是可用的分區。

 

2磁盤設備文件的命名規則

IDE硬盤:/dev/hd

SCSI,SATA,SAS,USB等存儲設備:/dev/sd#

不同設備:a-z          #例如/dev/sda, /dev/sdb, ...

同一設備上的不同分區:1,2, ...    #例如/dev/sda1, ... /dev/sda5…

 

3、文件系統類別

1) 各平臺文件系統類別

      Linux文件系統:

    ext2,ext3, ext4, xfs, btrfs, reiserfs, jfs

    swap:交換分區

    光盤:iso9660

    Windows:fat32,ntfs

    網絡文件系統:NFS,CIFS

    集羣文件:GFS2,OCFS2

    分佈式文件系統:ceph,moosefs,mogilefs,GlusterFS,Lustre

 

2根據其是否支持“journal”功能;

       日誌型文件系統:ext3, ext4, xfs ...

      非日誌型:ext2,vfat

 

3) linux文件系統的組成部分:

內核中的模塊:ext4,xfs, vfat

用戶空間的管理工具:mkfs.ext2,mkfs.ext4, mkfs.xfs,mkfs,vfat

 

4常用的分區管理工具有: fdiskparted, sfdisk。在這裏使用的是fdisk工具進行分區。

       相關語法:fdisk[OPTION]DEVICE

OPTION

   -l:列出給定設備的分區表信息,如果後面不指定DEVICE

則列出從/proc/partitions 中獲取到的信息。

  -b:設定扇區大小(1024,2048,4096...)

子命令:管理功能

p:print,顯示已有分區

n:new,創建

d:delete,刪除

w:write,寫入磁盤並退出

q:退出

m:獲取幫助

l:列表所有分區的類型id

t:調整分區id


二、實驗

1在新建分區前,先用命令fdisk -l查看分區情況。磁盤在系統中命名爲/dev/sda,假如再添加多一塊磁盤,系統會顯示新磁盤爲/dev/sdb。由命令結果可看出系統中已存在2個主分區。

[root@localhost ~]#fdisk -l
 
Disk /dev/sda: 85.9GB, 85899345920 bytes
255 heads, 63sectors/track, 10443 cylinders
Units = cylinders of16065 * 512 = 8225280 bytes
Sector size(logical/physical): 512 bytes / 512 bytes
I/O size(minimum/optimal): 512 bytes / 512 bytes
Disk identifier:0x0002791b
 
   Device Boot      Start         End      Blocks  Id  System
/dev/sda1   *          1        1913    15360000  83  Linux
/dev/sda2            1913        1938      204800  82  Linux swap / Solaris


2、新建主分區。

命令:fdisk /dev/sda   /dev/sda進行交互式分區管理,管理命令功能如下:

p:print,顯示已有分區

n:new,創建

d:delete,刪除

w:write,寫入磁盤並退出

q:退出

m:獲取幫助

l:列表所有分區的類型id

    t:調整分區id    

[root@localhost ~]#fdisk /dev/sda
 
WARNING:DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') andchange display units to
         sectors (command 'u').
 
Command (m forhelp): p  
 
Disk /dev/sda: 85.9GB, 85899345920 bytes
255 heads, 63sectors/track, 10443 cylinders
Units = cylinders of16065 * 512 = 8225280 bytes
Sector size(logical/physical): 512 bytes / 512 bytes
I/O size(minimum/optimal): 512 bytes / 512 bytes
Disk identifier:0x0002791b
 
   Device Boot      Start         End      Blocks  Id  System
/dev/sda1   *          1        1913    15360000  83  Linux
/dev/sda2            1913        1938      204800  82  Linux swap / Solaris
Command (m for help): n                # 新建一個分區
Command action
   e  extended
   p  primary partition (1-4)
p                # 建立主分區
Partition number (1-4): 3             # 指定建立的分區
First cylinder (1938-10443, default 1938):         # 不指定默認延續之前的柱面
Using default value1938
Last cylinder, +cylinders or +size{K,M,G} (1938-10443, default10443): +10G      # 創建10G大小的主分區


3、調整分區id(根據實際需要,這裏僅介紹)

默認創建爲Linux(id83),根據實際需要,若要創建爲swap分區,則要調整分區id82,即 Linux swap /So;若要創建Linux LVM 分區,則需調整分區id8e。在這裏按默認,不需要調整。

列出分區id列表:

Command(m for help): l
 
 0 Empty           24  NEC DOS         81 Minix / old Lin bf  Solaris        
 1 FAT12           39  Plan 9          82 Linux swap / So c1  DRDOS/sec(FAT-
 2  XENIXroot      3c  PartitionMagic  83 Linux           c4  DRDOS/sec (FAT-
 3  XENIXusr       40  Venix 80286     84 OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16<32M      41  PPC PReP Boot   85 Linux extended  c7  Syrinx        
(以下內容省略)…

調整分區:

使用Command  t ,並指定分區,具體步驟在這裏不演示。


4、建立擴展分區

需要注意的是,建立擴展分區時,如果之前已經分配好了3個主分區,不把剩餘的全部的空間都佔據,則沒有被分配到的空間無法使用。因爲邏輯分區只能在擴展分區的空間範圍內。

Command (m forhelp): n
Command action
   e  extended
   p  primary partition (1-4)
e             # 指定建立擴展分區
Selected partition 4
First cylinder(3244-10443, default 3244):
Using default value3244
Last cylinder, +cylinders or +size{K,M,G} (3244-10443, default10443):      #  回車指定使用默認全部空間
Usingdefault value 10443


5、建立邏輯分區

Command (m forhelp): n
First cylinder(3244-10443, default 3244):
Using default value3244
Last cylinder, +cylinders or +size{K,M,G} (3244-10443, default10443): +10G      # 新建10G大小的邏輯分區


6、檢查新建分區並保存退出

Command (m for help): p      # 查看已有分區
 
Disk /dev/sda: 85.9GB, 85899345920 bytes
255 heads, 63sectors/track, 10443 cylinders
Units = cylinders of16065 * 512 = 8225280 bytes
Sector size(logical/physical): 512 bytes / 512 bytes
I/O size(minimum/optimal): 512 bytes / 512 bytes
Disk identifier:0x0002791b
 
   Device Boot      Start         End      Blocks  Id  System
/dev/sda1   *          1        1913    15360000  83  Linux
/dev/sda2            1913        1938      204800  82  Linux swap / Solaris
/dev/sda3            1938        3243   10483573+  83  Linux
/dev/sda4            3244       10443   57834000    5  Extended
/dev/sda5            3244        4549   10490413+  83  Linux
 
Command (m forhelp): w     # 切記,保存退出
The partition tablehas been altered!
 
Calling ioctl() tore-read partition table.
 
WARNING: Re-readingthe partition table failed with error 16: Device or resource busy.
The kernel stilluses the old table. The new table will be used at
the next reboot orafter you run partprobe(8) or kpartx(8)
Syncing disks.


7、使新增分區生效

     在上面交互式管理磁盤操作中,只是新建好分區,而內核不會主動去識別更改後的分區。用cat /proc/partitions命令查看,新建的分區還沒生效。命令結果如下:

[root@localhost ~]#cat /proc/partitions
major minor  #blocks name
 
   8       0   83886080 sda
   8       1   15360000 sda1
   8       2     204800 sda2


於是就要到partx命令,partx命令的用途是把現有的並編號的磁盤分區列表通知內核。在Centos 5.X中,用的是partprobe命令。

[root@localhost ~]#partx -a /dev/sda
BLKPG: Device orresource busy
error addingpartition 1
BLKPG: Device orresource busy
error addingpartition 2


再用fdisk -l命令查看,由下結果可以看到,系統已經識別了新增的分區。fdisk -l 命令結果是把/proc/partitions裏的分區表打印出來。

[root@localhost ~]#fdisk -l
 
Disk /dev/sda: 85.9GB, 85899345920 bytes
255 heads, 63sectors/track, 10443 cylinders
Units = cylinders of16065 * 512 = 8225280 bytes
Sector size(logical/physical): 512 bytes / 512 bytes
I/O size(minimum/optimal): 512 bytes / 512 bytes
Disk identifier:0x0002791b
 
   Device Boot      Start         End      Blocks  Id  System
/dev/sda1   *          1        1913    15360000  83  Linux
/dev/sda2            1913        1938      204800  82  Linux swap / Solaris
/dev/sda3            1938        3243   10483573+  83  Linux
/dev/sda4            3244       10443   57834000    5  Extended
/dev/sda5            3244        4549   10490413+  83  Linux

  

8、格式化分區並指定分區文件系統

   在這裏使用ext系列文件系統專用管理工具mke2fs進行格式化並指定文件系統類型。命令用法如下:

      mke2fs [OPTION]…  device

命令常用選項

-t{ext2|ext3|ext4}

-b{1024|2048|4096} :塊大小

-L'LABEL'

-j:相當於-t ext3

-i#:數據空間中每多少個字節創建一個inode,此大小不應該小於block大小

-N#:爲數據空間創建多少個inode

-m#:爲管理人員預留的空間佔據的百分比

 

[root@localhost ~]#mke2fs -t ext4 -b 2048 -L "Primary_Partition" /dev/sda3  
mke2fs 1.41.12(17-May-2010)
Filesystemlabel=Primary_Partitio
OS type: Linux
Block size=2048(log=1)
Fragment size=2048(log=1)
Stride=0 blocks,Stripe width=0 blocks
655360 inodes,5241786 blocks
262089 blocks(5.00%) reserved for the super user
First data block=0
Maximum filesystemblocks=542113792
320 block groups
16384 blocks pergroup, 16384 fragments per group
2048 inodes pergroup
Superblock backupsstored on blocks:
        16384, 49152, 81920, 114688, 147456,409600, 442368, 802816, 1327104,
        2048000, 3981312
 
Writing inodetables: done                            
Creating journal(32768 blocks): done
Writing superblocksand filesystem accounting information: done
 
This filesystem willbe automatically checked every 37 mounts or
180 days, whichevercomes first.  Use tune2fs -c or -i tooverride.
 
[root@localhost ~]#mke2fs -t ext4 -b 2048 -L "Logical_Partition" /dev/sda5
結果省略...


可以用命令blkid查看塊設備屬性信息查看。用法如下:

blkid [OPTION]...[DEVICE]

-UUUID:根據指定的UUID來查找對應的設備

-LLABEL:根據指定的LABEL來查找對應的設備

命令結果如下:

[root@localhost ~]#blkid /dev/sda3
/dev/sda3:LABEL="Primary_Partitio"UUID="4f0572f3-64a4-4b19-bc4c-eb5df4644741" TYPE="ext4"
[root@localhost ~]#blkid /dev/sda5
/dev/sda5:LABEL="Logical_Partitio"UUID="208ef72f-706e-4888-9655-32a91ecb18a2" TYPE="ext4"


9、掛載分區

mount基本用法爲:mount [OPTION] DEVICE  DIR

DEVICE:指明要掛載的設備,可以使設備文件,可以用卷標 -L 'LABEL'UUID -U 'UUID'

        或者僞文件系統名稱。

DIR:爲掛載目錄,必須爲已存在目錄,且目錄原有文件在掛載完成後會被臨時隱藏,

      建議使用空目錄

[root@localhost ~]# mkdir /Prim_test /Logi_test  #新建兩個目錄當作掛載點
[root@localhost ~]#mount /dev/sda3 /Prim_test/
[root@localhost ~]#mount /dev/sda5 /Logi_test
[root@localhost ~]# df -l     # 查看掛載項
Filesystem     1K-blocks  Used Available Use% Mounted on
/dev/sda1       14987656 667824  13551832  5% /
tmpfs             502176      0   502176   0% /dev/shm
/dev/sda3       10252772 12308   9716286   1% /Prim_test
/dev/sda5       10259072 12308   9722244   1% /Logi_test
[root@localhost ~]# ls /Prim_test/      # 掛載目錄出現lost+found表明掛載已經成功
lost+found
[root@localhost ~]#ls /Logi_test/
lost+found
[root@localhost ~]# cp /etc/fstab /Prim_test/  #  複製一個文件到掛載目錄裏,若能正常打開,則再次確認掛載成功
[root@localhost ~]#cat /Prim_test/fstab
#
# /etc/fstab
# Created byanaconda on Tue Aug 25 18:43:53 20
(文檔下面內容省略...)


10、配置分區開機自動掛載

編輯/etc/fstab配置文件,添加註釋的兩行內容

[root@localhost ~]#vim /etc/fstab
#
# /etc/fstab
# Created byanaconda on Tue Aug 25 18:43:53 2015
#
# Accessiblefilesystems, by reference, are maintained under '/dev/disk'
# See man pagesfstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=915ff827-bd8e-4b9c-b804-a8bb73441c7a/                     ext4    defaults       1 1
UUID=278e1b2d-05e7-4c58-bd9e-99740bdee416swap                  swap    defaults       0 0
UUID=4f0572f3-64a4-4b19-bc4c-eb5df4644741 /Prim_test           ext4    defaults       0 0       #掛載/dev/sda3
UUID=208ef72f-706e-4888-9655-32a91ecb18a2 /Logi_test           ext4    defaults       0 0       #掛載/dev/sda5
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

關於/etc/fstab配置文件各字段的解釋如下:

    1、需要掛載的設備或僞文件系統

            可以爲設備文件名稱、LABEL(LABEL="")、UUID(UUID="")、僞文件系統名稱

            (proc,sysfs),建議用UUID,可以唯一標識設備,UUID不容易混亂衝突。

    2、掛載點

    爲事先存在的目錄

    3、文件系統類型

    4、轉儲頻率:

            0:不做備份;1:每天轉儲;2:每隔一天轉儲。

    5、自檢次序

            0:不自檢;1:首先自檢;一般只有rootfs才用1。

 

三、一些擴展管理命令

tune2fs:重新設定ext系統文件系統可調整參數的值

tune2fs [OPTION]... DEVICE

常用選項:

-l:查看指定文件系統超級塊信息,superblock

-L"LABEL":修改卷標

-m#: 預留給管理員的空間百分比

-j:將ext2升級爲ext3

-O:文件系統屬性啓用或禁用

-o:調整文件系統的默認掛載選項

-UUUID:修改UUID號

dumpe2fs顯示設備文件系統的超級塊和塊組信息

dumpe2fs [OPTION]…  DEVICE

-h:只查看超級信息,不顯示塊組信息

        不加選項顯示超級快和塊組信息

mkswap:創建交換分區

fsck檢查和修復文件系統

    用法如下:

fsck.FS_TYPE device

fsck [OPTION]   device

-t:指定文件系統類型

-a:自動修復錯誤

-r:交互式修復錯誤

        注意:若指定FS_TYPE,則一定要與分區上已經存在文件類型相同

 

總結:磁盤分區可以用fdisk工具進行分區,和指定分區ID(根據實際需要),切記w保存後退 出。用partx命令通知內核重新識別讀取更改後的分區,再用mke2fs命令進行格式化和設定文件系統類型,並設置一些參數,如塊大小,卷標,給管理員保留空間所佔塊數百分比等等。mount掛載分區,測試是否掛載正常。若要開機掛載則在/etc/fstab配置文件中添加相關項。

除了磁盤分區用到的一些命令外,還有一些常用的磁盤管理命令,如查看塊設備信息blkid,重新設定ext文件系統參數tune2fs, 顯示文件系統超級快和塊組信息dumpe2fs, 文件系統和檢查命令fsck,等等。

 

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