磁盘格式化 磁盘挂载 手动增长swap空间

1.磁盘格式化node

格式化就是安装文件系统。Centos 7以XFS做为默认的文件系统,能够给它指定ext3或者ext4格式。linux

命令mke2fs、mkfs.ext二、mkfs.ext三、mkfs.ext四、和mkfs.xfs。安全

mke2fs经常使用命令bash

-b:表示分区时设定每一个数据区块占用的空间大小。每一个数据块支持1024B、2048B、4096B.app

-i:表示设定inode的大小。async

-N:表示设定inode的数量。ide

-c:表示在格式化以前先检测一下磁盘对否有问题。ui

-L:表示预设该分区的标签(label)this

-j:表示创建ext3格式的分区=mkfs.ext3。spa

-t:用来指定文件系统的类型。示例命令以下:

[root@aminglinux-128 ~]# mke2fs -t ext4 /dev/sdb5
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
64000 inodes, 256000 blocks
12800 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=262144000
8 block groups
32768 blocks per group, 32768 fragments per group
8000 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

命令mke2fs -t ext4 /dev/sdb5等同于mkfs.ex4 /dev/sdb5。mke2fs并不支持把分区格式化成XFS类型,而只能使用mkfs.xfs,示例命令以下:

[root@aminglinux-128 ~]# mke2fs -t xfs /dev/sdb6
mke2fs 1.42.9 (28-Dec-2013)

Your mke2fs.conf file does not define the xfs filesystem type.
Aborting...
[root@aminglinux-128 ~]# mke2fs.xfs /dev/sdb6
bash: mke2fs.xfs: 未找到命令...
[root@aminglinux-128 ~]# mkfs.xfs /dev/sdb6
meta-data=/dev/sdb6              isize=512    agcount=4, agsize=64000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=256000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

磁盘在格式化化的时候,会预先规定好每个块的大小,而后再把空间分割成一个一个的小块。存数据时的时候,也是一个块一个块地写入。若是磁盘里存储的都是特别小的文件,在格式化磁盘时指定块数值小一点。

[root@aminglinux-128 ~]# mkfs.xfs -b size=8192 /dev/sdb6
mkfs.xfs: /dev/sdb6 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
[root@aminglinux-128 ~]# mkfs.xfs -f -b size=8192 /dev/sdb6
meta-data=/dev/sdb6              isize=512    agcount=4, agsize=32000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=8192   blocks=128000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=8192   ascii-ci=0 ftype=1
log      =internal log           bsize=8192   blocks=559, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=8192   blocks=0, rtextents=0

上面实验能够发现,若是指定块大小为8192B,会提示块值设置得太大了,能够直接输入y强制格式化。mkfs.xfs用法有点特殊,须要注意和mke2fs区分。能够给分区指定标签,示例以下:

[root@aminglinux-128 ~]# mke2fs -L TEST -t ext4 /dev/sdb5
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=TEST
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
64000 inodes, 256000 blocks
12800 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=262144000
8 block groups
32768 blocks per group, 32768 fragments per group
8000 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

上例中使用-L选项来指定标签。除非有特殊需求,不然不须要指定块大小。

2.命令e2label

该命令用于查看或修改分区的标签,它只支持ext格式的文件系统,而不支持XFS文件系统。示例以下:

[root@aminglinux-128 ~]# e2label /dev/sdb5
TEST
[root@aminglinux-128 ~]# e2label /dev/sdb5 TEST123
[root@aminglinux-128 ~]# e2label /dev/sdb5 
TEST123

3.挂载/卸载磁盘

格式化后的磁盘是一个块设备文件,类型为b。

在挂载某个分区前,须要先创建一个挂载点,这个挂载点是以目录的形式出现的。一旦把某个分区挂载到这个挂载点(目录)下,要往这个目录写数据时,就会写到改分区中。因此,在挂载该分区前,挂载点(目录)下必须是个空目录。

4.命令mount

[root@aminglinux-128 ~]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=483152k,nr_inodes=120788,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,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/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)

这个命令,能够查看当前系统已经挂载的全部分区、分区文件类型、挂载点及一些选项等信息。未挂载的分区使用blkid命令查看。示例以下:

[root@aminglinux-128 ~]# mkdir /newdir
[root@aminglinux-128 ~]# touch /newdir/newfile.txt
[root@aminglinux-128 ~]# ls /newdir/
newfile.txt
[root@aminglinux-128 ~]# mouunt /dev/sdb5 /newdir/
bash: mouunt: 未找到命令...
类似命令是: 'mount'
[root@aminglinux-128 ~]# ls /newdir/
newfile.txt
[root@aminglinux-128 ~]# df -h |grep -v tmpfs
文件系统        容量  已用  可用 已用% 挂载点
/dev/sda3        76G  3.6G   73G    5% /
/dev/sda1       197M  111M   86M   57% /boot
[root@aminglinux-128 ~]# umount /newdir/
umount: /newdir/:未挂载
[root@aminglinux-128 ~]# df -h |grep tmpfs
devtmpfs        472M     0  472M    0% /dev
tmpfs           488M     0  488M    0% /dev/shm
tmpfs           488M  8.1M  480M    2% /run
tmpfs           488M     0  488M    0% /sys/fs/cgroup
tmpfs            98M     0   98M    0% /run/user/0

/etc/fstab 配置文件

[root@aminglinux-128 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed May 23 19:32:00 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=23c138ca-3a70-447e-9028-e002f1d0dea9 /                       xfs     defaults        0 0
UUID=28947ea8-fda8-4882-8f92-e0e192c7106c /boot                   xfs     defaults        0 0
UUID=2b7f5930-1da9-42bd-817e-c675a5fe16ec swap                    swap    defaults        0 0

这个文件中显示了系统启动时须要挂载的各个分区。

第4列经常使用选项

async/sync:async表示与磁盘和内存不一样步。sync则会时时同步内存和磁盘中的数据。

auto/noauto:表示开机自动挂载/不自动挂载。

default:表示按照大多数永久文件系统的默认值设置挂载定义。

ro:表示按只读权限挂载。

rw:表示按可读可写权限挂载。

exec/noexec:表示容许/不容许可执行文件执行。

user/nouser:表示容许/不容许root外的其它用户挂载分区。

suid/nosuid:表示容许/不容许分区有suid属性。

usrquota:表示启动用户的磁盘配额模式。

grquota:表示群组的磁盘配额模式。

mount -a 把/etc/fstab中出现的全部磁盘分区挂载上。

-t:用来指定挂载的分区类型,默认不指定,会自动识别。

-o:用来指定挂载的分区有哪些特性,即/etc/fstab配置文件中第4列那些。(经常使用)示例以下:

[root@aminglinux-128 ~]# mkdir /newdir/dir1
[root@aminglinux-128 ~]# mount -o remount,ro,sync /dev/sdb5 /newdir/
[root@aminglinux-128 ~]# mkdir /newdir/dir2
mkdir: 没法建立目录"/newdir/dir2": 只读文件系统

 

-o指定了ro参数,该分区只读了。mount 命令能够看到,/dev/sdb5也有ro参数。示例以下:

[root@aminglinux-128 ~]# mount |grep sdb5
/dev/sdb5 on /newdir type ext4 (ro,relatime,sync,data=ordered)

从新挂载,恢复读写。

[root@aminglinux-128 ~]# mount |grep sdb5
/dev/sdb5 on /newdir type ext4 (ro,relatime,sync,data=ordered)
[root@aminglinux-128 ~]# mount -o remount /newdir 
[root@aminglinux-128 ~]# mkdir /newdir/dir2
[root@aminglinux-128 ~]# ls /newdir/
dir1  dir2  lost+found

5.命令blkid

获取各分区的UUID。

[root@aminglinux-128 ~]# blkid
/dev/sda1: UUID="28947ea8-fda8-4882-8f92-e0e192c7106c" TYPE="xfs" 
/dev/sda2: UUID="2b7f5930-1da9-42bd-817e-c675a5fe16ec" TYPE="swap" 
/dev/sda3: UUID="23c138ca-3a70-447e-9028-e002f1d0dea9" TYPE="xfs" 
/dev/sdb5: LABEL="TEST123" UUID="48d2ff4a-3de2-4644-a8c4-7a3165d9891a" TYPE="ext4" 
/dev/sdb6: UUID="842793ba-b318-4b84-bed9-e77c793492e1" TYPE="xfs" 
/dev/sr0: UUID="2018-05-03-20-55-23-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"

指定查询哪一个分区,示例以下:

[root@aminglinux-128 ~]# blkid /dev/sdb5
/dev/sdb5: LABEL="TEST123" UUID="48d2ff4a-3de2-4644-a8c4-7a3165d9891a" TYPE="ext4"

分区在开机后自动挂载的两个方法:一是在/etc/fstab中添加一行。二是把挂载命令写到/etc/ec.d/rc.local文件中去(直接放到这个文件的最后),系统启动后会执行这个文件中的命令。

[root@aminglinux-128 ~]# cat /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

/usr/bin/mount UUID="48d2ff4a-3de2-4644-a8c4-7a3165d9891a" /newdir

写入到该文件的命令,要使用绝对路径。

打开执行权限。

chmod a+x /etc/rc.d/rc.local

6.命令umount

umount这个命令能够跟挂载点,分区名(/dev/sdb1),不能够跟LABEL和UUID。

7.手动增长swap空间

创建swapfile-格式化-swap格式-启用该虚拟磁盘

[root@aminglinux-128 ~]# dd if=/dev/zero of=/tmp/newdisk bs=1M count=1024

记录了1024+0 的读入
记录了1024+0 的写出
1073741824字节(1.1 GB)已复制,50.6039 秒,21.2 MB/秒

of指定目标文件,bs定义块大小,count定义块的数量。

格式化swap格式,命令以下:

[root@aminglinux-128 ~]# mkswap -f /tmp/newdisk
正在设置交换空间版本 1,大小 = 1048572 KiB
无标签,UUID=8472ee9a-9c0f-44a4-8263-bf5e72a8ea36

挂载使用

[root@aminglinux-128 ~]# mkswap -f /tmp/newdisk
正在设置交换空间版本 1,大小 = 1048572 KiB
无标签,UUID=8472ee9a-9c0f-44a4-8263-bf5e72a8ea36
[root@aminglinux-128 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            974         177          70           8         726         602
Swap:          4095           0        4095
[root@aminglinux-128 ~]# swapon /tmp/newdisk
swapon: /tmp/newdisk:不安全的权限 0644,建议使用 0600。
[root@aminglinux-128 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            974         178          69           8         726         602
Swap:          5119           0        5119
[root@aminglinux-128 ~]#

free  -m:表示以M

相关文章
相关标签/搜索