天天看點

Linux系統磁盤管理

磁盤分區,格式化,挂載

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

Linux系統磁盤管理

此處是你當初建立虛拟機的時候選擇的磁盤類型,此處也需要與其一緻

Linux系統磁盤管理

選擇建立新的硬碟

Linux系統磁盤管理

将其存儲為單個檔案,并選擇大小

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或裝置名) 挂載點 檔案類型 挂載參數 不檢查 不備份

Linux系統磁盤管理

//挂載的地方 挂載的系統格式 挂載的選項預設 不檢查不備份(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
           
Linux系統磁盤管理

//分區辨別(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 
           
Linux系統磁盤管理

//分區辨別(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   //打開這個檔案
           
Linux系統磁盤管理

可以看見删除後空間沒有得到釋放

繼續閱讀