Linux系统磁盘管理

Linux系统磁盘管理


磁盘分区,格式化,挂载

可以先添加几块硬盘,记住要先关闭虚拟机再添加,此处我添加了四块硬盘

此处是你当初创建虚拟机的时候选择的磁盘类型,此处也需要与其一致

选择创建新的硬盘

将其存储为单个文件,并选择大小


成功创建

fdisk分区

//生产分区建议: 如无特殊需求, 直接使用整个磁盘即可, 无需分区
//学习分区建议:1P+1E(3L)(个人比较推荐这种,划分出一个主分区,然后剩余的全部给扩展分区) 2P+1E(2L) 3P+1E(1L) (仅适用于练习)
//正常情况下我们是分一个主分区然后再分一个扩展分区将剩余的全部给扩展分区,然后再在扩展分区里面分逻辑分区
//这里面只有主分区和逻辑分区可以来进行格式化,扩展分区不行它只是用来占位置的,大小非常小

[root@lnh ~]# lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0  100G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   99G  0 part 
  ├─cs-root 253:0    0 65.2G  0 lvm  /
  ├─cs-swap 253:1    0    2G  0 lvm  [SWAP]
  └─cs-home 253:2    0 31.8G  0 lvm  /home
sdb           8:16   0    5G  0 disk 
sdc           8:32   0    5G  0 disk 
sdd           8:48   0    5G  0 disk 
sde           8:64   0    5G  0 disk 
sr0          11:0    1    9G  0 rom  
//查看分区
[root@lnh ~]# fdisk -l
Disk /dev/sdc: 5 GiB, 5368709120 bytes, 10485760 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 /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors //sda硬盘,一共100G,209715200个扇区,
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   //显示这个就是MBR格式的
Disk identifier: 0x7276f405

Device     Boot   Start       End   Sectors Size Id Type
/dev/sda1  *       2048   2099199   2097152   1G 83 Linux//*表示引导的系统盘
/dev/sda2       2099200 209715199 207616000  99G 8e Linux LVM


Disk /dev/sdd: 5 GiB, 5368709120 bytes, 10485760 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 /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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 /dev/sde: 5 GiB, 5368709120 bytes, 10485760 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 /dev/mapper/cs-root: 65.2 GiB, 69956796416 bytes, 136634368 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 /dev/mapper/cs-swap: 2 GiB, 2181038080 bytes, 4259840 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 /dev/mapper/cs-home: 31.8 GiB, 34158411776 bytes, 66715648 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 /dev/mapper/cs-root,Disk /dev/mapper/cs-swap,Disk /dev/mapper/cs-home是sda里面分的逻辑卷
//列出所有分区表
[root@lnh ~]# fdisk /dev/sdb 

Welcome to fdisk (util-linux 2.32.1).
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 0x35a0c29f.

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag //切换分区启动标记
   b   edit nested BSD disklabel //标签的修改
   c   toggle the dos compatibility flag 切换dos兼容模式的一个标记
 
  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
Command (m for help): n //创建分区
Partition type
   p   primary (0 primary, 0 extended, 4 free)//主分区(只能分四个)
   e   extended (container for logical partitions)//扩展分区
Select (default p):        //默认主分区
                        
Using default response p.
Partition number (1-4, default 1):   //默认第一个
First sector (2048-10485759, default 2048):    //默认这个最开始的位置
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759): +50M

Created a new partition 1 of type 'Linux' and of size 50 MiB.

Command (m for help): p //打印查看分区表
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: 0x35a0c29f

Device     Boot Start    End Sectors Size Id Type
/dev/sdb1        2048 104447  102400  50M 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): e      //扩展分区
Partition number (2-4, default 2):     //默认第二个
First sector (104448-10485759, default 104448):   //开始的位置
Last sector, +sectors or +size{K,M,G,T,P} (104448-10485759, default 10485759):   //结束的位置,默认结束就是将剩余的都给它了

Created a new partition 2 of type 'Extended' and of size 5 GiB.

Command (m for help): p    //打印分区表
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: 0x35a0c29f

Device     Boot  Start      End  Sectors Size Id Type
/dev/sdb1         2048   104447   102400  50M 83 Linux
/dev/sdb2       104448 10485759 10381312   5G  5 Extended
Command (m for help): n  创建分区
All space for primary partitions is in use.
Adding logical partition 5   //因为主分区是1-4,然后主分区选择了1,扩展分区选择2,3和4也没有了,所以现在没有位置进行选择了,只能默认选择逻辑分区
First sector (106496-10485759, default 106496): //开始的位置
Last sector, +sectors or +size{K,M,G,T,P} (106496-10485759, default 10485759): +200M

Created a new partition 5 of type 'Linux' and of size 200 MiB.

Command (m for help): p  //打印分区表
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: 0x41f56908

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sdb1         2048   104447   102400   50M 83 Linux
/dev/sdb2       104448 10485759 10381312    5G  5 Extended
/dev/sdb5       106496   516095   409600  200M 83 Linux
//sd2是扩展分区你现在看起来是5G,但是它是没有大小的
Command (m for help): w //保存
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[root@lnh ~]# partprobe 
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
//刷新分区表,使其生效
[root@lnh ~]# lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0  100G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   99G  0 part 
  ├─cs-root 253:0    0 65.2G  0 lvm  /
  ├─cs-swap 253:1    0    2G  0 lvm  [SWAP]
  └─cs-home 253:2    0 31.8G  0 lvm  /home
sdb           8:16   0    5G  0 disk 
├─sdb1        8:17   0   50M  0 part 
├─sdb2        8:18   0    1K  0 part 
└─sdb5        8:21   0  200M  0 part 
sdc           8:32   0    5G  0 disk 
sdd           8:48   0    5G  0 disk 
sde           8:64   0    5G  0 disk 
sr0          11:0    1    9G  0 rom  
//此时再查看,出现了刚刚的配置,然后sdb2是扩展分区几乎是没有大小的,只是占个位置,真正有大小的是主分区和逻辑分区

磁盘格式化

[root@lnh ~]# mkfs.
mkfs.cramfs  mkfs.ext3    mkfs.minix   
mkfs.ext2    mkfs.ext4    mkfs.xfs     
//TAB两下就是显示当前系统支持的格式化格式
[root@lnh ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 51200 1k blocks and 12824 inodes
Filesystem UUID: 1b8f98e1-75b2-483c-86cb-f40bae3c8c4c
Superblock backups stored on blocks: 
	8193, 24577, 40961

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
//用ext4格式的格式化之前的1分区

磁盘挂载

临时挂载:
[root@lnh ~]# lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0  100G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   99G  0 part 
  ├─cs-root 253:0    0 65.2G  0 lvm  /
  ├─cs-swap 253:1    0    2G  0 lvm  [SWAP]
  └─cs-home 253:2    0 31.8G  0 lvm  /home
sdb           8:16   0    5G  0 disk 
├─sdb1        8:17   0   50M  0 part 
├─sdb2        8:18   0    1K  0 part 
└─sdb5        8:21   0  200M  0 part 
sdc           8:32   0    5G  0 disk 
sdd           8:48   0    5G  0 disk 
sde           8:64   0    5G  0 disk 
sr0          11:0    1    9G  0 rom  
//先查看一下我们要挂载到哪,此处我选择的是sdb1
[root@lnh ~]# ls /media/  //准备挂载到这里
[root@lnh ~]# echo 'xbz' > /media/abc  //打印xbz到这个目录的文件下 
[root@lnh ~]# cat /media/abc    
xbz
[root@lnh ~]# mount /dev/sdb1 /media/     //挂载
[root@lnh ~]# df -hl     //查看挂载成功
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             956M     0  956M   0% /dev
tmpfs                975M     0  975M   0% /dev/shm
tmpfs                975M  8.7M  966M   1% /run
tmpfs                975M     0  975M   0% /sys/fs/cgroup
/dev/mapper/cs-root   66G  3.3G   62G   5% /
/dev/mapper/cs-home   32G  260M   32G   1% /home
/dev/sda1           1014M  178M  837M  18% /boot
tmpfs                195M     0  195M   0% /run/user/0
/dev/sdb1             45M  1.1M   40M   3% /media
[root@lnh ~]# ls /media/
lost+found
[root@lnh ~]# cd /media/
[root@lnh media]# ls -a
.  ..  lost+found
//可以看见挂载后原来的东西不见了,显示的是挂载后的内容.因为现在看见的这个地方不是之前那个位置,而是sdb1这个分区里面
[root@lnh media]# touch 333
[root@lnh media]# ls
333  lost+found
[root@lnh ~]# umount /dev/sdb1
[root@lnh ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             956M     0  956M   0% /dev
tmpfs                975M     0  975M   0% /dev/shm
tmpfs                975M  8.7M  966M   1% /run
tmpfs                975M     0  975M   0% /sys/fs/cgroup
/dev/mapper/cs-root   66G  3.3G   62G   5% /
/dev/mapper/cs-home   32G  260M   32G   1% /home
/dev/sda1           1014M  178M  837M  18% /boot
tmpfs                195M     0  195M   0% /run/user/0
[root@lnh ~]# ls /media/
abc
//可以看见出现了之前目录的
临时挂载2
[root@lnh ~]# blkid /dev/sdb1
/dev/sdb1: UUID="1b8f98e1-75b2-483c-86cb-f40bae3c8c4c" BLOCK_SIZE="1024" TYPE="ext4" PARTUUID="41f56908-01"
//查看其uuid用其进行挂载
[root@lnh ~]# mount UUID="1b8f98e1-75b2-483c-86cb-f40bae3c8c4c" /media/
[root@lnh ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             956M     0  956M   0% /dev
tmpfs                975M     0  975M   0% /dev/shm
tmpfs                975M  8.7M  966M   1% /run
tmpfs                975M     0  975M   0% /sys/fs/cgroup
/dev/mapper/cs-root   66G  3.3G   62G   5% /
/dev/mapper/cs-home   32G  260M   32G   1% /home
/dev/sda1           1014M  178M  837M  18% /boot
tmpfs                195M     0  195M   0% /run/user/0
/dev/sdb1             45M  1.1M   40M   3% /media
[root@lnh ~]# ls /media/
333  lost+found
永久挂载:
[root@lnh ~]# blkid /dev/sdb1
/dev/sdb1: UUID="1b8f98e1-75b2-483c-86cb-f40bae3c8c4c" BLOCK_SIZE="1024" TYPE="ext4" PARTUUID="41f56908-01"
[root@lnh ~]# vim /etc/fstab 

//分区标识(UUID或设备名) 挂载点 文件类型 挂载参数 不检查 不备份

//挂载的地方 挂载的系统格式 挂载的选项默认 不检查不备份(0 0)

[root@lnh ~]# mount -a   //读取一下
[root@lnh ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             956M     0  956M   0% /dev
tmpfs                975M     0  975M   0% /dev/shm
tmpfs                975M  8.8M  966M   1% /run
tmpfs                975M     0  975M   0% /sys/fs/cgroup
/dev/mapper/cs-root   66G  3.3G   62G   5% /
/dev/mapper/cs-home   32G  260M   32G   1% /home
/dev/sda1           1014M  178M  837M  18% /boot
tmpfs                195M     0  195M   0% /run/user/0
/dev/sdb1             45M  1.1M   40M   3% /media
//查看,此时重启一下也是挂载的,不过一定要小心,不能写错不然就会重启不出来

gdisk分区

[root@lnh ~]# dnf -y install gdisk
//一般是要先进行下载的
[root@lnh ~]# lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0  100G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   99G  0 part 
  ├─cs-root 253:0    0 65.2G  0 lvm  /
  ├─cs-swap 253:1    0    2G  0 lvm  [SWAP]
  └─cs-home 253:2    0 31.8G  0 lvm  /home
sdb           8:16   0    5G  0 disk 
├─sdb1        8:17   0   50M  0 part /media
├─sdb2        8:18   0    1K  0 part 
└─sdb5        8:21   0  200M  0 part 
sdc           8:32   0    5G  0 disk 
sdd           8:48   0    5G  0 disk 
sde           8:64   0    5G  0 disk 
sr0          11:0    1    9G  0 rom  
//查看一下,挂载到sdc
[root@lnh ~]# gdisk /dev/sdc
GPT fdisk (gdisk) version 1.0.3

Partition table scan:
  MBR: not present  //没提供
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.
Command (? for help): ?
b	back up GPT data to a file
c	change a partition's name
d	delete a partition //删除分区
i	show detailed information on a partition //列出分区表类型
l	list known partition types
n	add a new partition  //创建新分区
o	create a new empty GUID partition table (GPT)
p	print the partition table  //打印分区表
q	quit without saving changes //不保存退出
r	recovery and transformation options (experts only)
s	sort partitions
t	change a partition's type code  //改变分区表类型
v	verify disk
w	write table to disk and exit   保存并退出
x	extra functionality (experts only)
?	print this menu  //打印这个菜单
//查看帮助
Command (? for help): n  //创建分区
Partition number (1-128, default 1):    //可以创建128个主分区 
First sector (34-10485726, default = 2048) or {+-}size{KMGTP}://开始 
Last sector (2048-10485726, default = 10485726) or {+-}size{KMGTP}: +50M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): //默认格式
Changed type of partition to 'Linux filesystem'
Command (? for help): p
Disk /dev/sdc: 10485760 sectors, 5.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 5E84DBA9-4E92-41C8-9694-5B055771ED16
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 10485726
Partitions will be aligned on 2048-sector boundaries
Total free space is 10383293 sectors (5.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048          104447   50.0 MiB    8300  Linux filesystem
Command (? for help): w  //保存

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): Y   //确定
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.
[root@lnh ~]# partprobe  //使配置生效
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
[root@lnh ~]# lsblk  //查看分区表
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0  100G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   99G  0 part 
  ├─cs-root 253:0    0 65.2G  0 lvm  /
  ├─cs-swap 253:1    0    2G  0 lvm  [SWAP]
  └─cs-home 253:2    0 31.8G  0 lvm  /home
sdb           8:16   0    5G  0 disk 
├─sdb1        8:17   0   50M  0 part /media
├─sdb2        8:18   0  512B  0 part 
└─sdb5        8:21   0  200M  0 part 
sdc           8:32   0    5G  0 disk 
└─sdc1        8:33   0   50M  0 part 
sdd           8:48   0    5G  0 disk 
sde           8:64   0    5G  0 disk 
sr0          11:0    1    9G  0 rom  
[root@lnh ~]# fdisk -l   //查看格式
Disk /dev/sdc: 5 GiB, 5368709120 bytes, 10485760 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: gpt       //GPT格式
Disk identifier: 5E84DBA9-4E92-41C8-9694-5B055771ED16

Device     Start    End Sectors Size Type
/dev/sdc1   2048 104447  102400  50M Linux filesystem


Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 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    //MBR格式
Disk identifier: 0x7276f405

Device     Boot   Start       End   Sectors Size Id Type
/dev/sda1  *       2048   2099199   2097152   1G 83 Linux
/dev/sda2       2099200 209715199 207616000  99G 8e Linux LVM


Disk /dev/sdd: 5 GiB, 5368709120 bytes, 10485760 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 /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: 0x41f56908

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sdb1         2048   104447   102400   50M 83 Linux
/dev/sdb2       104448 10485759 10381312    5G  5 Extended
/dev/sdb5       106496   516095   409600  200M 83 Linux


Disk /dev/sde: 5 GiB, 5368709120 bytes, 10485760 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 /dev/mapper/cs-root: 65.2 GiB, 69956796416 bytes, 136634368 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 /dev/mapper/cs-swap: 2 GiB, 2181038080 bytes, 4259840 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 /dev/mapper/cs-home: 31.8 GiB, 34158411776 bytes, 66715648 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
//gdisk进行分区的是GPT格式,fdisk进行分区的是MRP格式

交换分区SWAP

交换分区SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用。

交换分区一般指定虚拟内存的大小为实际内存的1.5-2倍。如果实际内存超过8GB,可以直接划分16GB给虚拟内存即可,如果虚拟内存不够用的情况,须增加一个虚拟磁盘,由于不能给原有的磁盘重新分区,所以可以选择新建。

swap分区创建与使用

用文件的方式进行:
只是临时的
[root@lnh ~]# ls /opt/
[root@lnh ~]# dd if=/dev/zero of=/opt/swap bs=1024M count=1
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 11.9777 s, 89.6 MB/s
//if:指定源  一般写/dev/zero of:指定目标 bs:定义块大小
count:数量
[root@lnh ~]# ll /opt/
total 1048576
-rw-r--r--. 1 root root 1073741824 Jul 17 16:12 swap
//创建了一个1G的文件
[root@lnh ~]# mkswap -f /opt/swap   //格式化
mkswap: /opt/swap: insecure permissions 0644, 0600 suggested. //这是个不完全的权限644,请改成600
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=c06e75d2-7a3c-4b9c-a5ff-0790cba83ca9
[root@lnh ~]# chmod 0600 /opt/swap 
//改一下让其放心
[root@lnh ~]# swapon -f /opt/swap  //进行挂载
[root@lnh ~]# free -m  //查看
              total        used        free      shared  buff/cache   available
Mem:           1948         243        1103           8         602        1543
Swap:          3103           0        3103
[root@lnh ~]# swapoff /opt/swap  //取消挂载
[root@lnh ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1948         243        1103           8         602        1544
Swap:          2079           0        2079
//可以看见挂载上去时加了1024,取消后还原
永久挂载:
1.用系统里面自带的文件进行操作的
[root@lnh ~]# vim /etc/fstab


//分区标识(UUID或设备名) 挂载点 文件类型 挂载参数 不检查 不备份

此时你已经写入配置文件里面去了,必须要是下次启动的时候才会生效,现在生效不了
[root@lnh ~]# swapon -f /opt/swap
[root@lnh ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1948         244        1098           8         605        1543
Swap:          3103           0        3103
//可以用这个命令让其生效
2.可以用系统的空间进行操作
[root@lnh ~]# swapon -s
Filename				Type		Size	Used	Priority
/dev/dm-1                              	partition	2129916	0	-2
/opt/swap                              	file    	1048572	0	-3
//上面的是分区下面的是我们配置的文件交换分区
[root@lnh ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1948         244        1098           8         605        1543
Swap:          3103           0        3103
//可以查看是由哪些组成
[root@lnh ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n    //创建分区
All space for primary partitions is in use.
Adding logical partition 6        //默认
First sector (518144-10485759, default 518144):  //开始默认
Last sector, +sectors or +size{K,M,G,T,P} (518144-10485759, default 10485759): +1000M

Created a new partition 6 of type 'Linux' and of size 1000 MiB.

Command (m for help): p      //打印
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: 0x41f56908

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sdb1         2048   104447   102400   50M 83 Linux
/dev/sdb2       104448 10485759 10381312    5G  5 Extended
/dev/sdb5       106496   516095   409600  200M 83 Linux
/dev/sdb6       518144  2566143  2048000 1000M 83 Linux  //此处格式不对

Command (m for help): l   //查看有哪些格式

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE 
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT            

Command (m for help): t   //修改格式
Partition number (1,2,5,6, default 6): 
Hex code (type L to list all codes): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): p   //打印
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: 0x41f56908

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sdb1         2048   104447   102400   50M 83 Linux
/dev/sdb2       104448 10485759 10381312    5G  5 Extended
/dev/sdb5       106496   516095   409600  200M 83 Linux
/dev/sdb6       518144  2566143  2048000 1000M 82 Linux swap / Solaris
Command (m for help): w   //保存
The partition table has been altered.
Syncing disks.
[root@lnh ~]# partprobe 
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
//使修改生效
[root@lnh ~]# mkswap /dev/sdb6
Setting up swapspace version 1, size = 1000 MiB (1048571904 bytes)
no label, UUID=bca092ec-58f6-4fa1-8268-b8bfb13064ac
//格式化
[root@lnh ~]# blkid /dev/sdb6
/dev/sdb6: UUID="bca092ec-58f6-4fa1-8268-b8bfb13064ac" TYPE="swap" PARTUUID="41f56908-06"
//查看uuid 和格式
[root@lnh ~]# vim /etc/fstab 


//分区标识(UUID或设备名) 挂载点 文件类型 挂载参数 不检查 不备份

[root@lnh ~]# swapon -a  //使其生效
[root@lnh ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1948         245        1089           8         613        1541
Swap:          4103           0        4103
//查看
[root@lnh ~]# lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0  100G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   99G  0 part 
  ├─cs-root 253:0    0 65.2G  0 lvm  /
  ├─cs-swap 253:1    0    2G  0 lvm  [SWAP]
  └─cs-home 253:2    0 31.8G  0 lvm  /home
sdb           8:16   0    5G  0 disk 
├─sdb1        8:17   0   50M  0 part /media
├─sdb2        8:18   0  512B  0 part 
├─sdb5        8:21   0  200M  0 part 
└─sdb6        8:22   0 1000M  0 part [SWAP]
sdc           8:32   0    5G  0 disk 
└─sdc1        8:33   0   50M  0 part 
sdd           8:48   0    5G  0 disk 
sde           8:64   0    5G  0 disk 
sr0          11:0    1    9G  0 rom  
//可以看见其格式发生改变

磁盘故障案例

[root@lnh ~]# dd if=/dev/zero of=/opt/shan bs=1k count=1024
1024+0 records in
1024+0 records out
1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.001302 s, 805 MB/s
//生成一个1024k的磁盘
[root@lnh ~]# mkfs.ext4 -i 1024 /opt/shan  //格式化
mke2fs 1.45.6 (20-Mar-2020)

Filesystem too small for a journal
Discarding device blocks: done                            
Creating filesystem with 1024 1k blocks and 1024 inodes

Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done

[root@lnh ~]# mkdir /hai
[root@lnh ~]# mount -t ext4 -o loop /opt/shan /hai/
//挂载,因为是文件所以要加-o loop
[root@lnh ~]# cd /hai/
[root@lnh hai]# touch {1..20000}
...
touch: cannot touch '19994': No space left on device
touch: cannot touch '19995': No space left on device
touch: cannot touch '19996': No space left on device
touch: cannot touch '19997': No space left on device
touch: cannot touch '19998': No space left on device
touch: cannot touch '19999': No space left on device
touch: cannot touch '20000': No space left on device
//inode被占满
[root@lnh hai]# df -i|grep hai
/dev/loop2              1024  1024        0  100% /hai
[root@lnh hai]# df -h|grep hai
/dev/loop2           891K   42K  778K   6% /hai
inode被占满,剩余block也是无法继续使用,可以看见有空余的但是无法使用
[root@lnh ~]# dd if=/dev/zero of=/opt/shan bs=1024M count=2
2+0 records in
2+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 2.44033 s, 880 MB/s
[root@lnh ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             956M     0  956M   0% /dev
tmpfs                975M     0  975M   0% /dev/shm
tmpfs                975M  8.8M  966M   1% /run
tmpfs                975M     0  975M   0% /sys/fs/cgroup
/dev/mapper/cs-root   66G  8.2G   57G  13% /   
/dev/mapper/cs-home   32G  260M   32G   1% /home
/dev/sda1           1014M  178M  837M  18% /boot
tmpfs                195M     0  195M   0% /run/user/0
/dev/sdb1             45M  1.1M   40M   3% /media
/dev/loop1           891K   21K  799K   3% /xxb
/dev/loop2           891K   42K  778K   6% /hai
[root@lnh opt]# > /opt/shan
[root@lnh opt]# df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             956M     0  956M   0% /dev
tmpfs                975M     0  975M   0% /dev/shm
tmpfs                975M  8.8M  966M   1% /run
tmpfs                975M     0  975M   0% /sys/fs/cgroup
/dev/mapper/cs-root   66G  4.2G   61G   7% /    //可以看见减少了4G
/dev/mapper/cs-home   32G  260M   32G   1% /home
/dev/sda1           1014M  178M  837M  18% /boot
tmpfs                195M     0  195M   0% /run/user/0
/dev/sdb1             45M  1.1M   40M   3% /media
/dev/loop1           891K   21K  799K   3% /xxb
/dev/loop2           891K   42K  778K   6% /hai
//正确做法如下, 清空该文件即可释放文件内容 (> access.log)
[root@lnh ~]# dd if=/dev/zero of=/opt/xxx bs=1024M count=2
2+0 records in
2+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 2.44033 s, 880 MB/s
[root@lnh ~]#vim /opt/xxx   //打开这个文件


可以看见删除后空间没有得到释放

posted @ 2022-07-17 18:05  涂山小布  阅读(682)  评论(0编辑  收藏  举报