1总结整理磁盘管理及文件系统管理中设计的各种命令的使用,并附注一定的示例;

  磁盘管理命令:

   磁盘分区工具:fdisk,parted,sfdisk

   fdisk的使用:

   最多支持在一块硬盘上的15个分区;

       分区管理子命令:

p: 显示

n: 创建

d: 删除

t: 修改分区ID

l: 列出所有支持ID类型

w: 保存退出

q: 放弃修改并退出

m: 获取帮助

 示例:

  [root@localhost ~]#fdisk /dev/sda  

  Command (m for help): n     

  Command action           

   e  extended

   p  primary partition (1-4)

e

Partition number(1-4): 4         

First cylinder(1-15665, default 1): 1          

Last cylinder,+cylinders or +size{K,M,G} (1-63, default 63): +10G 

Command (m forhelp):w        

# cat /proc/partitions

 有三个命令可以让内核重读磁盘分区表:

CentOS 5: partprobe[DEVICE]

CentOS 6,7:

partx

kpartx

 partx命令:

partx DEVICE

partx -a DEVICE

partx -a -n M:N DEVICE

M

M:

:N

       kpartx命令:

kpartx -af DEVICE

Linux文件系统管理命令:

mkfs, mkfs -t type =mkfs.type

 ext:mke2fs

 创建文件系统:

mke2fs [OPTION]... DEVICE

-t {ext2|ext3|ext4}

-b {1024|2048|4096}

-L 'LABEL'

-j: mke2fs -t ext3

-i #

-N #

-m #: 预留磁盘空间占据多大百分比的空间为后期管理使用;

-O FEATURE[,...]

-O ^FEATURE:关闭此特性

 文件系统属性查看及调整工具:

e2label

e2lable DEVICE [LABEL]

tune2fs

显示ext系列文件系统的属性,或调整其属性;

-l:显示超级块中的信息;显示整个文件的属性及布局等相关信息;

-L 'LABEL':修改卷标;

-m #: 调整预留给管理员的管理空间百分比;

-j: ext2 --> ext3

-O:文件系统属性的启动或关闭

-o:文件系统默认挂载选项的启用或关闭

示例:

[root@localhost ~]#tune2fs -L 'liaodijin' /dev/sda1     \\用tune2fs修改卷标名为liaodijin

 tune2fs 1.41.12 (15-Sep-2015)

 dumpe2fs

-h: 仅显示超级块信息;      

blkid命令:

blkid DEVICE

  LABEL, UUID, TYPE

[root@localhost ~]#blkid /dev/sda1   \\使用blkid查看分区UUID TYPE及卷标(即分区名)。

 /dev/sda1: UUID="46d989e4-d6d6-42d0-8562-e2c1543f6881"TYPE="xfs"

文件系统检测:

fsck: Filesystem check

fsck -t type

fsck.type

-a: 自动修复错误

-r: 交互式修复错误

-f: 强制检测

 e2fsck:ext系列文件系统专用的检测修复工具;

-y: 自动回答为“yes”

-f:force

文件系统的挂载和使用:

将额外文件系统与根文件系统某现存的目录建立起关联关系,进而使得此目录做为其它文件系统访问入口的行为,称之为挂载;

解除此关联关系的过程:卸载;

挂载点:mount point, 设备挂载至目录;

注意:挂载点在挂载在之后,其内部原有的文件会被暂时隐藏;建立使用空目录做为挂载点;

挂载方法:

mount:通过读取/etc/mtab文件来显示当前系统所有已经挂载的设备;

mount -a:挂载/etc/fstab文件中的所有支持自动挂载的文件系统;

mount [options] [-ooptions] DEVICE MOUNT_POINT

[options]:命令选项

[-o options]:挂载选项

DEVICE: 要挂载的设备

(1) 设备文件:/dev/sda5

(2) 卷标:-L 'LABEL'

(3) UUID:-U 'UUID'

(4) 伪文件系统名称

MOUNT_POINT: 挂载点

常用选项:

-t type:文件系统类型

-r: 以“只读”方式挂载此文件系统

-w: 以“读写”方式挂载此文件系统

-n:每个文件系统在挂载时都会自动更新/etc/mtab文件,-n用于禁止此功能;

此时,如果想查看挂载的所有文件系统:cat/proc/mounts

-a: 自动挂载所有支持自动挂载的设备;

-B:绑定目录至另一个目录上;

     -o 挂载选项:

async:异步模式

sync: 同步模式

atime/noatime:是否更新访问时间戳;

diratime/nodiratime:是否更新目录的访问时间戳;

auto/noauto: 是否允许此设备被自动挂载;

exec/noexec:是否允许执行此文件系统上应用程序;

dev/nodev:是否支持在此设备上使用设备文件;

suid/nosuid

remount:重新挂载

ro

rw

user/nouser: 是否允许普通挂载此设备;

acl:是否支持此设备上使用facl;

defaults:rw, suid, dev, exec, auto, nouser, andasync

umount:卸载命令

# umount DEVICE

# umount MOUNT_POINT

查看正在访问指定挂载点的进程:

# fuser -v MOUNT_POINT

终止所有正在访问指定的挂载点的进程:

# fuser -kmMOUNT_POINT

 

交换分区:

free命令:

查看memory和swap的使用状态

-m: 以MB为单位

-g: 以GB为单位

mkswap:创建交换分区

mkswap [option] DEVICE

-L 'LABEL'

swapon:启用交换分区

swapon [option][DEVICE]

-a: 激活所有交换分区

-p PRIORITY: 设定其优先级;

swapoff:禁用交换分区

swapoff [option][DEVICE]

 

文件系统等空间占用信息的查看工具:

df: disk free

-h: human-readable

-i: inode数量

-P: 以Posix兼容的格式输出

du: disk usage

-h: human-readable

-s: summary

 

文件系统相关挂载配置文件:/etc/fstab

 

每行定义一个文件系统;

要挂载的设备或伪文件系统  挂载点 文件系统类型 挂载选项 转储频率  自检次序

要挂载的设备:

设备文件、LABEL=、UUID=、伪文件系统名称

挂载选项:

defaults

转储频率:

0: 不转储

1: 每天转储

2: 每隔一天转储

自检次序:

0:不自检

1:首先自检,通常只有/才为1

2

...

符号链接:

链接文件的数据指向另一个文件路径;

可以对目录进行;

可以跨分区;

指向的是另一个文件路径;而非inode;

对文件创建符号链接不会增加其引用计数;删除原文件,符号链接文件也将失效;

ln [-s] SRC DEST

-s:symbolic link

-v:verbose

2创建一个10G的文件系统,类型为ext4,要求开机可自动挂载至/mydata目录

 [root@localhost ~]# fdisk /dev/sda    

 WARNING:DOS-compatible mode is deprecated. It's strongly recommended to

         switch off the mode (command 'c') andchange display units to

         sectors (command 'u').

 Command (m for help):p

 Disk /dev/sda: 128.8GB, 128849018880 bytes

255 heads, 63sectors/track, 15665 cylinders

Units = cylinders of16065 * 512 = 8225280 bytes

Sector size(logical/physical): 512 bytes / 512 bytes

I/O size(minimum/optimal): 512 bytes / 512 bytes

Disk identifier:0x00085376

    Device Boot      Start         End      Blocks  Id  System

/dev/sda1   *          1          26      204800  83  Linux

Partition 1 does notend on cylinder boundary.

/dev/sda2              26        6553   52428800   8e  Linux LVM

 Command (m for help):n

Command action

   e  extended

   p  primary partition (1-4)

p

Partition number(1-4): 3

First cylinder(6553-15665, default 6553):

Using default value6553

Last cylinder,+cylinders or +size{K,M,G} (6553-15665, default 15665): +10G

 Command (m for help):p

 Disk /dev/sda: 128.8GB, 128849018880 bytes

255 heads, 63sectors/track, 15665 cylinders

Units = cylinders of16065 * 512 = 8225280 bytes

Sector size(logical/physical): 512 bytes / 512 bytes

I/O size(minimum/optimal): 512 bytes / 512 bytes

Disk identifier:0x00085376

    Device Boot      Start         End      Blocks  Id  System

/dev/sda1   *          1          26      204800  83  Linux

Partition 1 does notend on cylinder boundary.

/dev/sda2              26        6553   52428800   8e  Linux LVM

/dev/sda3            6553        7858   10484761   83  Linux

 Command (m for help):w

The partition tablehas been altered!

 Calling ioctl() tore-read partition table.

 WARNING: Re-readingthe partition table failed with error 16: Device or resource busy.

The kernel still usesthe old table. The new table will be used at

the next reboot or afteryou run partprobe(8) or kpartx(8)

Syncing disks.

[root@localhost ~]#cat /proc/partitions

major minor  #blocks name

    8       0  125829120 sda

   8       1     204800 sda1

   8       2   52428800 sda2

 253       0   29356032 dm-0

 253       1    2097152 dm-1

 253       2   20971520 dm-2

[root@localhost ~]#partx -a /dev/sda

BLKPG: Device orresource busy

error adding partition1

BLKPG: Device orresource busy

error adding partition2

[root@localhost ~]#partx -a /dev/sda3    

last arg is not thewhole disk

call: partx -optsdevice wholedisk

[root@localhost ~]#cat /proc/partitions 

major minor  #blocks name

    8       0  125829120 sda

   8       1     204800 sda1

   8       2   52428800 sda2

   8       3   10484761 sda3

 253       0   29356032 dm-0

 253       1    2097152 dm-1

 253       2   20971520 dm-2

[root@localhost ~]#mke2fs -t ext4 /dev/sda3    

mke2fs 1.41.12(17-May-2010)

Filesystem label=

OS type: Linux

Block size=4096(log=2)

Fragment size=4096(log=2)

Stride=0 blocks,Stripe width=0 blocks

655360 inodes, 2621190blocks

131059 blocks (5.00%)reserved for the super user

First data block=0

Maximum filesystemblocks=2684354560

80 block groups

32768 blocks pergroup, 32768 fragments per group

8192 inodes per group

Superblock backupsstored on blocks:

    32768, 98304, 163840, 229376, 294912,819200, 884736, 1605632

 Writing inode tables:done                           

Creating journal(32768 blocks): done

Writing superblocksand filesystem accounting information: done

 This filesystem willbe automatically checked every 24 mounts or

180 days, whichevercomes first.  Use tune2fs -c or -i tooverride.

[root@localhost ~]#mkdir /mydata    

[root@localhost ~]#mount /dev/sda3 /mydata

[root@localhost ~]# df-TH   

Filesystem           Type   Size Used Avail Use% Mounted on

/dev/mapper/vol0-root

                     ext4    30G 397M   28G   2% /

tmpfs                tmpfs  515M  78k  515M   1% /dev/shm

/dev/sda1            ext4   199M  38M  152M  20% /boot

/dev/mapper/vol0-usrext4    22G  3.2G  17G  16% /usr

/dev/sda3            ext4    11G  24M  9.9G   1% /mydata

[root@localhost ~]#vim /etc/fstab   

# /etc/fstab

# Created by anacondaon Mon Sep  7 23:06:37 2015

#

# Accessiblefilesystems, by reference, are maintained under '/dev/disk'

# See man pagesfstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

/dev/mapper/vol0-root   /                       ext4    defaults        1 1

UUID=7ae1abef-3d62-4c11-8c6d-f3b776696d61/boot                   ext4    defaults        1 2

/dev/mapper/vol0-usr    /usr                    ext4    defaults        1 2

/dev/mapper/vol0-swap   swap                    swap    defaults        0 0

/dev/sda3               /mydata                 ext4   defaults        0 0

tmpfs                   /dev/shm                tmpfs   defaults        0 0

devpts                  /dev/pts                devpts  gid=5,mode=620  0 0

sysfs                   /sys                    sysfs   defaults       0 0

proc                    /proc                   proc    defaults        0 0