您的位置:首页 > 运维架构 > Linux

三周第五次课

2017-12-31 00:00 169 查看
三周第五次课

4.5/4.6 磁盘格式化

4.7/4.8 磁盘挂载

4.9 手动增加swap空间

4.5/4.6 磁盘格式化

磁盘虽然分好区了,如果没有进行格式化,还是不能用的。所谓格式化,其实就是安装文件系统,windows下的文件系统有FAT32(基本已经过时)和NTFS。CentOS7以XFS为默认的文件系统,但我们依然可以给它指定ext3和ext4格式。

下面是CentOS7支持的文件系统格式

[root@localhost ~]# cat /etc/filesystems

xfs

ext4

ext3

ext2

nodev proc

nodev devpts

iso9660

vfat

hfs

hfsplus

*

[root@localhost ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda3 26G 7.2G 19G 29% /

devtmpfs 898M 0 898M 0% /dev

tmpfs 912M 0 912M 0% /dev/shm

tmpfs 912M 9.0M 903M 1% /run

tmpfs 912M 0 912M 0% /sys/fs/cgroup

/dev/sda1 378M 162M 216M 43% /boot

tmpfs 183M 12K 183M 1% /run/user/42

tmpfs 183M 0 183M 0% /run/user/0

[root@localhost ~]#

/或者/boot就是XFS文件系统

用mount命令查看一个分区的的文件系统

[root@localhost ~]# mount

sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)

proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)

devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=918828k,nr_inodes=229707,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 (ro,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/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)

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/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)

cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)

cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)

cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_prio,net_cls)

cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)

cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)

cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)

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=25,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=13714)

mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)

debugfs on /sys/kernel/debug type debugfs (rw,relatime)

hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)

nfsd on /proc/fs/nfsd type nfsd (rw,relatime)

/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)

tmpfs on /run/user/42 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=186708k,mode=700,uid=42,gid=42)

tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=186708k,mode=700)

[root@localhost ~]#

上面的文件有很多,但是我们只看/dev下的即可。/dev/sda3和/dev/sda1都是xfs文件系统。

swap是swap分区格式,是它自有的一种格式。

/和/boot都是XFS,而CentOS6之间的版本,使用的都是ext格式,6是ext4,5是ext3,再往前是ext2。ext4、3、2是它们的版本,它们都是ext文件系统。只不过在CentOS7之后,改成了XFS文件系统。XFS和ext都差不多,现在可以格式化成ext4,也可以格式化成ext3,甚至是ext2,但是默认的是XFS。既然CentOS7默认的是XFS文件系统,那我们就按照它的默认走。

现在查看分区

[root@localhost ~]# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors

Units = sectors of 1 * 512 = 512 bytes

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

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

Disk label type: dos

Disk identifier: 0x000c51b7

Device Boot Start End Blocks Id System

/dev/sda1 * 2048 782335 390144 83 Linux

/dev/sda2 782336 9170943 4194304 82 Linux swap / Solaris

/dev/sda3 9170944 62914559 26871808 83 Linux

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors

Units = sectors of 1 * 512 = 512 bytes

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

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

Disk label type: dos

Disk identifier: 0xb0c618aa

Device Boot Start End Blocks Id System

[root@localhost ~]#

发现没有分区,现在格式化一个分区

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

Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.

Command (m for help): n

Partition type:

p primary (0 primary, 0 extended, 4 free)

e extended

Select (default p): p

Partition number (1-4, default 1):

First sector (2048-20971519, default 2048):

Using default value 2048

Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +3G

Partition 1 of type Linux and of size 3 GiB is set

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

[root@localhost ~]# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors

Units = sectors of 1 * 512 = 512 bytes

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

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

Disk label type: dos

Disk identifier: 0x000c51b7

Device Boot Start End Blocks Id System

/dev/sda1 * 2048 782335 390144 83 Linux

/dev/sda2 782336 9170943 4194304 82 Linux swap / Solaris

/dev/sda3 9170944 62914559 26871808 83 Linux

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors

Units = sectors of 1 * 512 = 512 bytes

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

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

Disk label type: dos

Disk identifier: 0xb0c618aa

Device Boot Start End Blocks Id System

/dev/sdb1 2048 6293503 3145728 83 Linux

[root@localhost ~]#

当用man命令查询mke2fs、mkfs.ext2、mkfs.ext3、mkfs.ext4这4个命令的帮助文档时,发现它们是同一个文档,这说明这4个命令是一样的。

mke2fs的常用选项如下所示:

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

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

-N:表示设定inode的数量。有时候默认的inode数不够用,所以要自定义inode的数量。

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

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

-j:表示建立ext3格式的分区。如果使用mkfs.ext3格式,就不用加这个选项了。

-t:用来指定文件系统的类型,可以是ext2、ext3,也可以是ext4。

[root@localhost ~]# ls -l

total 12

drwxr-xr-x. 2 root root 28 Dec 24 11:34 123

-rw-r--r--. 1 root root 3348 Dec 22 07:36 2.txt

-rw-r--r--. 1 root root 0 Dec 30 15:14 3.txt

drwxr-xr-x. 3 root root 36 Dec 24 11:31 456

lrwxrwxrwx. 1 root root 3 Dec 24 10:34 789 -> 456

-rw-------. 1 root root 2123 Dec 21 12:19 anaconda-ks.cfg.1

-rw-r--r--. 1 root root 2171 Dec 21 12:41 initial-setup-ks.cfg

drwxr-xr-x. 2 root root 6 Dec 21 12:46 perl5

[root@localhost ~]# du -sh 3.txt

0 3.txt

[root@localhost ~]# echo 1 > 3.txt

[root@localhost ~]# ls -lh 3.txt

-rw-r--r--. 1 root root 2 Dec 30 15:15 3.txt

[root@localhost ~]# du -sh 3.txt

4.0K 3.txt

[root@localhost ~]#

du -sb和ls -lh查看的结果相同

[root@localhost ~]# du -sb 3.txt

2 3.txt

[root@localhost ~]# ls -lh 3.txt

-rw-r--r--. 1 root root 2 Dec 30 15:15 3.txt

[root@localhost ~]#

[root@localhost ~]# mke2fs -t xfs -b 2048 /dev/sdb1

mke2fs 1.42.9 (28-Dec-2013)

Your mke2fs.conf file does not define the xfs filesystem type.

Aborting...

[root@localhost ~]#

mkfs.ext4和mke2fs -t ext4是一样的。

下面格式化成ext4文件系统

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

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

196608 inodes, 786432 blocks

39321 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=805306368

24 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912

Allocating group tables: done

Writing inode tables: done

Creating journal (16384 blocks): done

Writing superblocks and filesystem accounting information: done

[root@localhost ~]#

分区即使格式化过一次,还可以再次格式化。

[root@localhost ~]# mkfs.ext4 /dev/sdb1

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

196608 inodes, 786432 blocks

39321 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=805306368

24 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912

Allocating group tables: done

Writing inode tables: done

Creating journal (16384 blocks): done

Writing superblocks and filesystem accounting information: done

[root@localhost ~]#

CentOS7不支持mke2fs -t xfs命令,可以使用mkfs.xfs命令,它会提示使用-f选项。

[root@localhost ~]# mkfs.xfs /dev/sdb1

mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (ext4).

mkfs.xfs: Use the -f option to force overwrite.

[root@localhost ~]# mkfs.xfs -f /dev/sdb1

meta-data=/dev/sdb1 isize=512 agcount=4, agsize=196608 blks

= sectsz=512 attr=2, projid32bit=1

= crc=1 finobt=0, sparse=0

data = bsize=4096 blocks=786432, imaxpct=25

= sunit=0 swidth=0 blks

naming =version 2 bsize=4096 ascii-ci=0 ftype=1

log =internal log bsize=4096 blocks=2560, version=2

= sectsz=512 sunit=0 blks, lazy-count=1

realtime =none extsz=4096 blocks=0, rtextents=0

上面有bsize=4096字样,这就涉及到块的概念。磁盘在格式化的时候,会预先规定好每一个快的大小,然后再把所有的空间分割成一个一个的小块。存数据的时候,也是一个块一个块的写入。如果你的磁盘里存储的都是特别小的文件,比如说1KB或者2KB,在格式化的时候可以指定块数据小一点。ext4文件系统的默认块大小为4096B(4KB)。在格式化时,可以指定块大小为1024B、2048B、4096B(它们是成倍增加的)。虽然格式化时可以指定块大小超过4096B,但一旦超过4096B,就不能正常挂载。

使用mount命令是看不到/dev/sdb1的,因为它还没有挂载到任何挂载点上。

[root@localhost ~]# mount

sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)

proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)

devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=918828k,nr_inodes=229707,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 (ro,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/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)

cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)

cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_prio,net_cls)

cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)

cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)

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/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/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)

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=35,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=11167)

hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)

mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)

debugfs on /sys/kernel/debug type debugfs (rw,relatime)

nfsd on /proc/fs/nfsd type nfsd (rw,relatime)

/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)

tmpfs on /run/user/42 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=186708k,mode=700,uid=42,gid=42)

tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=186708k,mode=700)

[root@localhost ~]#

如果要查看一个还没有挂载的磁盘,使用blkid命令

[root@localhost ~]# blkid /dev/sdb1

/dev/sdb1: UUID="1e2f4222-3442-44c5-923e-04ab693bb094" TYPE="xfs"

[root@localhost ~]#

[root@localhost ~]# mke2fs -b 8192 /dev/sdb1

Warning: blocksize 8192 not usable on most systems.

mke2fs 1.42.9 (28-Dec-2013)

mke2fs: 8192-byte blocks too big for system (max 4096)

Proceed anyway? (y,n) n

[root@localhost ~]#

当存储的文件都是大文件时,可以将块的大小设成8192B,下面设成2048B。

[root@localhost ~]# mke2fs -b 2048 /dev/sdb1

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=

OS type: Linux

Block size=2048 (log=1)

Fragment size=2048 (log=1)

Stride=0 blocks, Stripe width=0 blocks

196608 inodes, 1572864 blocks

78643 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=538968064

96 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

Allocating group tables: done

Writing inode tables: done

Writing superblocks and filesystem accounting information: done

[root@localhost ~]#

选项-m设置预留空间大小,后面的数字表示百分比。

[root@localhost ~]# mke2fs -m 1 /dev/sdb1

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

196608 inodes, 786432 blocks

7864 blocks (1.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=805306368

24 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912

Allocating group tables: done

Writing inode tables: done

Writing superblocks and filesystem accounting information: done

[root@localhost ~]#

如果感觉1%过大,可以设置成0.1%。

[root@localhost ~]# mke2fs -m 0.1 /dev/sdb1

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

196608 inodes, 786432 blocks

786 blocks (0.10%) reserved for the super user

First data block=0

Maximum filesystem blocks=805306368

24 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912

Allocating group tables: done

Writing inode tables: done

Writing superblocks and filesystem accounting information: done

[root@localhost ~]#

命令blkid查看文件系统,如果不指定的话,就是ext2。

[root@localhost ~]# blkid /dev/sdb1

/dev/sdb1: UUID="4a0eb261-3721-48b4-a0e2-2b035812c618" TYPE="ext2"

[root@localhost ~]#

下面制定ext4

[root@localhost ~]# mke2fs -t ext4 -m 0.1 /dev/sdb1

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

196608 inodes, 786432 blocks

786 blocks (0.10%) reserved for the super user

First data block=0

Maximum filesystem blocks=805306368

24 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912

Allocating group tables: done

Writing inode tables: done

Creating journal (16384 blocks): done

Writing superblocks and filesystem accounting information: done

[root@localhost ~]#

[root@localhost ~]# mkfs.ext4 -m 0.1 /dev/sdb1

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

196608 inodes, 786432 blocks

786 blocks (0.10%) reserved for the super user

First data block=0

Maximum filesystem blocks=805306368

24 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912

Allocating group tables: done

Writing inode tables: done

Creating journal (16384 blocks): done

Writing superblocks and filesystem accounting information: done

[root@localhost ~]#

mkfs.ext4和mke2fs - t ext4是一样的。

inode是格式化分区的时候创建的。

-i选项指定多少个字节对应一个inode,默认是4个块,每个块是4k,也就是16k对应一个inode,下面改成8k对应一个inode。

[root@localhost ~]# mke2fs -i 8192 -t ext4 /dev/sdb1

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

393216 inodes, 786432 blocks

39321 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=805306368

24 block groups

32768 blocks per group, 32768 fragments per group

16384 inodes per group

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912

Allocating group tables: done

Writing inode tables: done

Creating journal (16384 blocks): done

Writing superblocks and filesystem accounting information: done

[root@localhost ~]#

我们可以使用-L选项来指定标签,标签会在挂载磁盘时使用,也可以写入配置文件。

[root@centos7 ~]# mke2fs -L TEST -t ext4 /dev/sdb1

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=TEST

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

305216 inodes, 1220699 blocks

61034 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=1251999744

38 block groups

32768 blocks per group, 32768 fragments per group

8032 inodes per group

Superblock backups stored on blocks:

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

Allocating group tables: done

Writing inode tables: done

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done

[root@centos7 ~]#

除非有要求,否则不需要指定块大小,也就是说,只需要记住-t和-L两个选项即可。

命令e2label用于查看或修改分区的标签,它只支持ext格式的文件系统,而不支持XFS文件系统,这个很少使用。

[root@centos7 ~]# e2label /dev/sdb1

TEST

[root@centos7 ~]# e2label /dev/sdb1 TEST123

[root@centos7 ~]# e2label /dev/sdb1

TEST123

[root@centos7 ~]#

4.7/4.8 磁盘挂载

之前讲了分区和格式化,那么格式化之后怎么使用磁盘呢?这就涉及到挂载了。格式化后的磁盘其实是一个块设备文件,类型为b。

在linux里面,要想访问一个磁盘,必须挂载到一个挂载点上面。在挂载某个分区前,需要先建立一个挂载点,这个挂载点是以目录的形式出现的。一旦把某个分区挂载到这个挂载点(目录)下,要再往这个目录写数据时,就都会写到该分区中。所以,在挂载该分区前,挂载点(目录)下必须是个空目录。其实目录不为空并不影响所挂载分区的使用,但一旦挂载上了,该目录下以前的东西就看不到了(数据并没有丢失),除非卸载该分区。

以前讲到一个观点,一块磁盘可以不分区直接格式化,也是没有问题的。

只有格式化后,才能挂载文件系统

先把磁盘中的分区删除(不管是否分区,都可格式化)

然后格式化文件系统,mkfs.xfs /dev/sdb

[root@centos7 ~]# fdisk /dev/sdb

WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors

Units = sectors of 1 * 512 = 512 bytes

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

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

Disk label type: gpt

# Start End Size Type Name

1 34 9765625 4.7G Microsoft basic /dev/sdb1

2 9766912 13672447 1.9G Microsoft basic /dev/sdb2

Command (m for help): d

Partition number (1,2, default 2): 1

Partition 1 is deleted

Command (m for help): d

Selected partition 2

Partition 2 is deleted

Command (m for help): w

The partition table has been altered!

alling ioctl() to re-read partition table.

Syncing disks.

[root@centos7 ~]# fdisk /dev/sdb

WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors

Units = sectors of 1 * 512 = 512 bytes

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

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

Disk label type: gpt

# Start End Size Type Name

Command (m for help):

现在/dev/sdb没有分区了,现在直接格式化,格式化成XFS。

[root@tianqi-01 ~]# mkfs.xfs -f /dev/sdb
meta-data=/dev/sdb isize=512 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2621440, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@tianqi-01 ~]#

使用blkid /dev/sdb 命令查看文件系统

[root@tianqi-01 ~]# blkid /dev/sdb
/dev/sdb: UUID="28bfb830-6f0b-4f4b-bb77-010ba9262879" TYPE="xfs"
[root@tianqi-01 ~]#

Linux系统中,磁盘要想访问,必须挂载到一个挂载点上(挂载点就是目录)

使用mount /dev/sdb /mnt/命令进行挂载

[root@tianqi-01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 16G 1.4G 15G 9% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 197M 97M 100M 50% /boot
tmpfs 98M 0 98M 0% /run/user/0
[root@tianqi-01 ~]# mount /dev/sdb /mnt/
[root@tianqi-01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 16G 1.4G 15G 9% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 197M 97M 100M 50% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb 10G 33M 10G 1% /mnt
[root@tianqi-01 ~]#

会看到最后一行多了一个分区,也可称之为磁盘。

在使用mount命令就可以看到磁盘的文件系统

磁盘卸载掉,使用umount命令

使用mount命令,不加任何选项可以查看当前系统已经挂载的所有分区、分区文件系统的类型、挂载点及一些选项等信息。如果想知道某个已挂载分区的文件系统类型,直接用mount命令查看即可。

[root@tianqi-01 ~]# cd /mnt/
[root@tianqi-01 mnt]# ls
[root@tianqi-01 mnt]# touch 1.txt 2.txt
[root@tianqi-01 mnt]# mkdir 124
[root@tianqi-01 mnt]# cd
[root@tianqi-01 ~]# umount /dev/sdb
[root@tianqi-01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 16G 1.4G 15G 9% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 197M 97M 100M 50% /boot
tmpfs 98M 0 98M 0% /run/user/0
[root@tianqi-01 ~]#

若是在当前目录下,可以umount -l /mnt/ 其中 l 表示lazy懒惰的意思

[root@tianqi-01 ~]# mount /dev/sdb /mnt/
[root@tianqi-01 ~]# ls /mnt/
124 1.txt 2.txt
[root@tianqi-01 ~]# cd /mnt/
[root@tianqi-01 mnt]# umount /dev/sdb
umount: /mnt: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@tianqi-01 mnt]# umount -l /mnt/
[root@tianqi-01 mnt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 16G 1.4G 15G 9% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 197M 97M 100M 50% /boot
tmpfs 98M 0 98M 0% /run/user/0
[root@tianqi-01 mnt]#

mount和umount命令

mount命令

mount命令,用于加载文件系统到指定的加载点

格式: mount /dev/sdb /mnt/ ,其中/dev/sdb就是那个磁盘,/mnt/表示挂载点

挂载点就是磁盘的入口,没有挂载点就无法在磁盘中进行读写

mount命令用法

man mount

然后 /defaults 去搜索

不加任何选项,包含defaults用法,默认包含以下权限

defaults

Use default option:rw, suid, dev, exec, auto, nouser, and

rw,表示可读写

suid,表示允许这个分区中的文件设置suid权限

dev 就不用管了

exec,表示是否可执行。若改为 no exec 那挂载下面的分区文件就没有可执行权限了

auto,表示自动挂载

nouser,表示是否允许普通用户去挂载(默认只能是root用户)

async,表示不会实时同步

mount -o remount,rw /dev/foo /dir 表示重新挂载

umount命令

umount命令,用于卸载已经加载的文件系统

格式: umount /dev/sdb

参数: -l 可以在当前目录下删除磁盘

umount -l /mnt/

配置文件/etc/fstab

/etc/fstab配置文件,系统在启动中,默认挂载的磁盘和分区都是在这个文件中去配置的

UUID=efee2b33-366d-4085-8082-eeddaef92da0 / xfs defaults 0 0
UUID=4cec7be4-42e4-42fb-9d0c-488bc752e0be /boot xfs defaults 0 0
UUID=56ea6aaa-80bc-438f-968a-3881a802c6eb swap swap defaults 0 0

第一列表示为,就是设备号,还可以写成/dev/sdb

第二列表示为,挂载点

第三列表示为,分区格式

第四列表示为,挂载选项(默认为defaults)

第五列表示为,是否备份,改为1就是备份,但基本没有用

第六列表示为,设置优先级,作用是是否检测。重新启动时,它会检测磁盘是否有错误,在设置成 1 和 2 就是去检测,1 的级别比 2 更高

[root@tianqi-01 mnt]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Jan 26 09:14:08 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=efee2b33-366d-4085-8082-eeddaef92da0 / xfs defaults 0 0
UUID=4cec7be4-42e4-42fb-9d0c-488bc752e0be /boot xfs defaults 0 0
UUID=56ea6aaa-80bc-438f-968a-3881a802c6eb swap swap defaults 0 0

#这里自己添加的
/dev/sdb /mnt/ xfs defaults 0 0

若想写 UUID,可以使用blkid命令

4.9 手动增加swap空间

安装系统时我们接触了swap命令,它类似于windows的虚拟内存,分区时一般指定虚拟内存的大小为实际内存的2倍。

这个swap空间和分区的时候不太一样,是我们手动再增加上去的。如果你的实际内存超过4GB,那么划分8GB给虚拟内存就足够日常交换了,因为多数公司用的ssd硬盘还是比较贵的。

如果真遇到了虚拟内存不够用的情况,就必须增加一个虚拟磁盘,因为我们不可以重新给磁盘分区。增加虚拟磁盘的基本思路是:建立swapfile文件——格式化为swap格式——启用该虚拟磁盘。

首先,在文件系统里,弄出一个模拟磁盘,1000M的小空间。

首先,建立swapfile,如下所示:

[root@centos7 /]# dd if=/dev/zero of=/tmp/newdisk bs=1M count=1024

1024+0 records in

1024+0 records out

1073741824 bytes (1.1 GB) copied, 44.6785 s, 24.0 MB/s

[root@centos7 /]#

[root@centos7 /]# du -sh /tmp/newdisk

1.0G /tmp/newdisk

[root@centos7 /]#

dd命令会经常用到,所以要掌握它的使用方法,它是用来操作磁盘的,可以读,可以写磁盘:用if指定源,也就是指定从哪里读(一般是写/dev/zero,它是UNIX系统特有的一个文件,它可以源源不断的提供“0”),of指定目标文件,就是0要写入哪里去,bs定义块的大小,count定义块的数量。bs和count这两个参数决定了目标文件的大小,即目标文件大小=bs*count。上面的示例中,用dd命令建立一个大小为1.0GB的文件,下面将它格式化为swap格式,命令如下:

[root@centos7 /]# mkswap -f /tmp/newdisk

Setting up swapspace version 1, size = 1048572 KiB

no label, UUID=6016280e-4ea8-40e5-a81e-e5ecc445cc74

[root@centos7 /]#

文件格式化后,就可以挂载了,示例如下:

[root@centos7 /]# free -m

total used free shared buff/cache available

Mem: 977 110 67 6 799 696

Swap: 1906 0 1906

[root@centos7 /]# swapon /tmp/newdisk

swapon: /tmp/newdisk: insecure permissions 0644, 0600 suggested.

#提示不安全,但是依然挂载上去了

[root@centos7 /]# free -m

total used free shared buff/cache available

Mem: 977 110 66 6 800 696

Swap: 2930 0 2930

[root@centos7 /]#

前后对比一下swap分区,我们发现多了1024M的空间。其中,free命令用来查看内存的使用情况,-m选项表示以MB为单位显示。

为了安全,更改以下权限。

[root@centos7 /]# chmod 0600 /tmp/newdisk

[root@centos7 /]#

如果不想要这个磁盘了,可以使用swapoff命令卸载掉。

[root@centos7 /]# swapoff /tmp/newdisk

[root@centos7 /]# free -m

total used free shared buff/cache available

Mem: 977 110 67 6 800 696

Swap: 1906 0 1906

[root@centos7 /]#

查看一下,swap分区又恢复到原来的空间大小了。

如果不想要,就删掉好了。

[root@centos7 /]# rm -rf /tmp/newdisk

友情链接:阿铭Linux
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux