用fdisk进行分区、格式化、挂载实现最终的磁盘分区可用
1、建立分区
~]# 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): <<<==========这个是选择分区编号主分区是(1-4)
First sector (2048-2097151, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +100M<<=== 建一个100M的主分区
Partition 1 of type Linux and of size 100 MiB is set
最多四个主分区并且编号是1-4,无法创建扩展分区;1-3个主分区空间足够的话还可以创建扩展分区 ,逻辑分区建立在扩展分区之下。扩展分区的大小决定着逻辑分区
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 <<<<==========注在fdisk中用dos 在parted 中用gpt
Disk identifier: 0x0008d765
Device Boot Start End Blocks Id System
/dev/sdb1 2048 206847 102400 83 Linux 主分区
/dev/sdb2 206848 411647 102400 83 Linux 主分区
/dev/sdb3 411648 616447 102400 83 Linux 主分区
/dev/sdb4 616448 2097151 740352 5 Extended 扩展分区
/dev/sdb5 618496 720895 51200 83 Linux 扩展分区下逻辑分区
/dev/sdb6 722944 927743 102400 83 Linux 扩展分区下逻辑分区
Command (m for help): w <<<<========建好后w保存 q不保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
~]# ls -l /dev/sdb* <<<=====然后我们查看下我们建立的分区
brw-rw---- 1 root disk 8, 16 Mar 29 11:31 /dev/sdb
brw-rw---- 1 root disk 8, 17 Mar 29 11:31 /dev/sdb1
brw-rw---- 1 root disk 8, 18 Mar 29 11:31 /dev/sdb2
2、格式化分区(6中是ext4格式,7中是xfs文件系统)
~]# mkfs.xfs /dev/sdb2 #格式化xfs文件系统 (在cetos7中用的就是xfs格式的文件系统)
meta-data=/dev/sdb2 isize=512 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=25600, 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
~]# mkfs.xfs -f /dev/sdb2 此处是加-f 意思是强制覆盖
meta-data=/dev/sdb2 isize=512 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=25600, 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
~]# xfs_info /dev/sdb1 (这是查看centos7中的block大小)
meta-data=/dev/sda1 isize=512 agcount=4, agsize=16384 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=65536, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
~]# mkfs -t ext4 -b 4096 -I 512 /dev/sdb3 指定是sbd3的block大小和inode大小(适用于cetos6 因为c7不限制block和inode大小)
3、挂载分区文件系统
下面是临时挂载
~]# mount -t xfs /dev/sdb2 /opt 《=========挂载文件系统到/opt下
You have new mail in /var/spool/mail/root
~]# df -h|grep opt 《《=======查看是否挂载成功
/dev/sdb2 97M 5.3M 92M 6% /opt
~]# touch /opt/oldgirl 《《======测试在opt是否可以建文件
~]# ls /opt 《《==有了就代表挂载成功
Oldgirl
这是实现开机自动挂载的命令
~]# vim/etc/fstab <<<======我们进入/etc/fstab编辑分区信息 要按照格式进行编写
cat /etc/fstab
这是我们的开机自启内的编写格式
UUID=3a3a295f-88f8-456d-94dc-1a3eeb517c02 / xfs defaults 0 0
UUID=fd2e0ca7-32be-425f-86a2-85c02b9ec5ea /boot xfs defaults 0 0
UUID=79a3924b-739e-48dc-ab0c-0444b9ac6591 swap swap defaults 0 0
/dev/sdb2 /opt xfs defaults 0 0
设备 挂载点 文件系统类型 默认挂载选项 是否备份 是否开机磁盘检查 0是否 1是是
写入后保存退出 重启虚拟机看是否成功挂载
~]# tail -1 /etc/fstab 开机后查看下
/dev/sdb2 /opt xfs defaults 0 0
~]# blkid 然后我们查看下block信息
/dev/sda3: UUID="632eef00-4111-40d3-a31e-8f2d5eba6669" TYPE="xfs"
/dev/sda1: UUID="8b675bd3-fce1-45fa-89dc-510238a1b217" TYPE="xfs"
/dev/sda2: UUID="c84b67fe-c30f-4857-997b-867a22d79f6c" TYPE="swap"
/dev/sdb1: UUID="495d599a-5665-411f-8012-f06e17525d60" TYPE="ext4"
/dev/sdb2: UUID="baf24e32-c7eb-4313-a8a2-ac6f394926a0" TYPE="xfs"
/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.7G 17G 10% /
devtmpfs 900M 0 900M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.6M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sdb2 97M 5.3M 92M 6% /opt 我们的挂载分区
/dev/sda1 197M 116M 82M 59% /boot
tmpfs 182M 0 182M 0% /run/user/0
/mnt]# umount /mnt 卸载/mnt挂载点
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@oldboyedu /mnt]# pwd
/mnt
You have new mail in /var/spool/mail/root
[root@oldboyedu /mnt]# umount -lf /mnt #<==强制卸载
[root@oldboyedu /mnt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.9G 18G 10% /
devtmpfs 980M 5.0M 975M 1% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 18M 973M 2% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 253M 136M 118M 54% /boot
tmpfs 199M 0 199M 0% /run/user/0
tmpfs 199M 0 199M 0% /run/user/1000
~]# mount /dev/sdb2 /opt #没有格式化没法挂载
mount: /dev/sdb2 is write-protected, mounting read-only
mount: unknown filesystem type '(null)'
4、磁盘的检查和修复

[root@oldboyedu ~]# fsck
fsck fsck.btrfs fsck.cramfs fsck.ext2 fsck.ext3 fsck.ext4 fsck.minix fsck.xfs
[root@oldboyedu ~]# fsck
fsck 磁盘检查和修复
1、正常的磁盘不能操作。
2、卸载挂载点在操作
[root@oldboyedu ~]# fsck -a /dev/sdb1
fsck from util-linux 2.23.2
/dev/sdb1: clean, 12/25688 files, 8896/102400 blocks
[root@oldboyedu ~]# mount /dev/sdb1 /mnt
[root@oldboyedu ~]# fsck -a /dev/sdb1
fsck from util-linux 2.23.2
/dev/sdb1 is mounted.
e2fsck: Cannot continue, aborting.
[root@oldboyedu ~]# e2fsck /dev/sdb1
e2fsck 1.42.9 (28-Dec-2013)
/dev/sdb1: clean, 12/25688 files, 8896/102400 blocks
[root@oldboyedu ~]# fsck -a /dev/sdb1
fsck from util-linux 2.23.2
/dev/sdb1: clean, 12/25688 files, 8896/102400 blocks
[root@oldboyedu ~]# xfs_repair /dev/sdb2
xfs_repair: /dev/sdb2 contains a mounted filesystem
xfs_repair: /dev/sdb2 contains a mounted and writable filesystem
fatal error -- couldn't initialize XFS library
[root@oldboyedu ~]# umount /opt
[root@oldboyedu ~]# xfs_repair /dev/sdb2
Phase 1 - find and verify superblock...
Phase 2 - using internal log
- zero log...
- scan filesystem freespace and inode maps...
- found root inode chunk
Phase 3 - for each AG...
- scan and clear agi unlinked lists...
- process known inodes and perform inode discovery...
- agno = 0
- agno = 1
- agno = 2
- agno = 3
- process newly discovered inodes...
Phase 4 - check for duplicate blocks...
- setting up duplicate extent list...
- check for inodes claiming duplicate blocks...
- agno = 0
- agno = 1
- agno = 2
- agno = 3
Phase 5 - rebuild AG headers and trees...
- reset superblock...
Phase 6 - check inode connectivity...
- resetting contents of realtime bitmap and summary inodes
- traversing filesystem ...
- traversal finished ...
- moving disconnected inodes to lost+found ...
Phase 7 - verify and correct link counts...
done
5、swap被占用原因
文件删除原理
no space left on device.
swap作用,内存不够时候,用来充当内存,一般内存1.5倍。大于8G给8G
将来JAVA服务,内存泄漏。。。
swap就会占用。。。操作系统性能下降
[root@oldboyedu ~]# free -m
total used free shared buff/cache available
Mem: 1980 143 1689 9 146 1671
Swap: 767 0 767
增加swap分区 100M
二、用parted创建分区、格式化、挂载和实现最终的应用
1、创建分区
]# parted /dev/sdb <<<======进入分区控制界面进行交互分区
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
(parted) mklabel gpt <<===========修改分区格式为gpt
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to
continue?
Yes/No? Yes
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt <==修改后结果
(parted) mkpart primary 0 480 <<======创建第一个主分区480M。
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore #忽略提示。
(parted) mkpart primary 481 580 <<====创建第二个主分区100M。
Warning: You requested a partition from 481MB to 580MB (sectors 939453..1132812).
The closest location we can manage is 481MB to 481MB (sectors 940031..940031).
Is this still acceptable to you?
Yes/No? Y
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I #忽略提示。
(parted) mkpart logic 581 600 <<=====创建一个20M逻辑分区,逻辑分区是logic
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 17.4kB 480MB 480MB primary
3 481MB 481MB 512B primary
2 481MB 580MB 98.6MB primary
4 581MB 600MB 18.9MB logic
(parted) rm 4 <<==rm是删除,后面跟我们要删除的分区
(parted) p
后面的格式化文件和挂载到开机自启中和上面类似,这里就不再一一详解了。
文件系统:
1、什么是文件系统?
计算机存储和组织数据的方法或者机制。
2、为什么需要文件系统?
磁盘、物理介质、磁粒子物理元素。硬件是需要软件驱动使用,硬盘需要文件系统驱动。
文件系统实现通过磁盘管理规划、存取数据
3、文件系统种有哪些种类:
Windows: NTFS 、fat32、msdos
Linux: ext2 、ext3(C5)、 ext4(C6)、xfs (C7)、btrfs
4、创建文件系统实践
5、文件系统原理(ext文件系统)
1、企业如何选择文件系统?
7、企业里如何优化文件系统?