1. 程式人生 > >Linux 添加硬盤設備

Linux 添加硬盤設備

既然 uil 內存 一次 pan 如果 swap分區 bytes ascii

fdisk命令用於管理磁盤分區,格式為:“fdisk [磁盤名稱]”。

管理Linux系統中的硬盤設備最常用的方法就當屬是用fdisk命令了,這條命令提供了添加、刪除、轉換分區等等功能於一身的“一站式分區服務”,不過這條命令的參數是交互式的,而不是像咱們以前直接寫到命令後面的參數一樣,因此在管理硬盤設備的時候特別方便,可以根據需求動態的調整。

參數 作用
m 查看全部可用的參數
n 添加新的分區
d 刪除某個分區信息
l 列出所有可用的分區類型
t 改變某個分區的類型
p 查看分區表信息
w 保存並退出
q 不保存直接退出

一、添加磁盤分區

查看當前磁盤及分區情況:

[[email protected] ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root  8.5G  3.1G  5.5G  36% /
devtmpfs               986M     0  986M   0% /dev
tmpfs                 1001M   84K 1001M   1% /dev/shm
tmpfs                 1001M  8.7M  992M   1% /run
tmpfs                 1001M     0 1001M   0% /sys/fs/cgroup
/dev/sda1              497M  140M  358M  29% /boot
tmpfs                  201M   16K  201M   1% /run/user/42
tmpfs                  201M     0  201M   0% /run/user/0
[[email protected]
/* */ ~]# fdisk -l Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x0006c4ff Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 20971519 9972736 8e Linux LVM Disk /dev/mapper/rhel-root: 9093 MB, 9093251072 bytes, 17760256 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 /dev/mapper/rhel-swap: 1073 MB, 1073741824 bytes, 2097152 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 /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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 [[email protected]
/* */ ~]#

從上面可以看出,第二塊磁盤/dev/sdb共5368MB,並未使用。現在我們使用 fdisk 命令來將/dev/sdb劃分出500MB,掛載到/lxjtest目錄;然後再劃分出200MB當作swap分區使用。

第1步:首先使用fdisk命令嘗試管理/dev/sdb硬盤設備,看到提示信息後輸入參數p來查看硬盤設備內已有的分區信息,這其中包括了硬盤的容量大小,扇區個數等等信息:

[[email protected] ~]# 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.

Device does not contain a recognized partition table  ---磁盤未包括任何可用的分區
Building a new DOS disklabel with disk identifier 0x148e18c9.

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: 0x148e18c9

   Device Boot      Start         End      Blocks   Id  System

第2步:輸入參數n來嘗試新建分區信息,系統會要求選擇繼續輸入參數p來創建主分區,或者輸入參數e來創建擴展分區,因此輸入參數p來創建一個主分區:

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p

第3步:確認創建一個主分區後,系統要求您先輸入分區的編號,既然已經知道主分區的編號範圍是1-4,因此默認輸入1就可以了,接下來系統會提示定義下起始的扇區,這一項默認不需要改動,系統會自動計算出最靠前空閑的扇區位置,咱們敲擊一下回車就可以,最後系統會要求定義分區的結束扇區位置,這其實就是想要去定義下整個分區的大小是多少,但其實不用去算扇區的個數,只需要輸入+500M即可創建出一個容量為500MB的硬盤分區。

Partition number (1-4, default 1): 
First sector (2048-10485759, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): +500M
Partition 1 of type Linux and of size 500 MiB is set

第4步:再次使用參數p來查看下硬盤設備中的分區信息,果然就能看到一個名稱為/dev/sdb1,起始扇區為2048,結束扇區是1026047的分區啦,這時候千萬不要直接關閉窗口,而應該敲擊參數w後回車,這樣分區信息才是真正的寫入成功啦。

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: 0x148e18c9

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1026047      512000   83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] ~]# 

第5步:當做完上面所說的所有步驟後,Linux系統會自動把這個硬盤分區抽象成/dev/sdb1這個設備文件,可以用file命令來看到這個文件的屬性。有些時候系統並沒有自動的把分區信息同步給Linux內核,也可以輸入partprobe命令來手動的同步信息到內核,而且一般推薦敲擊兩次會效果更佳,但一旦遇到連這個命令都不能解決的時候,那麽就重啟計算機吧。

[[email protected] ~]# file /dev/sdb1
/dev/sdb1: block special

使用mkfs格式化/dev/sdb1分區。

[[email protected] ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=256    agcount=4, agsize=32000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=128000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[[email protected] ~]# 

接下來就可以掛載使用了。

[[email protected] ~]# mkdir /lxjtest
[[email protected] ~]# mount /dev/sdb1 /lxjtest/
[[email protected] ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root  8.5G  3.1G  5.5G  36% /
devtmpfs               986M     0  986M   0% /dev
tmpfs                 1001M   84K 1001M   1% /dev/shm
tmpfs                 1001M  8.7M  992M   1% /run
tmpfs                 1001M     0 1001M   0% /sys/fs/cgroup
/dev/sda1              497M  140M  358M  29% /boot
tmpfs                  201M   16K  201M   1% /run/user/42
tmpfs                  201M     0  201M   0% /run/user/0
/dev/sdb1              497M   26M  472M   6% /lxjtest

使用mount命令掛載的設備文件會在下一次重啟的時候失效,因此如果您還想讓這個設備文件的掛載永久的有效下去,需要把掛載的信息項目寫入到配置文件才可以:

[[email protected] ~]# vi /etc/fstab 


#
# /etc/fstab
# Created by anaconda on Wed Jul  5 03:56:47 2017
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=4f396f9a-467e-4472-a654-836f2dc7d6ca /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
/dev/sdb1       /lxjtest        xfs     defaults        0 0     
二、添加交換分區

SWAP交換分區是一種類似於Windows系統虛擬內存的功能,通過把一部分硬盤空間虛擬成內存來使用,從而解決內存容量不足的情況。但由於SWAP畢竟是用硬盤資源虛擬的,速度上比真實物理內存要慢很多,所以一般只有當真實物理內存耗盡時才會調用SWAP交換分區,把內存中暫時不常用的數據臨時存放到硬盤中,騰出內存空間讓更活躍的程序服務來使用。

第1步:SWAP交換分區的創建過程非常類似於上一個小節所講到的分區設備掛載使用的方法,首先第一步就是再進行對/dev/sdb存儲設備分區操作,取出一個大小為200MB的主存儲分區然後保存退出即可:

[[email protected] ~]# 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): p
Partition number (2-4, default 2): 
First sector (1026048-10485759, default 1026048): 
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-10485759, default 10485759): +200M          
Partition 2 of type Linux and of size 200 MiB is set

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: 0x148e18c9

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1026047      512000   83  Linux
/dev/sdb2         1026048     1435647      204800   83  Linux

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

Calling ioctl() to re-read partition table.

WARNING: 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.
[[email protected] ~]# partprobe 執行該命令將分區信息同步到內核

第2步:把新建的存儲分區使用SWAP交換分區專用的格式化mkswap命令進行格式化操作:

[[email protected] ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 204796 KiB
no label, UUID=4746286f-4680-4a05-878d-118fff5904ac

第3步:接下來使用swapon命令把準備好的SWAP交換分區設備正式的掛載到系統中,並可以使用free -m 命令來看到交換分區大小的變化(由1023M提升至了1223M):

[[email protected] ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           2000         259        1398           8         342        1578
Swap:          1023           0        1023
[[email protected] ~]# swapon /dev/sdb2
[[email protected] ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           2000         259        1398           8         342        1578
Swap:          1223           0        1223

第4步:為了能夠讓新的SWAP交換分區設備在重啟後依然生效,需要按照下面的格式寫入到配置文件中。

[[email protected] ~]# vi /etc/fstab 


#
# /etc/fstab
# Created by anaconda on Wed Jul  5 03:56:47 2017
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=4f396f9a-467e-4472-a654-836f2dc7d6ca /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
/dev/sdb1       /lxjtest        xfs     defaults        0 0
/dev/sdb2       swap    swap    defaults        0 0

Linux 添加硬盤設備