分區工具: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中的說明很清楚,分區操作也有進一步提示,完全可以按提示執行
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 創建分區
示例
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)
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