4.5/4.6 磁盘格式化
Linux 支持的文件系统格式
[root@wangshuang-01 ~]# cat /etc/filesystems xfs //centos7默认的文件系统 ext4 ext3 ext2 nodev proc nodev devpts iso9660 vfat hfs hfsplus *
查看分区的文件系统是什么格式的
mount 命令
先创建分区:
[root@wangshuang-01 ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0xfbfb8b39 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-20971519,默认为 2048):+3G
Last 扇区, +扇区 or +size{K,M,G} (6291456-20971519,默认为 20971519):
将使用默认值 20971519
分区 1 已设置为 Linux 类型,大小设为 7 GiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@wangshuang-01 ~]# fdisk -l
磁盘 /dev/sda:32.2 GB, 32212254720 字节,62914560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00000b89
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 4605951 2097152 82 Linux swap / Solaris
/dev/sda3 4605952 62914559 29154304 83 Linux
磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xfbfb8b39
设备 Boot Start End Blocks Id System
/dev/sdb1 6291456 20971519 7340032 83 Linux
再格式化:
mke2
fs -t xfs //指定要格式化成什么格式的系统
mke2
fs -t xfs -b 2048 /dev/sdb1 //指定块大小
du -sh 与 du -lh、du -sb 查看文件的区别
[root@wangshuang-01 ~]# du -sh a.txt 0 a.txt [root@wangshuang-01 ~]# echo 1 > a.txt [root@wangshuang-01 ~]# du -sh a.txt //显示占用的块的大小 4.0K a.txt [root@wangshuang-01 ~]# ls -lh a.txt //显示实际的大小 -rw-r--r--. 1 root root 2 6月 17 09:39 a.txt [root@wangshuang-01 ~]# du -sb a.txt //显示实际的大小 2 a.txt
格式化成ext4 格式的系统命令
mkfs.ext4 == mke2fs -t ext4 (不支持格式化 xfk格式的,需要配置?)
[root@wangshuang-01 ~]# mke2fs -t ext4 /dev/sdb1 mke2fs 1.42.9 (28-Dec-2013) 文件系统标签= OS type: Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 458752 inodes, 1835008 blocks 91750 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=1879048192 56 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 Allocating group tables: 完成 正在写入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成
分区格式化后,还可以再格式化:再使用 mkfs.ext4 /dev/sdb1 格式化一下
[root@wangshuang-01 ~]# mkfs.ext4 /dev/sdb1 mke2fs 1.42.9 (28-Dec-2013) 文件系统标签= OS type: Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 458752 inodes, 1835008 blocks 91750 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=1879048192 56 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 Allocating group tables: 完成 正在写入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成
使用mke2fs 格式化 xfs 格式的文件
[root@wangshuang-01 ~]# mke2fs -t xfs /dev/sdb1 mke2fs 1.42.9 (28-Dec-2013) Your mke2fs.conf file does not define the xfs filesystem type. Aborting... [root@wangshuang-01 ~]# 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@wangshuang-01 ~]# mkfs.xfs -f /dev/sdb1 //加 -f 强制执行 meta-data=/dev/sdb1 isize=512 agcount=4, agsize=458752 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=1835008, 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
使用mount 命令,只能查看已经挂载的文件,看不到刚格式化的文件系统,因为没有挂载。
使用 blkid /dev/sdb1 可以查看没有挂载的文件
[root@wangshuang-01 ~]# blkid /dev/sdb1
/dev/sdb1: UUID="80efcb06-64e2-45cf-8f93-2a2e84ca3297" TYPE="xfs"
指定块占大空间的时候弹出提示
[root@wangshuang-01 ~]# 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) 无论如何也要继续? (y,n) n //提示是否继续 [root@wangshuang-01 ~]# mke2fs -b 2048 /dev/sdb1 mke2fs 1.42.9 (28-Dec-2013) 文件系统标签= OS type: Linux 块大小=2048 (log=1) 分块大小=2048 (log=1) Stride=0 blocks, Stripe width=0 blocks 458752 inodes, 3670016 blocks 183500 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=541065216 224 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 Allocating group tables: 完成 正在写入inode表: 完成 Writing superblocks and filesystem accounting information: 完成
mke2fs -m 0.1 /dev/sdb1 指定预留的空间设置
[root@wangshuang-01 ~]# mke2fs -m 0.1 /dev/sdb1 mke2fs 1.42.9 (28-Dec-2013) 文件系统标签= OS type: Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 458752 inodes, 1835008 blocks 1835 blocks (0.10%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=1879048192 56 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 Allocating group tables: 完成 正在写入inode表: 完成 Writing superblocks and filesystem accounting information: 完成
不指定文件格式,默认是ext2,
[root@wangshuang-01 ~]# blkid /dev/sdb1 /dev/sdb1: UUID="2ac3bebf-1154-455a-b2c9-1254289d79de" TYPE="ext2" [root@wangshuang-01 ~]# mke2fs -t ext4 -m 0.1 /dev/sdb1 mke2fs 1.42.9 (28-Dec-2013) 文件系统标签= OS type: Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 458752 inodes, 1835008 blocks 1835 blocks (0.10%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=1879048192 56 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 Allocating group tables: 完成 正在写入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 [root@wangshuang-01 ~]# blkid /dev/sdb1 /dev/sdb1: UUID="2935b7a2-4956-46b0-b7b3-952f59cf564f" TYPE="ext4"
查看inode、block比例及数量
[root@wangshuang-01 ~]# mke2fs -t ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
458752 inodes, 1835008 blocks //inode和块的比例是1:4 块16 k
91750 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1879048192
56 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
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
mke2fs -i 8192 -t ext4 /dev/sdb1 :多少字节对应一个inode ,更改inode、block比例
mke2fs -i 8192 -t ext4 /dev/sdb1 //更改块 8k
[root@wangshuang-01 ~]# mke2fs -i 8192 -t ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
917504 inodes, 1835008 blocks //1:2
91750 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1879048192
56 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
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
4.7/4.8 磁盘挂载
无论是否 分区 都可以格式化的,只有格式化后才能挂载文件系统,
linux 要想访问磁盘必须挂载一个挂载点下面,实际是一个目录
不分区,直接格式化
[root@wangshuang-01 ~]# mkfs.xfs /dev/sdb mkfs.xfs: /dev/sdb appears to contain a partition table (dos). mkfs.xfs: Use the -f option to force overwrite. [root@wangshuang-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@wangshuang-01 ~]# blkid /dev/sdb /dev/sdb: UUID="43efda10-cd7f-4981-af08-ff1808501c1a" TYPE="xfs"
mount /dev/sdb /mnt 把/dev/sdb磁盘挂载在 /mnt 上
[root@wangshuang-01 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 989M 27G 4% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 97M 100M 50% /boot
tmpfs 99M 0 99M 0% /run/user/0
[root@wangshuang-01 ~]# mount /dev/sdb /mnt
[root@wangshuang-01 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 989M 27G 4% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 97M 100M 50% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/sdb 10G 33M 10G 1% /mnt
[root@wangshuang-01 ~]# 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=494220k,nr_inodes=123555,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/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)
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/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
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/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
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=29,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=11892)
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)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=100816k,mode=700)
/dev/sdb on /mnt type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
卸载 分区
[root@wangshuang-01 ~]# cd /mnt [root@wangshuang-01 mnt]# ls [root@wangshuang-01 mnt]# touch 1.txt 2.txt [root@wangshuang-01 mnt]# mkdir 234 [root@wangshuang-01 mnt]# ls 1.txt 234 2.txt [root@wangshuang-01 mnt]# umount /dev/sdb umount: /mnt:目标忙。 (有些情况下通过 lsof(8) 或 fuser(1) 可以 找到有关使用该设备的进程的有用信息) [root@wangshuang-01 /]# cd //退出该目录 [root@wangshuang-01 ~]# umount /dev/sdb [root@wangshuang-01 ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda3 28G 989M 27G 4% / devtmpfs 483M 0 483M 0% /dev tmpfs 493M 0 493M 0% /dev/shm tmpfs 493M 6.8M 486M 2% /run tmpfs 493M 0 493M 0% /sys/fs/cgroup /dev/sda1 197M 97M 100M 50% /boot tmpfs 99M 0 99M 0% /run/user/0
umount -l /mnt 无需退出挂载目录,直接卸载分区
[root@wangshuang-01 ~]# cd /mnt
[root@wangshuang-01 mnt]# umount -l /mnt
[root@wangshuang-01 mnt]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 989M 27G 4% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 97M 100M 50% /boot
tmpfs 99M 0 99M 0% /run/user/0
man mount
mount 命令 defaults 默认的选项:
Use default options: rw(可读可写), suid(允许设置), dev, exec(是否可执行), auto(自动挂载), nouser(非普通用户挂载), and async(不实时同步内存数据).
mount -o remount 重新挂载
vi /etc/fstab 配置文件,系统启动默认挂载哪些磁盘,分区,在该文件下配置
第一列:设备号,uuid 使用blkid 可查看uuid 也可以写 挂载文件 + 挂载点
第二列:分区格式
第三列:挂载选项
第四列:备份
第五列:检测的优先级 挂载到根下 置1,其他目录置2 ,置0 不检测
4.9 手动增加swap空间
//模拟磁盘100M dd:操作磁盘、if:指定从哪里读、of:指定写到哪里,bs:指定每个块的大小、count:数量
[root@wangshuang-01 ~]# dd if=/dev/zero of=/tmp/newdisk bs=1M count=100 记录了100+0 的读入 记录了100+0 的写出 104857600字节(105 MB)已复制,4.83536 秒,21.7 MB/秒 [root@wangshuang-01 ~]# du -sh /tmp/newdisk //查看磁盘多大 100M /tmp/newdisk [root@wangshuang-01 ~]# mkswap -f /tmp/newdisk //格式化 正在设置交换空间版本 1,大小 = 102396 KiB 无标签,UUID=94ae9962-2bb1-47cd-8f56-bb459d2163e2 [root@wangshuang-01 ~]# free -m total used free shared buff/cache available Mem: 984 121 598 6 263 687 Swap: 2047 0 2047 [root@wangshuang-01 ~]# swapon /tmp/newdisk //加到swap上 swapon: /tmp/newdisk:不安全的权限 0644,建议使用 0600。 [root@wangshuang-01 ~]# free -m total used free shared buff/cache available Mem: 984 121 599 6 263 687 Swap: 2147 0 2147 [root@wangshuang-01 ~]# chmod 0600 /tmp/newdisk //更改权限 [root@wangshuang-01 ~]# swapoff /tmp/newdisk //卸载磁盘 [root@wangshuang-01 ~]# free -m total used free shared buff/cache available Mem: 984 121 599 6 263 687 Swap: 2047 0 2047 [root@wangshuang-01 ~]# rm -f /tmp/newdisk //删掉磁盘