主頁 > 作業系統 > Linux系統磁盤管理

Linux系統磁盤管理

2022-07-18 06:27:21 作業系統

Linux系統磁盤管理


目錄
  • Linux系統磁盤管理
    • 磁盤磁區,格式化,掛載
      • fdisk磁區
      • 磁盤格式化
      • 磁盤掛載
      • gdisk磁區
    • 交換磁區SWAP
      • swap磁區創建與使用
    • 磁盤故障案例

磁盤磁區,格式化,掛載

可以先添加幾塊硬碟,記住要先關閉虛擬機再添加,此處我添加了四塊硬碟

此處是你當初創建虛擬機的時候選擇的磁盤型別,此處也需要與其一致

選擇創建新的硬碟

將其存盤為單個檔案,并選擇大小


成功創建

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   //打開這個檔案


可以看見洗掉后空間沒有得到釋放

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/499501.html

標籤:其他

上一篇:Color Wheel for Mac (數字色輪)

下一篇:沁恒CH32V103C8T6(二): Linux RISC-V編譯和燒錄環境配置

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • CA和證書

    1、在 CentOS7 中使用 gpg 創建 RSA 非對稱密鑰對 gpg --gen-key #Centos上生成公鑰/密鑰對(存放在家目錄.gnupg/) 2、將 CentOS7 匯出的公鑰,拷貝到 CentOS8 中,在 CentOS8 中使用 CentOS7 的公鑰加密一個檔案 gpg -a ......

    uj5u.com 2020-09-10 00:09:53 more
  • Kubernetes K8S之資源控制器Job和CronJob詳解

    Kubernetes的資源控制器Job和CronJob詳解與示例 ......

    uj5u.com 2020-09-10 00:10:45 more
  • VMware下安裝CentOS

    VMware下安裝CentOS 一、軟硬體準備 1 Centos鏡像準備 1.1 CentOS鏡像下載地址 下載地址 1.2 CentOS鏡像下載程序 點擊下載地址進入如下圖的網站,選擇需要下載的版本,這里選擇的是Centos8,點擊如圖所示。 決定選擇Centos8后,選擇想要的鏡像源進行下載,此 ......

    uj5u.com 2020-09-10 00:12:10 more
  • 如何使用Grep命令查找多個字串

    如何使用Grep 命令查找多個字串 大家好,我是良許! 今天向大家介紹一個非常有用的技巧,那就是使用 grep 命令查找多個字串。 簡單介紹一下,grep 命令可以理解為是一個功能強大的命令列工具,可以用它在一個或多個輸入檔案中搜索與正則運算式相匹配的文本,然后再將每個匹配的文本用標準輸出的格式 ......

    uj5u.com 2020-09-10 00:12:28 more
  • git配置http代理

    git配置http代理 經常遇到克隆 github 慢的問題,這里記錄一下幾種配置 git 代理的方法,解決 clone github 過慢。 目錄 git配置代理 git單獨配置github代理 git配置全域代理 配置終端環境變數 git配置代理 主要使用 git config 命令 git單獨 ......

    uj5u.com 2020-09-10 00:12:33 more
  • Linux npm install 裝包時提示Error EACCES permission denied解

    npm install 裝包時提示Error EACCES permission denied解決辦法 ......

    uj5u.com 2020-09-10 00:12:53 more
  • Centos 7下安裝nginx,使用yum install nginx,提示沒有可用的軟體包

    Centos 7下安裝nginx,使用yum install nginx,提示沒有可用的軟體包。 18 (flaskApi) [root@67 flaskDemo]# yum -y install nginx 19 已加載插件:fastestmirror, langpacks 20 Loading ......

    uj5u.com 2020-09-10 00:13:13 more
  • Linux查看服務器暴力破解ssh IP

    在公網的服務器上經常遇到別人爆破你服務器的22埠,用來挖礦或者干其他嘿嘿嘿的事情~ 這種情況下正確的做法是: 修改默認ssh的22埠 使用設定密鑰登錄或者白名單ip登錄 建議服務器密碼為復雜密碼 創建普通用戶登錄服務器(root權限過大) 建立堡壘機,實作統一管理服務器 統計爆破IP [root ......

    uj5u.com 2020-09-10 00:13:17 more
  • CentOS 7系統常見快捷鍵操作方式

    Linux系統中一些常見的快捷方式,可有效提高操作效率,在某些時刻也能避免操作失誤帶來的問題。 ......

    uj5u.com 2020-09-10 00:13:31 more
  • CentOS 7作業系統目錄結構介紹

    作業系統存在著大量的資料檔案資訊,相應檔案資訊會存在于系統相應目錄中,為了更好的管理資料資訊,會將系統進行一些目錄規劃,不同目錄存放不同的資源。 ......

    uj5u.com 2020-09-10 00:13:35 more
最新发布
  • vim的常用命令

    Vim的6種基本模式 1. 普通模式在普通模式中,用的編輯器命令,比如移動游標,洗掉文本等等。這也是Vim啟動后的默認模式。這正好和許多新用戶期待的操作方式相反(大多數編輯器默認模式為插入模式)。 2. 插入模式在這個模式中,大多數按鍵都會向文本緩沖中插入文本。大多數新用戶希望文本編輯器編輯程序中一 ......

    uj5u.com 2023-04-20 08:43:21 more
  • vim的常用命令

    Vim的6種基本模式 1. 普通模式在普通模式中,用的編輯器命令,比如移動游標,洗掉文本等等。這也是Vim啟動后的默認模式。這正好和許多新用戶期待的操作方式相反(大多數編輯器默認模式為插入模式)。 2. 插入模式在這個模式中,大多數按鍵都會向文本緩沖中插入文本。大多數新用戶希望文本編輯器編輯程序中一 ......

    uj5u.com 2023-04-20 08:42:36 more
  • docker學習

    ###Docker概述 真實專案部署環境可能非常復雜,傳統發布專案一個只需要一個jar包,運行環境需要單獨部署。而通過Docker可將jar包和相關環境(如jdk,redis,Hadoop...)等打包到docker鏡像里,將鏡像發布到Docker倉庫,部署時下載發布的鏡像,直接運行發布的鏡像即可。 ......

    uj5u.com 2023-04-19 09:26:53 more
  • 設定Windows主機的瀏覽器為wls2的默認瀏覽器

    這里以Chrome為例。 1. 準備作業 wsl是可以使用Windows主機上安裝的exe程式,出于安全考慮,默認情況下改功能是無法使用。要使用的話,終端需要以管理員權限啟動。 我這里以Windows Terminal為例,介紹如何默認使用管理員權限打開終端,具體操作如下圖所示: 2. 操作 wsl ......

    uj5u.com 2023-04-19 09:25:49 more
  • docker學習

    ###Docker概述 真實專案部署環境可能非常復雜,傳統發布專案一個只需要一個jar包,運行環境需要單獨部署。而通過Docker可將jar包和相關環境(如jdk,redis,Hadoop...)等打包到docker鏡像里,將鏡像發布到Docker倉庫,部署時下載發布的鏡像,直接運行發布的鏡像即可。 ......

    uj5u.com 2023-04-19 09:19:04 more
  • Linux學習筆記

    IP地址和主機名 IP地址 ifconfig可以用來查詢本機的IP地址,如果不能使用,可以通過install net-tools安裝。 Centos系統下ens33表示主網卡;inet后表示IP地址;lo表示本地回環網卡; 127.0.0.1表示代指本機;0.0.0.0可以用于代指本機,同時在放行設 ......

    uj5u.com 2023-04-18 06:52:01 more
  • 解決linux系統的kdump服務無法啟動的問題

    問題:專案麒麟系統服務器的kdump服務無法啟動,沒有相關日志無法定位問題。 1、查看服務狀態是關閉的,重啟系統也無法啟動 systemctl status kdump 2、修改grub引數,修改“crashkernel”為“512M(有的機器數值太大太小都會導致報錯,建議從128M開始試,或者加個 ......

    uj5u.com 2023-04-12 09:59:50 more
  • 解決linux系統的kdump服務無法啟動的問題

    問題:專案麒麟系統服務器的kdump服務無法啟動,沒有相關日志無法定位問題。 1、查看服務狀態是關閉的,重啟系統也無法啟動 systemctl status kdump 2、修改grub引數,修改“crashkernel”為“512M(有的機器數值太大太小都會導致報錯,建議從128M開始試,或者加個 ......

    uj5u.com 2023-04-12 09:59:01 more
  • 你是不是暴露了?

    作者:袁首京 原創文章,轉載時請保留此宣告,并給出原文連接。 如果您是計算機相關從業人員,那么應該經歷不止一次網路安全專項檢查了,你肯定是收到過資訊系統技術檢測報告,要求你加強風險監測,確保你提供的系統服務堅實可靠了。 沒檢測到問題還好,檢測到問題的話,有些處理起來還是挺麻煩的,尤其是線上正在運行的 ......

    uj5u.com 2023-04-05 16:52:56 more
  • 細節拉滿,80 張圖帶你一步一步推演 slab 記憶體池的設計與實作

    1. 前文回顧 在之前的幾篇記憶體管理系列文章中,筆者帶大家從宏觀角度完整地梳理了一遍 Linux 記憶體分配的整個鏈路,本文的主題依然是記憶體分配,這一次我們會從微觀的角度來探秘一下 Linux 內核中用于零散小記憶體塊分配的記憶體池 —— slab 分配器。 在本小節中,筆者還是按照以往的風格先帶大家簡單 ......

    uj5u.com 2023-04-05 16:44:11 more