磁盤管理
本文將按順序以實例演示磁盤管理的所有操作,讓我們開始吧!
一,磁盤的添加
① 先來查看linux系統總共有幾個磁盤,由圖可知是兩個,分別是sda,sdb。我們再加一個,按照磁盤命名順序,應是sdc,他們都在/dev目錄下。
補充:
1,磁盤命名規則:
不同磁盤,按照a-z依次標識,如sda,sdb,sdc
同一磁盤的不同分區,按照1,2,…的規則標識。如sda1,sda2,…
2,lsblk 列出所有可用塊設備的信息(磁盤的存取單位為塊,block)
② 下面是磁盤的添加步驟
③ 此時用lsblk查看,會發現并沒有發現有sdc磁盤,需要執行下面一條語句。
好了,磁盤添加成功!
二,磁盤的分區
1) 在分區之前,先了解一下MBR和GPT
MBR
主引導記錄,又叫做主引導扇區,是計算機開機后訪問硬盤時所必須要讀取的首個扇區,它在硬盤上 的三維地址為(柱面,磁頭,扇區)=(0,0,1)。它由4個部分組成:
主引導程序(偏移地址0000H–0088H),負責從活動分區中裝載
出錯信息數據區,偏移地址0089H–00E1H為出錯信息,00E2H–01BDH全為0字節
分區表含4個分區項,偏移地址為01BEH–01FDH,每個分區表項長16個字節,共64字節分別為分區項1,分區項2,分區項3,分區4.
結束標志字,偏移地址01FEH–01FFH的兩個字節值為結束標志
上面列出的偏移地址數是16進制,詳細請看下圖
GPT分區:
支持128個分區,使用64位,支持8Z(512byte/Block)和64Z(4096byte/Block), 使用128位UUID表示磁盤和分區
向后兼容MBR
底層硬件必須使用UEFI
必須使用64位操作系統
2) 創建分區使用命令:
fdisk 使用MBR分區
gdisk 使用GPT分區
parted 高級分區操作
以fdisk演示
[root@localhost ~]# fdisk /dev/sdc
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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x7d4c5be8.
Command (m for help): m #輸入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): n #由上面的幫助信息可知n是創建分區
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p #要先分主分區,所以輸入p
Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048): #可以默認
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): +1G
Partition 1 of type Linux and of size 1 GiB is set
#以下便可如以上重復,直到建到第三個分區
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2099200-209715199, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-209715199, default 209715199): +20G
Partition 2 of type Linux and of size 20 GiB is set
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3):
First sector (44042240-209715199, default 44042240):
Using default value 44042240
Last sector, +sectors or +size{K,M,G} (44042240-209715199, default 209715199): +10G
Partition 3 of type Linux and of size 10 GiB is set
#4一般選擇為擴展分區
Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): e
Selected partition 4
First sector (65013760-209715199, default 65013760):
Using default value 65013760
Last sector, +sectors or +size{K,M,G} (65013760-209715199, default 209715199):
Using default value 209715199
Partition 4 of type Extended and of size 69 GiB is set
#下面是創建邏輯分區,
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (65015808-209715199, default 65015808):
Using default value 65015808
Last sector, +sectors or +size{K,M,G} (65015808-209715199, default 209715199): +1G
Partition 5 of type Linux and of size 1 GiB is set
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (67115008-209715199, default 67115008):
Using default value 67115008
Last sector, +sectors or +size{K,M,G} (67115008-209715199, default 209715199):
Using default value 209715199
Partition 6 of type Linux and of size 68 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 953M 0 part /boot
├─sda2 8:2 0 46.6G 0 part /
├─sda3 8:3 0 3.7G 0 part [SWAP]
├─sda4 8:4 0 512B 0 part
├─sda5 8:5 0 46.6G 0 part /app
└─sda6 8:6 0 1G 0 part
sdb 8:16 0 100G 0 disk
└─sdb1 8:17 0 190M 0 part
sdc 8:32 0 100G 0 disk
├─sdc1 8:33 0 1G 0 part
├─sdc2 8:34 0 20G 0 part
├─sdc3 8:35 0 10G 0 part
├─sdc4 8:36 0 1K 0 part
├─sdc5 8:37 0 1G 0 part
└─sdc6 8:38 0 68G 0 part
sr0 11:0 1 1024M 0 rom
除了lsblk,可以用如下命令查看內核是否已經識別到新的分區
三,為分區寫入文件系統
文件系統是操作系統用于明確存儲設備或分區上的文件的方法和數據結構,即上存儲設備上組織文件 的方法。操作系統中負責管理和存儲文件信息的軟件結構稱為文件管理系統,簡稱文件系統。
Linux文件系統包括:ext2 , ext3 , ext4 , xfs , btrfs , reiserfs , jfs , swap …
文件系統類型:
查看系統支持的文件系統使用:ls /lib/modules/`uname -r`/kernel/fs
為分區裝入文件系統:
mkfs.ext3 /dev/sdc1
mkfs.ext4 /dev/sdc2
mkfs.ext2 /dev/sdc3
mkfs.ext4 /dev/sdc5
mkfs.xfs /dev/sdc6
查看分區所裝的文件系統:
[root@localhost ~]# blkid /dev/sdc1
/dev/sdc1: UUID=”7f0334db-9fe2-4449-a364-dbae7f092ba1″ SEC_TYPE=”ext2″ TYPE=”ext3″
我們過濾一下,只顯示TYPE
為分區寫入文件系統成功!
補充:
blkid 顯示有關你系統里所有可用塊設備的信息。
四,分區的掛載
掛載:將額外文件系統與根文件系統某現存的目錄建立起關聯關系,進而使得此目錄 做為其它文件訪問入口的行為。
卸載:為解除此關聯關系的行為
下圖使用df看到掛載項一列
掛載使用mount
-a 安裝在/etc/fstab文件中列出的所有文件系統
-f 伙裝mount,作出檢查設備和目錄的樣子,但并不真正掛載文件系統
-n 不把安裝記錄在/etc/mtab文件中。
-r 講文件系統安裝只為讀
-v 詳細顯示安裝信息
-w 將文件系統安裝為可寫,為命令默認情況
-t 指定設備的文件系統類型
-o 指定掛載文件系統時的選項,有些也可寫在/etc/fstab中,常用的有:
defaults 使用所有選項的默認值(auto, nouser, rw, suid)
auto/noauto 允許/不允許以-a選項進行安裝
dev/nodev 對/不對文件系統上的特殊設備進行解釋
exec/noexec 允許/不允許執行二進制代碼
suid/nosuid 確認/不確認suid和sgid位
user/nouser 允許/不允許一般用戶掛載
ro 以只讀方式掛載
rw 以讀寫方式掛載
remount 重新安裝已經安裝了的文件系統
說明:df只顯示掛載的分區,如sda1,sda2,若沒有掛載就不會出現,如圖可知,/dev/sdc1, 掛載到了/media/guazai。
原創文章,作者:xxcj,如若轉載,請注明出處:http://www.www58058.com/85030