4.5/4.6磁盘格式化
有了分区不格式化磁盘是不能使用的,Windows下支持NTFS/FAT16/FAT32等文件系统,那么linux下支持的文件系统格式可以从配置文件/etc/filesystems中查看到。
[root@localhost ~]# cat /etc/filesystems
xfs
ext4
ext3
ext2
nodev proc
nodev devpts
iso9660
vfat
hfs
hfsplus
*
xfs是centos7下默认的文件系统,/和/boot都是xfs,使用mount命令可以查看各个分区挂载的文件系统类型。centos6之前使用的都是ext的文件系统。
[root@localhost ~]# mount | grep /dev
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=1927024k,nr_inodes=481756,mode=755)
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)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
/dev/sda3 on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
下面我们使用xfs格式来格式化新的分区,涉及到的命令是mke2fs。
使用-t选项指定文件系统的类型,-b选项指定最小的块大小,
这里简单说一下磁盘的块大小,一个文件保存在磁盘中,它的占用空间一定是块大小的整数倍。块分的越大读写起来越快,碎片化程度越小,而造成的空间浪费也就越多,因此块大小的指定要合适。
使用命令格式化
[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...
我们发现在mke2fs.conf里并不包括xfs的文件系统。
在这里可以使用mkfs.xfs命令。
那么对于在mke2fs.conf有的文件系统下面两条命令是等价的。
mkfs.ext4 /dev/sdb1
mke2fs -t ext4 /dev/sdb1
如果提示你已经格式化过了,可以使用-f选项强制格式化
[root@localhost ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=32000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=128000, 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
那么现在使用blkid命令查看一下磁盘的格式。
[root@localhost ~]# blkid /dev/sdb1
/dev/sdb1: UUID="39b14f7b-75f7-4bd0-bc20-1afc83c7f9de" TYPE="xfs"
mke2fs还有-m选项,设置预留给root用户的空间(百分比),根据磁盘大小调整。但是mkfs.xfx不支持这个选项。
[root@localhost ~]# mke2fs -m 1.5 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
128016 inodes, 512000 blocks
7680 blocks (1.50%) reserved for the super user ####预留空间
First data block=1
Maximum filesystem blocks=67633152
63 block groups
8192 blocks per group, 8192 fragments per group
2032 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
此时,没有-t选项默认ext2文件格式
[root@localhost ~]# blkid /dev/sdb1
/dev/sdb1: UUID="1be0052a-b0fb-4c63-809e-46616fedccd8" TYPE="ext2"
-i选项指定多少数量的字节对应一个inode(最小是一个块对应一个inode)
[root@localhost ~]# mke2fs -i 8192 -t ext4 -b 4096 /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
64000 inodes, 128000 blocks
6400 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=132120576
4 block groups
32768 blocks per group, 32768 fragments per group
16000 inodes per group
Superblock backups stored on blocks:
32768, 98304
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
4.7/4.8磁盘挂载
之前提过,一个磁盘可以不分区直接格式化使用,对于扩展用的磁盘,我们常常这么做。
首先我们把之前的分区删掉,直接格式化。
[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): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x7e73ed68
Device Boot Start End Blocks Id System
/dev/sdb1 2048 1026047 512000 83 Linux
Command (m for help): d
Selected partition 1
Partition 1 is deleted
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x7e73ed68
Device Boot Start End Blocks Id System
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# mkfs.xfs -f /dev/sdb
meta-data=/dev/sdb isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=262144, 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
linux下访问磁盘必须要挂载。挂载前必须要格式化。挂载使用mount命令。
[root@localhost ~]#
[root@localhost ~]# blkid /dev/sdb
/dev/sdb: UUID="9ef1bd7e-6b27-4f59-ba25-768bdc899aca" TYPE="xfs"
[root@localhost ~]# mount /dev/sdb /mnt/
[root@localhost ~]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 16G 1.1G 15G 7% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 197M 113M 84M 58% /boot
tmpfs 379M 0 379M 0% /run/user/0
/dev/sdb 1014M 33M 982M 4% /mnt
常用的挂载点就是/mnt/目录,此时是空的。可以创建几个文档和目录。
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ls
[root@localhost mnt]# touch 1.txt 2.txt
[root@localhost mnt]# mkdir 234
[root@localhost mnt]# ls
1.txt 234 2.txt
现在我要卸载掉,使用umount命令,如果你正在使用的话,是不可以卸载的。
[root@localhost 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@localhost mnt]# cd
[root@localhost ~]# umount /dev/sdb
[root@localhost ~]# echo $?
0
当然也可以使用-l选项(--lazy),当所有用户不再访问这个目录的时候就会直接卸载掉。
[root@localhost ~]# !mou
mount /dev/sdb /mnt/
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# umount -l /mnt/
[root@localhost mnt]# pwd
/mnt
[root@localhost mnt]# ls
1.txt 234 2.txt
[root@localhost mnt]# cd
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ls
[root@localhost mnt]#
-o选项是一个很常用的选项,我们man一下看一下。
defaults
Use default options: rw, suid, dev, exec, auto, nouser, and async.
defaults就是你不使用-o选项时候的缺省值,默认是可读可写,可以被赋予suid权限,挂载为块设备或者字符设备,有执行权限,自动挂载,普通用户无法挂载,不会时时同步到磁盘。
还有一个很常用的就是remount(重新挂载,常常结合ro,rw使用)。
在配置文件/etc/fstab里显示了系统重启之后自动挂载的磁盘,
#
# /etc/fstab
# Created by anaconda on Mon Jun 11 15:59:42 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=0d54d876-7911-4ecc-844b-3ba4ae9f1cc6 / xfs defaults 0 0
UUID=ba6e1621-feb7-40ed-b305-664cef06ed89 /boot xfs defaults 0 0
UUID=b4f711da-bb19-498a-b18c-662860f00cde swap swap defaults 0 0
第一列是设备号,可以写UUID 也可以写文件系统名;第二列是挂载目录;第三列是文件系统格式;第四列设置为缺省即可(就是mount的-o选项);第五列是是否备份(fs_dump);第六列是优先级(fs_pass)。
fs_dump - 该选项被"dump"命令使用来检查一个文件系统应该以多快频率进行转储,若不需要转储就设置该字段为0.文件系统频率(fs_freq),被dump程序使用来确定哪个文件系统需要dump,如果最后一个字段没有设置,系统将认为其值为0,而dump程序则认为此文件系统无需dump。
fs_pass- 该字段被fsck命令用来决定在启动时需要被扫描的文件系统的顺序,根文件系统"/"对应该字段的值应该为1,其他文件系统应该为2。若该文件系统无需在启动时扫描则设置该字段为0.被fsck程序所使用来确定进行在系统重启进行文件系统检查时的顺序,对于根系统/这个值应设为1,其它文件系统可以设为2,在同一个物理硬盘内的文件系统应该被顺序检测,而不同硬盘中的文件系统则应该同时检测以充分利用系统的并行性。如果最后一个字段值为0或没有设置,fsck程序装跳过此文件系统的检测。在linux编程中可以用getmntent过程来访问这个文件的内容。
UUID是唯一标识码
[root@localhost ~]# blkid
/dev/sda1: UUID="ba6e1621-feb7-40ed-b305-664cef06ed89" TYPE="xfs"
/dev/sda2: UUID="b4f711da-bb19-498a-b18c-662860f00cde" TYPE="swap"
/dev/sda3: UUID="0d54d876-7911-4ecc-844b-3ba4ae9f1cc6" TYPE="xfs"
/dev/sr0: UUID="2017-09-06-10-51-00-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sdb: UUID="9ef1bd7e-6b27-4f59-ba25-768bdc899aca" TYPE="xfs"
挂载的时候也可以用UUID挂载。
[root@localhost ~]# ls /mnt/
[root@localhost ~]# mount UUID="9ef1bd7e-6b27-4f59-ba25-768bdc899aca" /mnt/
[root@localhost ~]# ls /mnt/
1.txt 234 2.txt
4.9手动增加swap空间
首先要生成一个要增添一个虚拟磁盘,可以使用dd命令,dd命令是对磁盘操作的命令,可以读写。
if是来源,/dev/zero是linux系统自带的造零器。of是目的文件。bs是块大小,count是个数。
[root@localhost ~]# dd if=/dev/zero of=/tmp/newdisk bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.296321 s, 354 MB/s
swap是一个特有的文件系统,我们需要对newdisk格式化,使用mkswap命令。
[root@localhost ~]# mkswap /tmp/newdisk
Setting up swapspace version 1, size = 102396 KiB
no label, UUID=81c861ae-29bc-423c-a1bf-f66f4057e95e
再使用swapon命令将其添加到现有的swap空间。
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 3783 140 3365 8 276 3384
Swap: 4095 0 4095
[root@localhost ~]# swap
swaplabel swapoff swapon
[root@localhost ~]# swapon /tmp/newdisk
swapon: /tmp/newdisk: insecure permissions 0644, 0600 suggested.
[root@localhost ~]# chmod 0600 /tmp/newdisk
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 3783 141 3365 8 276 3384
Swap: 4195 0 4195
如果不想要了,卸载就是用swapoff命令。
[root@localhost ~]# swapoff /tmp/newdisk