Bootstrap

关于linux重启后磁盘分区消失的情况复现与修复

先说结论: 重启后分区消失可能是由于自己在分区操作时已经使用parted命令将分区删除,但是并未重启导致磁盘状态没有更新,实际上分区已经被删除了,重启后磁盘状态更新

实际上在删除分区的时候会有一个报错提示会让你重启之后再进行其他操作

Error: Partition(s) 1 on /dev/sdb have been written, but we have been unable to inform the kernel of the change, probably
because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making
further changes. 

错误:已写入/dev/sdb上的分区1,但我们可能无法将更改通知内核。
因为它/他们正在使用中。因此,旧分区将继续使用。您应该现在重新启动,然后再创建。
进一步的变化。

虚拟机增加一块新盘sdb

[root@k8s-node2 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk 
├─sda1   8:1    0  300M  0 part /boot
├─sda2   8:2    0    2G  0 part 
└─sda3   8:3    0 17.7G  0 part /
sdb      8:16   0   10G  0 disk 
sr0     11:0    1  4.4G  0 rom  

进行parted分区并格式化

[root@k8s-node2 ~]# parted /dev/sdb 
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to
continue?
Yes/No? Yes                                                               
(parted) mkpart 
Partition name?  []?                                                      
File system type?  [ext2]? xfs                                            
Start? 0                                                                  
End? -1                                                                   
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore                                                     
(parted) q                                                                
Information: You may need to update /etc/fstab.


[root@k8s-node2 ~]# mkfs.xfs /dev/sdb1 
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=655298 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2621191, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

创建lvm

[root@k8s-node2 ~]# pvcreate /dev/sdb1
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created

[root@k8s-node2 ~]# vgcreate datavg  /dev/sdb1
  Volume group "datavg" successfully created
[root@k8s-node2 ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  datavg   1   0   0 wz--n- <10.00g <10.00g
  
[root@k8s-node2 ~]# lvcreate -n datavg_test -l 100%Free datavg 
  Logical volume "datavg_test" created.
[root@k8s-node2 ~]# lvs
  LV          VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  datavg_test datavg -wi-a----- <10.00g 
  
  [root@k8s-node2 ~]# mkfs.xfs /dev/mapper/datavg-datavg_test 
meta-data=/dev/mapper/datavg-datavg_test isize=512    agcount=4, agsize=655104 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2620416, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

挂载

[root@k8s-node2 ~]# cat /etc/fstab 
 ...
 ...
/dev/mapper/datavg-datavg_test /vdata   xfs defaults 0 0

[root@k8s-node2 ~]# mount -a

# 随便创建几个内容
[root@k8s-node2 ~]# cd /vdata/
[root@k8s-node2 vdata]# ls
[root@k8s-node2 vdata]# mkdir 123
[root@k8s-node2 vdata]# mkdir 12331a
[root@k8s-node2 vdata]# mkdir 12asdklz
[root@k8s-node2 vdata]# echo 1 > 1.txt

重启查看内容还在

[root@k8s-node2 ~]# ls /vdata/
123  12331a  12asdklz  1.txt
[root@k8s-node2 ~]# cat /vdata/1.txt 
1
模拟现象
# 删除分区
[root@k8s-node2 ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      17.4kB  10.7GB  10.7GB

(parted) rm 1                                                             
Error: Partition(s) 1 on /dev/sdb have been written, but we have been unable to inform the kernel of the change, probably
because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making
further changes.
Ignore/Cancel? Ignore
(parted) q                                                                
Information: You may need to update /etc/fstab.

# 删除成功后,不重启查看内容还在
[root@k8s-node2 ~]# lsblk                                                 
NAME                   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                      8:0    0   20G  0 disk 
├─sda1                   8:1    0  300M  0 part /boot
├─sda2                   8:2    0    2G  0 part 
└─sda3                   8:3    0 17.7G  0 part /
sdb                      8:16   0   10G  0 disk 
└─sdb1                   8:17   0   10G  0 part 
  └─datavg-datavg_test 253:0    0   10G  0 lvm  /vdata
sr0                     11:0    1  4.4G  0 rom  
[root@k8s-node2 ~]# ll /vdata/
total 4
drwxr-xr-x 2 root root 6 Sep  7 00:59 123
drwxr-xr-x 2 root root 6 Sep  7 00:59 12331a
drwxr-xr-x 2 root root 6 Sep  7 00:59 12asdklz
-rw-r--r-- 1 root root 2 Sep  7 00:59 1.txt
[root@k8s-node2 ~]# cat /vdata/1.txt 
1

# 删除后创建个新文件,成功
[root@k8s-node2 ~]# echo 2 > /vdata/2.txt
[root@k8s-node2 ~]# cat /vdata/2.txt 
2

# 重启
[root@k8s-node2 ~]# reboot



启动报错,忘记取消fstab内的挂载内容

(img-ICx1J3Vd-1663218562600)(https://secure2.wostatic.cn/static/22Ez5oBu7sYcGxgKMFSiSY/image.png)]

输入密码可进系统

(img-F7wHO4rB-1663218562601)(https://secure2.wostatic.cn/static/ct8gvrFTR19RfypQAukpWr/image.png)]

注释掉内容重启

在这里插入图片描述

# 磁盘已经”掉盘“
[root@k8s-node2 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk 
├─sda1   8:1    0  300M  0 part /boot
├─sda2   8:2    0    2G  0 part 
└─sda3   8:3    0 17.7G  0 part /
sdb      8:16   0   10G  0 disk 
sr0     11:0    1  4.4G  0 rom  
[root@k8s-node2 ~]# ls /vdata/

[root@k8s-node2 ~]# fdisk -l /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: gpt
Disk identifier: 77D78615-E5F9-4B27-92D6-FE1D7EDB0965


#         Start          End    Size  Type            Name
[root@k8s-node2 ~]#

恢复

重新进行parted分区,挂载,不要格式化,

[root@k8s-node2 ~]# parted /dev/sdb 
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mk                                                               
mklabel  mkpart   mktable  
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

(parted) mkpart 
Partition name?  []?                                                      
File system type?  [ext2]? xfs                                            
Start? 0                                                                  
End? -1                                                                   
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore                                                     
(parted) q                                                                
Information: You may need to update /etc/fstab.



[root@k8s-node2 ~]# lsblk                                                 
NAME                   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                      8:0    0   20G  0 disk 
├─sda1                   8:1    0  300M  0 part /boot
├─sda2                   8:2    0    2G  0 part 
└─sda3                   8:3    0 17.7G  0 part /
sdb                      8:16   0   10G  0 disk 
└─sdb1                   8:17   0   10G  0 part 
  └─datavg-datavg_test 253:0    0   10G  0 lvm  
sr0                     11:0    1  4.4G  0 rom  
[root@k8s-node2 ~]# vi /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Aug 13 20:44:35 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=483dabee-b08d-4a32-9f8e-d806fded0843 /                       xfs     defaults        0 0
UUID=47041f57-0ec7-4422-ad9c-944171226382 /boot                   xfs     defaults        0 0
#UUID=f72d39e6-a02d-4c2d-aa61-9b068661d738 swap                    swap    defaults        0 0
/dev/mapper/datavg-datavg_test /vdata   xfs defaults 0 0
~                                                                                                                            
                                                                                                                      
"/etc/fstab" 12L, 557C written
[root@k8s-node2 ~]# mount -a

# 内容还在,被格式化之后创建的文件也在
[root@k8s-node2 ~]# ls /vdata/
123  12331a  12asdklz  1.txt  2.txt
[root@k8s-node2 ~]# cat /vdata/2.txt 
2


;