分区工具: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 创建分区
示例
parted.png3. 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
网友评论