1.磁盘分区
# 1. 查看现有磁盘
# 请提前创建盘 vdb => qemu-img create -f qcow2 disk1.img 10G
# 创建虚拟机同时 创建disk1.img 在xml文件 也需添加disk1磁盘部分
# 添加到描述文件 xml
[root@stream9 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
vda 253:0 0 20G 0 disk
└─vda1 253:1 0 20G 0 part /
vdb 253:16 0 10G 0 disk
[root@stream9 ~]#
[root@stream9 ~]# blkid
/dev/vda1: UUID="c3506e82-5c52-4fd1-aeeb-df31262e5dfd" TYPE="xfs" PARTUUID="c409ea87-01"
# 2. 磁盘设备(块设备 block) 与 普通文件 => 两者在 linux 上呈现方式一样,都可以 ls 查看
[root@stream9 ~]# touch 1.txt
[root@stream9 ~]#
[root@stream9 ~]# ls /root/1.txt
/root/1.txt
[root@stream9 ~]#
[root@stream9 ~]# ls /dev/vdb
/dev/vdb
[root@stream9 ~]# ls -l /root/1.txt
-rw-r--r-- 1 root root 0 Oct 14 22:28 /root/1.txt
[root@stream9 ~]#
[root@stream9 ~]#
[root@stream9 ~]# ls -l /dev/vdb
brw-rw---- 1 root disk 253, 16 Oct 14 22:19 /dev/vdb
# 2. 把 vdb 分成vdb1,vdb2,vdb3 3个区
# n = new
# 0 - 2047
# 2048 - 20971519
[root@stream9 ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x645dc8a2.
Command (m for help): n # new 新增分区
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p # 查看 print
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519): 2G
Value out of range.
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519): +2G
Created a new partition 1 of type 'Linux' and of size 2 GiB.
Command (m for help): p
Disk /dev/vdb: 10 GiB, 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
Disklabel type: dos
Disk identifier: 0x645dc8a2
Device Boot Start End Sectors Size Id Type
/dev/vdb1 2048 4196351 4194304 2G 83 Linux
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2):
First sector (4196352-20971519, default 4196352):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (4196352-20971519, default 20971519): +3G
Created a new partition 2 of type 'Linux' and of size 3 GiB.
Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3):
First sector (10487808-20971519, default 10487808):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (10487808-20971519, default 20971519):
Created a new partition 3 of type 'Linux' and of size 5 GiB.
Command (m for help): p
Disk /dev/vdb: 10 GiB, 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
Disklabel type: dos
Disk identifier: 0x645dc8a2
Device Boot Start End Sectors Size Id Type
/dev/vdb1 2048 4196351 4194304 2G 83 Linux
/dev/vdb2 4196352 10487807 6291456 3G 83 Linux
/dev/vdb3 10487808 20971519 10483712 5G 83 Linux
Command (m for help): w # 保存分区
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
# 3. 查看分区
[root@stream9 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
vda 253:0 0 20G 0 disk
└─vda1 253:1 0 20G 0 part /
vdb 253:16 0 10G 0 disk
├─vdb1 253:17 0 2G 0 part
├─vdb2 253:18 0 3G 0 part
└─vdb3 253:19 0 5G 0 part
[root@stream9 ~]#
[root@stream9 ~]# blkid
/dev/vda1: UUID="c3506e82-5c52-4fd1-aeeb-df31262e5dfd" TYPE="xfs" PARTUUID="c409ea87-01"
/dev/vdb2: PARTUUID="645dc8a2-02"
/dev/vdb3: PARTUUID="645dc8a2-03"
/dev/vdb1: PARTUUID="645dc8a2-01"
# 4. 分区格式化
# 4.1 格式化vdb1 为 xfs 文件系统
[root@stream9 ~]# blkid
/dev/vda1: UUID="c3506e82-5c52-4fd1-aeeb-df31262e5dfd" TYPE="xfs" PARTUUID="c409ea87-01"
/dev/vdb2: PARTUUID="645dc8a2-02"
/dev/vdb3: PARTUUID="645dc8a2-03"
/dev/vdb1: PARTUUID="645dc8a2-01"
[root@stream9 ~]#
[root@stream9 ~]#
[root@stream9 ~]# mkfs.xfs /dev/vdb1
meta-data=/dev/vdb1 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=1 inobtcount=1 nrext64=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=16384, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@stream9 ~]#
[root@stream9 ~]#
[root@stream9 ~]# blkid
/dev/vda1: UUID="c3506e82-5c52-4fd1-aeeb-df31262e5dfd" TYPE="xfs" PARTUUID="c409ea87-01"
/dev/vdb2: PARTUUID="645dc8a2-02"
/dev/vdb3: PARTUUID="645dc8a2-03"
/dev/vdb1: UUID="6216710a-dd47-4a54-867d-5fba08e222c7" TYPE="xfs" PARTUUID="645dc8a2-01"
# 4.2 格式化vdb2 为 ext4 文件系统
# 格式化vdb3 为 ext3 文件系统
[root@stream9 ~]# mkfs.ext4 /dev/vdb2
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 786432 4k blocks and 196608 inodes
Filesystem UUID: d47cbad2-962f-427c-bc4c-59c59b5ad17a
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@stream9 ~]# mkfs.ext3 /dev/vdb3
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 1310464 4k blocks and 327680 inodes
Filesystem UUID: 9ad85542-430c-498c-aaf2-a9ecd15858e5
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
[root@stream9 ~]# blkid # 查看
/dev/vda1: UUID="c3506e82-5c52-4fd1-aeeb-df31262e5dfd" TYPE="xfs" PARTUUID="c409ea87-01"
/dev/vdb1: UUID="6216710a-dd47-4a54-867d-5fba08e222c7" TYPE="xfs" PARTUUID="645dc8a2-01"
/dev/vdb2: UUID="d47cbad2-962f-427c-bc4c-59c59b5ad17a" TYPE="ext4" PARTUUID="645dc8a2-02"
/dev/vdb3: UUID="9ad85542-430c-498c-aaf2-a9ecd15858e5" SEC_TYPE="ext2" TYPE="ext3" PARTUUID="645dc8a2-03"
# 5. 挂载使用
# 5.1 创建载体
[root@stream9 ~]# ls /
afs boot etc lib media opt root sbin sys usr
bin dev home lib64 mnt proc run srv tmp var
[root@stream9 ~]# mkdir /test1
[root@stream9 ~]# ls
1.txt
[root@stream9 ~]# cd /
[root@stream9 /]# ls
afs boot etc lib media opt root sbin sys tmp var
bin dev home lib64 mnt proc run srv test1 usr
[root@stream9 /]#
[root@stream9 /]#
[root@stream9 /]# cd mnt
[root@stream9 mnt]# ls
[root@stream9 mnt]#
[root@stream9 mnt]# mkdir test2
[root@stream9 mnt]# mkdir test3
[root@stream9 mnt]# ls
test2 test3
# 5.2 挂在磁盘到载体
[root@stream9 mnt]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
vda 253:0 0 20G 0 disk
└─vda1 253:1 0 20G 0 part /
vdb 253:16 0 10G 0 disk
├─vdb1 253:17 0 2G 0 part
├─vdb2 253:18 0 3G 0 part
└─vdb3 253:19 0 5G 0 part
[root@stream9 mnt]#
[root@stream9 mnt]# mount /dev/vdb1 /test1
[root@stream9 mnt]#
[root@stream9 mnt]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 889M 0 889M 0% /dev/shm
tmpfs 356M 456K 356M 1% /run
/dev/vda1 20G 2.8G 18G 14% /
tmpfs 178M 4.0K 178M 1% /run/user/0
/dev/vdb1 2.0G 47M 1.9G 3% /test1
[root@stream9 mnt]#
[root@stream9 mnt]# mount /dev/vdb2 /mnt/test2
[root@stream9 mnt]#
[root@stream9 mnt]# mount /dev/vdb3 /mnt/test3
[root@stream9 mnt]#
[root@stream9 mnt]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 889M 0 889M 0% /dev/shm
tmpfs 356M 456K 356M 1% /run
/dev/vda1 20G 2.8G 18G 14% /
tmpfs 178M 4.0K 178M 1% /run/user/0
/dev/vdb1 2.0G 47M 1.9G 3% /test1
/dev/vdb2 2.9G 24K 2.8G 1% /mnt/test2
/dev/vdb3 4.9G 92K 4.6G 1% /mnt/test3
# 拷贝文件进去
[root@stream9 ~]# cp -r /var/log /mnt/test3
[root@stream9 ~]# ls /mnt/test3/
3.txt log lost+found
[root@stream9 ~]# du -sh /mnt/test3
13M /mnt/test3
# 5.txt 还是在vda1上
[root@stream9 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 889M 0 889M 0% /dev/shm
tmpfs 356M 456K 356M 1% /run
/dev/vda1 20G 2.8G 18G 14% /
tmpfs 178M 4.0K 178M 1% /run/user/0
/dev/vdb1 2.0G 47M 1.9G 3% /test1
/dev/vdb2 2.9G 28K 2.8G 1% /mnt/test2
/dev/vdb3 4.9G 13M 4.6G 1% /mnt/test3
[root@stream9 ~]#
[root@stream9 ~]#
[root@stream9 ~]# echo 555 > /mnt/5.txt
[root@stream9 ~]# ls /mnt/ # 或者挂在 /mnt 下
5.txt test2 test3
[root@stream9 ~]# ls /dev/vdb* # 把块设置映射承文件夹
/dev/vdb /dev/vdb1 /dev/vdb2 /dev/vdb3
# 6. 卸载盘
[root@stream9 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 889M 0 889M 0% /dev/shm
tmpfs 356M 456K 356M 1% /run
/dev/vda1 20G 2.8G 18G 14% /
tmpfs 178M 4.0K 178M 1% /run/user/0
/dev/vdb1 2.0G 47M 1.9G 3% /test1
/dev/vdb2 2.9G 28K 2.8G 1% /mnt/test2
/dev/vdb3 4.9G 13M 4.6G 1% /mnt/test3
[root@stream9 ~]#
[root@stream9 ~]# umount /test1
[root@stream9 ~]# umount /mnt/test2
[root@stream9 ~]# umount /mnt/test3
[root@stream9 ~]#
[root@stream9 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 889M 0 889M 0% /dev/shm
tmpfs 356M 456K 356M 1% /run
/dev/vda1 20G 2.8G 18G 14% /
tmpfs 178M 4.0K 178M 1% /run/user/0
[root@stream9 ~]#
[root@stream9 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
vda 253:0 0 20G 0 disk
└─vda1 253:1 0 20G 0 part /
vdb 253:16 0 10G 0 disk
├─vdb1 253:17 0 2G 0 part
├─vdb2 253:18 0 3G 0 part
└─vdb3 253:19 0 5G 0 part
# 7. 删除分区
# 7.1 查看帮助
[root@stream9 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
vda 253:0 0 20G 0 disk
└─vda1 253:1 0 20G 0 part /
vdb 253:16 0 10G 0 disk
├─vdb1 253:17 0 2G 0 part
├─vdb2 253:18 0 3G 0 part
└─vdb3 253:19 0 5G 0 part
[root@stream9 ~]#
[root@stream9 ~]# blkid
/dev/vda1: UUID="c3506e82-5c52-4fd1-aeeb-df31262e5dfd" TYPE="xfs" PARTUUID="c409ea87-01"
/dev/vdb1: UUID="6216710a-dd47-4a54-867d-5fba08e222c7" TYPE="xfs" PARTUUID="645dc8a2-01"
/dev/vdb2: UUID="d47cbad2-962f-427c-bc4c-59c59b5ad17a" TYPE="ext4" PARTUUID="645dc8a2-02"
/dev/vdb3: UUID="9ad85542-430c-498c-aaf2-a9ecd15858e5" SEC_TYPE="ext2" TYPE="ext3" PARTUUID="645dc8a2-03"
[root@stream9 ~]#
[root@stream9 ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
# 7.2 删除 vdb2
[root@stream9 ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d
Partition number (1-3, default 3): 2
Partition 2 has been deleted.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[root@stream9 ~]#
[root@stream9 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
vda 253:0 0 20G 0 disk
└─vda1 253:1 0 20G 0 part /
vdb 253:16 0 10G 0 disk
├─vdb1 253:17 0 2G 0 part
└─vdb3 253:19 0 5G 0 part
[root@stream9 ~]# blkid
/dev/vda1: UUID="c3506e82-5c52-4fd1-aeeb-df31262e5dfd" TYPE="xfs" PARTUUID="c409ea87-01"
/dev/vdb1: UUID="6216710a-dd47-4a54-867d-5fba08e222c7" TYPE="xfs" PARTUUID="645dc8a2-01"
/dev/vdb3: UUID="9ad85542-430c-498c-aaf2-a9ecd15858e5" SEC_TYPE="ext2" TYPE="ext3" PARTUUID="645dc8a2-03"
# 7.3 删除剩下的磁盘
[root@stream9 ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d
Partition number (1,3, default 3):
Partition 3 has been deleted.
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[root@stream9 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
vda 253:0 0 20G 0 disk
└─vda1 253:1 0 20G 0 part /
vdb 253:16 0 10G 0 disk
[root@stream9 ~]# blkid
/dev/vda1: UUID="c3506e82-5c52-4fd1-aeeb-df31262e5dfd" TYPE="xfs" PARTUUID="c409ea87-01"
/dev/vdb: PTUUID="645dc8a2" PTTYPE="dos"
# 7.4 开机自动挂载的文件 => 为什么 vda1 会被挂载在 / 下
# /dev/vda1 20G 2.8G 18G 14% /
[root@stream9 ~]# cat /etc/fstab
UUID=c3506e82-5c52-4fd1-aeeb-df31262e5dfd / xfs defaults 0 0
[root@stream9 ~]#
[root@stream9 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
vda 253:0 0 20G 0 disk
└─vda1 253:1 0 20G 0 part /
vdb 253:16 0 10G 0 disk
[root@stream9 ~]# blkid
/dev/vda1: UUID="c3506e82-5c52-4fd1-aeeb-df31262e5dfd" TYPE="xfs" PARTUUID="c409ea87-01"
/dev/vdb: PTUUID="645dc8a2" PTTYPE="dos"
[root@stream9 ~]#
[root@stream9 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 889M 0 889M 0% /dev/shm
tmpfs 356M 444K 356M 1% /run
/dev/vda1 20G 2.8G 18G 14% /
tmpfs 178M 4.0K 178M 1% /run/user/0
# vim 编写 /etc/fstab
[root@stream9 ~]# cat /etc/fstab
# /dev/vda1 / xfs defaults 0 0 二选一
UUID=c3506e82-5c52-4fd1-aeeb-df31262e5dfd / xfs defaults 0 0
/dev/vdb7 /test1 xfs defaults 0 0
/dev/vdb2 /mnt/test2 xfs defaults 0 0
/dev/vdb3 /mnt/test3 xfs defaults 0 0
2. 判断是否存在
[root@kvm images]# ls
1.txt disk1.img stream9.qcow2 vm1.img
[root@kvm images]# [ -e ./1.txt ] && echo 1 || echo 0
1
[root@kvm images]# rm -rf 1.txt
[root@kvm images]# [ -e ./1.txt ] && echo 1 || echo 0
0
if [ -e /data ];then
exit #直接退出
fi
3.判断文件信息是否被篡改
[root@kvm ~]# vim 1.txt
[root@kvm ~]# cat 1.txt
111
[root@kvm ~]#
[root@kvm ~]# md5sum 1.txt # 生成效验码
1181c1834012245d785120e3505ed169 1.txt
[root@kvm ~]#
[root@kvm ~]# vim 1.txt
[root@kvm ~]#
[root@kvm ~]# md5sum 1.txt
09e6033204ac9778a4fdc3cc9684e6e2 1.txt # 效验码改变 代表 文件内容被篡改
[root@kvm ~]#
[root@kvm ~]# stat 1.txt 查看被改文件的详细信息
File: 1.txt
Size: 8 Blocks: 8 IO Block: 4096 regular file
Device: 10301h/66305d Inode: 33687113 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2023-10-15 00:10:12.965295608 +0800
Modify: 2023-10-15 00:10:08.748219750 +0800
Change: 2023-10-15 00:10:08.750219786 +0800
Birth: 2023-10-15 00:10:08.748219750 +0800
[root@kvm images]# stat /etc/fstab
File: /etc/fstab
Size: 427 Blocks: 8 IO Block: 4096 regular file
Device: 10301h/66305d Inode: 16777347 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2023-10-10 23:49:36.142933282 +0800
Modify: 2023-05-31 23:58:43.873427849 +0800
Change: 2023-06-01 00:16:59.421117118 +0800
Birth: 2023-05-31 23:58:43.873427849 +0800
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?