linux 系統磁盤管理(主分區和邏輯分區)

摘要:linux系統磁盤管理主分區和邏輯分區node


##一、linux系統分區應瞭解的常識linux

  • 硬盤分區實質上是對硬盤的一種格式化,而後才能使用硬盤保存各類信息,在建立分區時,就已經設置好了硬盤的各項物理參數,指定了硬盤主引導記錄(即Master Boot Record,通常簡稱爲MBR)和引導記錄備份的存放位置.
  • MBR概述:全稱爲Master Boot Record,即硬盤的主引導記錄;是位於磁盤最前邊的一段引導(Loader)代碼。它負責磁盤操做系統(DOS)對磁盤進行讀寫時分區合法性的判別、分區引導信息的定位,它由磁盤操做系統(DOS)在對硬盤進行初始化時產生的。 1.主引導程序即主引導記錄(MBR)(佔446個字節) 可在FDISK程序中找到,它用於硬盤啓動時將系統控制轉給用戶指定的並在分區表中登記了的某個操做系統。 2.磁盤分區表項(DPT,Disk Partition Table) 由四個分區表項構成(每一個16個字節)。 負責說明磁盤上的分區狀況,其內容由磁盤介質及用戶在使用FDISK定義分區時決定。(具體內容略) 3.結束標誌(佔2個字節)(魔數) 其值爲AA55,存儲時低位在前,高位在後,即看上去是55AA(十六進制)。
  • 分區編號:主分區1-4 ,邏輯分區5……
  • LINUX規定:邏輯分區必須創建在擴展分區之上,而不是創建在主分區上
  • 分區做用: ①主分區:主要是用來啓動操做系統的,它主要放的是操做系統的啓動或引導程序,/boot分區最好放在主分區上; ②擴展分區是不能使用的,它只是作爲邏輯分區的容器存在的,先建立一個擴展分區,在擴展分區之上建立邏輯分區; ③咱們真正存放數據的是主分區和邏輯分區,大量數據都放在邏輯分區中。

注意:使用分區工具fdisk對磁盤進行操做,分區,格式化(重點)app

注意: 主分區+擴展分區 最多隻能有4個 擴展分區能夠是0個,最可能是1個 擴展分區不能直接使用,擴展分區必須首先建立成邏輯分區才能使用 邏輯分區能夠是0個 1個 多個dom

##二、磁盤管理命令ide

fdisk : Linux分區表操做工具軟件 n: 添加新的分區 p: 查看分區信息 w: 保存退出 q: 不保存退出 d: 刪除分區 t: 改變分區類型工具

##三、查看磁盤命令spa

ls  /dev/sd*     #查看磁盤

其中:a-z 表示設備的序號,如sda表示第一塊scsi硬盤,sdb就是第二塊…… n 表示每塊磁盤上劃分的磁盤分區編號 操作系統

##四、建立主分區演示:3d

工做環境中爲English環境,方便你們閱讀在國語環境下演示code

#第一步:添加磁盤建立主分區

╭─root@localhost.localdomain ~  
╰─➤  ls /dev/sd*     #查看磁盤
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb  /dev/sdc  /dev/sdd  /dev/sde
╭─root@localhost.localdomain ~  
╰─➤  fdisk /dev/sdb     #管理分區                                                        
歡迎使用 fdisk (util-linux 2.23.2)。

更改將停留在內存中,直到您決定將更改寫入磁盤。
使用寫入命令前請三思。

Device does not contain a recognized partition table
使用磁盤標識符 0x9fccbf7c 建立新的 DOS 磁盤標籤。

命令(輸入 m 獲取幫助):n   #新建分區
Partition type:       #分區類型
   p   primary (0 primary, 0 extended, 4 free)     #p--->主分區
   e   extended       #e  --->   擴展分區
Select (default p): p     #選擇主分區
分區號 (1-4,默認 1):
起始 扇區 (2048-41943039,默認爲 2048): #分區磁盤的起始位,默認值,回車就好!
將使用默認值 2048
Last 扇區, +扇區 or +size{K,M,G} (2048-41943039,默認爲 41943039):+5G   #磁盤大小選擇,加號不能省略,回車便可
分區 1 已設置爲 Linux 類型,大小設爲 5 GiB

命令(輸入 m 獲取幫助):P   #查詢分區是否建立成功

磁盤 /dev/sdb:21.5 GB, 21474836480 字節,41943040 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標籤類型:dos
磁盤標識符:0x9fccbf7c

   設備 Boot        Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux    #sdb1

命令(輸入 m 獲取幫助):w    #保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盤。

#第二步:查看磁盤

╭─root@localhost.localdomain ~  
╰─➤  ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb  /dev/sdb1  /dev/sdc  /dev/sdd  /dev/sde

#第三步:格式化成xfs文件系統

╭─root@localhost.localdomain ~  
╰─➤  mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

#第四步:掛載至本地目錄

╭─root@localhost.localdomain ~  
╰─➤  mkdir /xfs_du   #建立本地目錄          
╭─root@localhost.localdomain ~  
╰─➤  mount /dev/sdb1 /xfs_du   #使用mount掛載   mount 磁盤 目錄

#第五步:查看掛載信息

╭─root@localhost.localdomain ~  
╰─➤  df -h
文件系統             容量  已用  可用 已用% 掛載點
/dev/mapper/cl-root   17G  4.7G   13G   28% /
devtmpfs             473M     0  473M    0% /dev
tmpfs                489M   88K  489M    1% /dev/shm
tmpfs                489M  7.1M  482M    2% /run
tmpfs                489M     0  489M    0% /sys/fs/cgroup
/dev/sda1           1014M  173M  842M   18% /boot
tmpfs                 98M  8.0K   98M    1% /run/user/0
/dev/sr0             4.1G  4.1G     0  100% /run/media/root/CentOS 7 x86_64
/dev/sdb1            5.0G   33M  5.0G    1% /xfs_du

#第六步:開機自動掛載

╭─root@localhost.localdomain ~  
╰─➤  echo "/dev/sdb1/xfs_du xfs defaults 0 0" >> /etc/fstab

#開機自動掛載方法2:

echo 「mount /dev/sdb1 /xfs_du」 >> /etc/rc.local
chmod +x /etc/rc.d/rc.local   #給個執行權限


# /etc/rc.local 
   是使用者自訂開機啓動程序的文件,把須要開機自動運行的程序寫在這個腳本里

##<font color=red>fdisk分區演示–建立邏輯分區</font>

# 第一步:fdisk添加擴展分區

╭─root@du ~  
╰─➤ fdisk /dev/sdb
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): n    #添加新分區
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e     #選擇擴展分區
Partition number (2-4, default 2):
First sector (10487808-41943039, default 10487808):
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G    #5G大小
Partition 2 of type Extended and of size 5 GiB is set

Command (m for help): P   #查看分區

Disk /dev/sdb: 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: 0xe140b235

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
/dev/sdb2 10487808 20973567 5242880 5 Extended

Command (m for help): w   #保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

#第二步:建立邏輯分區

╭─root@du ~  
╰─➤ fdisk /dev/sdb
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): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l    #建立邏輯分區
Adding logical partition 5
First sector (10489856-20973567, default 10489856):
Using default value 10489856
Last sector, +sectors or +size{K,M,G} (10489856-20973567, default 20973567): +10G   #邏輯分區大小不能超過擴展分區大小
Value out of range.
Last sector, +sectors or +size{K,M,G} (10489856-20973567, default 20973567): +2g
Unsupported suffix: ‘g’.
Supported: 10^N: KB (KiloByte), MB (MegaByte), GB (GigaByte)
2^N: K (KibiByte), M (MebiByte), G (GibiByte)
Last sector, +sectors or +size{K,M,G} (10489856-20973567, default 20973567): +2G
Partition 5 of type Linux and of size 2 GiB is set

Command (m for help): P

Disk /dev/sdb: 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: 0xe140b235

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
/dev/sdb2 10487808 20973567 5242880 5 Extended
/dev/sdb5 10489856 14684159 2097152 83 Linux

Command (m for help): W
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

#第三步:建立文件系統

╭─root@du ~  
╰─➤ ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1 /dev/sdb2 /dev/sdb5
╭─root@du ~  
╰─➤ mkfs.xfs /dev/sdb5
meta-data=/dev/sdb5 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

#第四步:開機自動掛載

╭─root@du ~  
╰─➤ echo 「mount /dev/sdb5 /xfs_du」 >> /etc/rc.local

#<font color=red>本地目錄建立交換分區</font>

第一步:建立目錄

mkdir /swap

第二步:建立空文件

dd if=/dev/zero of=/swap/swap bs=2M count=2014

第三步:格式化爲swap格式

mkswap /swap/swap

第四步:寫入文件/etc/fstab 開機自動掛載

echo "/swap/swap swap swap defaults 0 0" >> /etc/fstab

第五步:修改權限,掛載

chmod 0600 /swap/swap

mount -a

第六步:開啓swap

swapon -a
[root@node1 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           976M        321M         60M        9.5M        594M        466M
Swap:          5.9G        221M        5.7G

最後:關閉swap

swapoff -a

相關文章
相關標籤/搜索