Linux之磁盘分区及格式化

df命令:查看文件系统磁盘使用情况

[root@localhost ~]# df
文件系统          1K-块    已用     可用 已用% 挂载点
/dev/sda2      18658304 1176496 17481808    7% /
devtmpfs         494380       0   494380    0% /dev
tmpfs            504196       0   504196    0% /dev/shm
tmpfs            504196    6832   497364    2% /run
tmpfs            504196       0   504196    0% /sys/fs/cgroup
/dev/sda1        201380  111464    89916   56% /boot
tmpfs            100840       0   100840    0% /run/user/0
  • df -h :使用合适的单位
[root@localhost ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
/dev/sda2        18G  1.2G   17G    7% /
devtmpfs        483M     0  483M    0% /dev
tmpfs           493M     0  493M    0% /dev/shm
tmpfs           493M  6.7M  486M    2% /run
tmpfs           493M     0  493M    0% /sys/fs/cgroup
/dev/sda1       197M  109M   88M   56% /boot
tmpfs            99M     0   99M    0% /run/user/0

文件系统中带有‘tmpfs’表示临时文件系统,/dev/shm 表示内存,为物理内存的一半。

  • df -m :以MB为单位

  • df -i :文件系统中inode使用情况

inode数量在磁盘格式化时已确定,跟磁盘大小、块大小有关

inode使用率到100%后,即使磁盘还有空间也无法写入

free:查看 swap 分区使用情况

[root@localhost ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1008392      110844      751812        6836      145736      735592
Swap:       2097148           0     2097148

du命令:查看文件或者目录大小

  • du filename :默认以 k 为单位显示文件大小
[root@localhost ~]# du /tmp/02.txt 
0	/tmp/02.txt
  • du -s directory :查看目大小
[root@localhost ~]# du -s /tmp/
16	/tmp/
  • du -h:使用适当的单位
[root@xxlinux-02 ~]# du -sh /root
48K /root
[root@xxlinux-02 ~]# du -sh /var
116M /var
  • 其他选项

  • -a或-all 显示目录中个别文件的大小

  • -b或-bytes 显示目录或文件大小时,以byte为单位。

  • -c或--total 除了显示个别目录或文件的大小外,同时也显示所有目录或文件的总和。

  • -k或--kilobytes 以KB(1024bytes)为单位输出

  • -m或--megabytes 以MB为单位输出

  • -s或--summarize 仅显示总计,只列出最后加总的值

  • -h或--human-readable 以K,M,G为单位,提高信息的可读性

  • -x或--one-file-xystem 以一开始处理时的文件系统为准,若遇上其它不同的文件系统目录则略过

  • -L<符号链接>或--dereference<符号链接> 显示选项中所指定符号链接的源文件大小

  • -S或--separate-dirs 显示个别目录的大小时,并不含其子目录的大小

  • -X<文件>或--exclude-from=<文件> 在<文件>指定目录或文件

  • --exclude=<目录或文件> 略过指定的目录或文件

  • -D或--dereference-args 显示指定符号链接的源文件大小

  • -H或--si 与-h参数相同,但是K,M,G是以1000为换算单位

  • -l或--count-links 重复计算硬件链接的文件


磁盘分区

小于等于 2TB 的磁盘使用 fdisk

fdisk 最大支持 2TB,最多划分 4个主分区(如果要划分超过4个分区,只能通过在扩展分区里继续划分逻辑分区)
主分区数量+扩展分区数量之和最大为 4.主分区分区号默认从1开始,起始扇区默认2048.
逻辑分区分区号默认从5开始,且为连续的,分区号不会留空

w:查看当前有哪些用户登录到系统中

[root@localhost zhouqiangCD]# w
 02:20:34 up 22:23,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0     四04    2.00s  0.38s  0.12s w

在增加了新的磁盘后,先要进行分区,然后才能使用

fdisk -l:列出当前系统中所有磁盘信息

[root@xxlinux-02 ~]# fdisk -l
磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000539dc
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 391167 194560 83 Linux
/dev/sda2 391168 4390911 1999872 82 Linux swap / Solaris
/dev/sda3 4390912 41943039 18776064 83 Linux
磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

fdisk 磁盘:给磁盘分区

[root@xxlinux-02 ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0xf851798c 创建新的 DOS 磁盘标签
命令(输入 m 获取帮助):

m:获取帮助信息

命令(输入 m 获取帮助):m
命令操作
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

n:新建分区

命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-20971519,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-20971519,默认为 20971519):+2G
分区 1 已设置为 Linux 类型,大小设为 2 GiB

p表示主分区,e表示扩展分区

p:查看分区情况

命令(输入 m 获取帮助):p
磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xeebb402a
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux

d:删除分区

命令(输入 m 获取帮助):p
磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xeebb402a
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 10487807 3145728 83 Linux
/dev/sdb3 10487808 18876415 4194304 83 Linux
命令(输入 m 获取帮助):d
分区号 (1-3,默认 3):2
分区 2 已删除
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xeebb402a
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb3 10487808 18876415 4194304 83 Linux

w:保存(分区完成后保存即可生效)


磁盘格式化

linux支持的文件系统格式

[root@localhost ~]# cat /etc/filesystems
xfs
ext4
ext3
ext2
nodev proc
nodev devpts
iso9660
vfat
hfs
hfsplus
*

CentOS 7 默认为 xfs格式。之前的版本为 ext 格式(包括 ext2,ext3,ext4)

查看分区的文件系统格式

[root@localhost ~]# mount
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=499860k,nr_inodes=124965,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (rw,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/net_cls type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda3 on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=32,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)

mke2fs命令

  • -t :指定某种文件系统格式
[root@localhost ~]# mke2fs -t ext4 /dev/sdb
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb is entire device, not just one partition!
无论如何也要继续? (y,n) y
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242880 blocks
262144 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2153775104
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

不指定文件系统格式的情况下默认为ext2

  • -b :指定每个块占用空间大小
[root@localhost ~]# mke2fs -t ext4 -b 2048 /dev/sdb
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb is entire device, not just one partition!
无论如何也要继续? (y,n) y
文件系统标签=
OS type: Linux
块大小=2048 (log=1)
分块大小=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 10485760 blocks
524288 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=278921216
640 block groups
16384 blocks per group, 16384 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
        16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104,
        2048000, 3981312, 5619712, 10240000
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

块的值越大,读写的速度就越快,相对会占用更多磁盘空间

  • -m :设定分区预留给root用户的空间百分比
[root@localhost ~]# mke2fs -t ext4 -m 0.5 /dev/sdb
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb is entire device, not just one partition!
无论如何也要继续? (y,n) y
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242880 blocks
26214 blocks (0.50%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2153775104
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

不设定的情况下默认为 5%

  • -i :设定inode的大小(多少个字节对应一个inode,最小值未一个块的大小)
[root@localhost ~]# mke2fs -t ext4 -i 8192 /dev/sdb
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb is entire device, not just one partition!
无论如何也要继续? (y,n) y
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2621440 inodes, 5242880 blocks
262144 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2153775104
160 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

值越小,inode的数量越多

  • -L :预设该分区的标签label
[root@localhost ~]# mke2fs -t ext4 -L 指定标签 /dev/sdb
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb is entire device, not just one partition!
无论如何也要继续? (y,n) y
文件系统标签=指定标签
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242880 blocks
262144 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2153775104
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

标签可以在挂载磁盘的时候使用,也可以写到配置文件里

其他选项

  • -N :设定inode数量,在默认的inode数不够用时需要自行设定inode数量

  • -c :在格式化前先检测一下磁盘是否有问题,加上这个选项后会非常慢


posted @ 2017-09-20 23:46  指环王Raul  阅读(865)  评论(0编辑  收藏  举报