Linux-磁盤管理

分區工具:fdisk、parted

格式化命令:mkfs

格式化swap分區:mkswap

掛載/卸載命令:mount/umount


1. fdisk - manipulate disk partition table 操作磁盤分區表(MBR:msdos)

fdisk分區操作會緩存在磁盤,執行w命令后才寫入磁盤

SYNOPSIS

fdisk [-uc] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device

fdisk -l [-u] [device...]

fdisk -s partition...

fdisk -v

fdisk -h

OPTIONS

-l    List the partition tables for the specified devices  and  then  exit.(列出分區表)

-v    Print version number of fdisk program and exit.(顯示fdisk版本號)

示例

對某個磁盤設備執行命令,fdisk /dev/vda

m : 獲取幫助

p : 打印當前分區情況

n : 創建一個分區

d : 刪除一個分區

t : 修改分區的system id(默認83為主分區,82為swap分區)

w : 保存并退出

q : 退出(不保存)
操作截圖如下,更多步驟不一一列舉,help中的說明很清楚,分區操作也有進一步提示,完全可以按提示執行

fdisk.png

2. parted - a partition manipulation program 分區操作程序(MBR,GPT)(個人強烈建議用這種)

與fdisk不同,parted支持分區大小調整,而且parted的操作都是實時的,fdisk操作會緩存在內存中,需要執行w命令才寫入磁盤

SYNOPSIS

parted [options] [device [command [options...]...]]

OPTIONS

-h, --help  displays a help message(顯示幫助信息)

-l, --list  lists partition layout on all block devices(顯示所有塊設備的分區)

-m, --machine  displays machine parseable output()

-s, --script  never prompts for user intervention(不提示用戶操作)

-v, --version  displays the version(顯示版本號)

操作

對磁盤/dev/vdb執行parted分區操作(如果不帶參數則默認第一塊硬盤)

parted /dev/vdb
命令

help    查看幫助
version    顯示版本及版權信息
select DEVICE   選擇要執行操作的設備
print [devices|free|list,all|NUMBER]   顯示分區表、可用設備、空余空間、所有建立的分區
mklabel,mktable LABEL-TYPE             創建分區表(msdos | gpt)
rm NUMBER    按分區編號刪除分區
unit UNIT    將默認的分區大小單位設為UNIT(默認MB,可選GB)
mkpart PART-TYPE [FS-TYPE] START END    創建分區

示例

parted.png

3. mkfs - build a Linux filesystem 建立文件系統(即格式化)

Linux的文件系統類型眾多,常見的有:ext2(無日志)、ext3、ext4、xfs、btrfs...(目前CentOS 6默認的文件系統時ext4,CentOS 7默認的文件系統時xfs)。

mkfs本身并不執行建立文件系統的工作,而是去調用相關的程序來執行。

如果要格式化已存在文件系統的分區,使用-f選項強制寫入。

SYNOPSIS

mkfs [options] [-t type] [fs-options] device [size]

OPTIONS

-t, --type type 文件系統類型

Specify the type of filesystem to be built.  If not specified, the default filesystem type (currently ext2) is used.

fs-options 傳遞指定的文件系統選項給真實的文件系統創建程序

Filesystem-specific options to be passed to the real filesystem builder.  Although not guaranteed, the following options are supported by most filesystem builders.

-V, --verbose 顯示執行過程

Produce verbose output, including all filesystem-specific commands that are executed.  Specifying this option more than once inhibits execution  of  any  filesystem-specific  commands.

This is really only useful for testing.

-V, --version 顯示版本信息(僅當-V是唯一選項時,否則會以--verbose執行)

Display version information and exit.  (Option -V will display version information only when it is the only parameter, otherwise it will work as --verbose.)

-h, --help 顯示幫助信息

Display help and exit.

示例
mkfs.ext2 /dev/vdb1 (等同于 mkfs -t ext2 /dev/vdb1)
mkfs.ext3 /dev/vdb2
mkfs.ext4 /dev/vdb3
mkfs.xfs /dev/vdb4
mkfs.xfs /dev/vdb5
mkswap /dev/vdb6 (格式化swap分區只能用mkswap,不能用mkfs)

mkfs.png

4. mount - mount a filesystem 掛載文件系統

mount命令單獨使用時為查看當前掛載情況

SYNOPSIS

mount [-lhV]

mount -a [-fFnrsvw] [-t vfstype] [-O optlist]

mount [-fnrsvw] [-o option[,option]...]  device|dir

mount [-fnrsvw] [-t vfstype] [-o options] device dir

COMMAND LINE OPTIONS

-v, --verbose 顯示執行過程

-a, --all 掛載fstab中的所有文件系統

    Mount all filesystems (of the given types) mentioned in fstab.

-r, --read-only

    Mount the filesystem read-only. A synonym is -o ro.

-w, --rw, --read-write

    Mount the filesystem read/write. This is the default. A synonym is -o rw.

-n, --no-mtab 默認情況下,設備掛載或卸載的操作會同步更新至/etc/mtab文件中;-n用于禁止此特性;

    Mount without writing in /etc/mtab.  This is necessary for example when /etc is on a read-only filesystem.

-t, --types vfstype 指明要掛載的設備上的文件系統的類型;多數情況下可省略,此時mount會通過blkid來判斷要掛載的設備的文件系統類型;

-L, --label label 掛載指定卷標的設備

    Mount the partition that has the specified label.

-U, --uuid uuid 掛載uuid指定的設備

    Mount the partition that has the specified uuid.

-o, --options opts(這個太復雜了,不同類型的文件系統對應的選項頁不一樣,需要使用時查看幫助)

    Options are specified with a -o flag followed by a comma separated string of options.

示例

常規用法一般直接掛載分區即可

mount /dev/vdb2 /mnt

5. umount - unmount file systems 卸載文件系統

SYNOPSIS

umount [-hV]

umount -a [-dflnrv] [-t vfstype] [-O options]

umount [-dflnrv] {dir|device}...

OPTIONS

-a, --all                  

-A, --all-targets              

-c, --no-canonicalize                 

-d, --detach-loop               

-f, --force              

-i, --internal-only   
          
-O, --test-opts options,list 

-R, --recursive

-r, --read-only

-t, --types vfstype,ext2,ext3

-v, --verbose

-h, --help

-V, --version

示例

常規用法直接卸載分區

umount /dev/vdb2

umount /mnt

6. swapon, swapoff - enable/disable devices and files for paging and swapping

掛載/卸載swap分區

SYNOPSIS

#Get info:
swapon -s [-h] [-V]

#Enable/disable:
swapon [-d] [-f] [-p priority] [-v] specialfile...
swapoff [-v] specialfile...

#Enable/disable all:

swapon -a [-e] [-f] [-v]

swapoff -a [-v]

OPTIONS

-a, --all

All devices marked as ``swap'' in /etc/fstab are made available, except for those with the ``noauto'' option.  Devices that are already being used as swap are silently skipped.

-d, --discard [=policy]

Enable  swap  discards,  if  the swap backing device supports the discard or trim operation. This may improve performance on some Solid State Devices, but often it does not. The option

allows one to select between two available swap discard policies: --discard=once to perform a single-time discard operation for the whole swap area at  swapon;  or  --discard=pages  to

discard freed swap pages before they are reused, while swapping.  If no policy is selected, the default behavior is to enable both discard types.  The /etc/fstab mount options discard,

discard=once, or discard=pages may be also used to enable discard flags.

-e, --ifexists

Silently skip devices that do not exist.  The /etc/fstab mount option nofail may be also used to skip non-existing device.

-f, --fixpgsz

Reinitialize (exec /sbin/mkswap) the swap space if its page size does not match that of the current running kernel.  mkswap(2) initializes the whole device and does not check  for  bad

blocks.

-h, --help

Provide help.

-L label

Use the partition that has the specified label.  (For this, access to /proc/partitions is needed.)

-p, --priority priority

Specify  the priority of the swap device.  priority is a value between -1 and 32767.  Higher numbers indicate higher priority.  See swapon(2) for a full description of swap priorities.

Add pri=value to the option field of /etc/fstab for use with swapon -a.  When priority is not defined it defaults to -1.

-s, --summary

Display swap usage summary by device. Equivalent to "cat /proc/swaps".  Not available before Linux 2.1.25.

--show [column,column]

Display definable device table similar to --summary output.  See --help output for column list.

--noheadings

Do not print headings when displaying --show output.

--raw  Display --show output without aligning table columns.

--bytes

Display swap size in bytes in --show output instead of user friendly size and unit.  -U uuid Use the partition that has the specified uuid.

-v, --verbose

Be verbose.

-V, --version

Display version.

示例

swapon /dev/vdb1

swapoff /dev/vdb2

7. 自動掛載

設定除根文件系統以外的其它文件系統能夠開機時自動掛載:/etc/fstab文件。

每行定義一個要掛載的文件系統及相關屬性,6個字段含義如下:

(1) 要掛載的設備:
設備文件;
LABEL
UUID
偽文件系統:如sysfs, proc, tmpfs等

(2) 掛載點
swap類型的設備的掛載點為swap;

(3) 文件系統類型;

(4) 掛載選項
defaults:使用默認掛載選項;
如果要同時指明多個掛載選項,彼此間以事情分隔;
defaults,acl,noatime,noexec

(5) 轉儲頻率
0:從不備份;
1:每天備份;
2:每2天備份;

(6) 自檢次序
0:不自檢;
1:首先自檢,通常只能是根文件系統可用1;
2:次級自檢
...

mount -a:可自動掛載定義在此文件中的所支持自動掛載的設備;

8.實例

# 512GB磁盤
系統分區 25GB /
Swap分區 2GB
剩余空間 /home
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,182評論 6 543
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,489評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,290評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,776評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,510評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,866評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,860評論 3 447
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,036評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,585評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,331評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,536評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,058評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,754評論 3 349
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,154評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,469評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,273評論 3 399
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,505評論 2 379

推薦閱讀更多精彩內容