vm动态(不关机)添加硬盘

准备

新添加的硬盘

关键命令记录

#读取新磁盘 host2是第2块盘 host3是第3块盘  以此类推
echo "- - -" > /sys/class/scsi_host/host2/scan

#命令执行完毕后,可以通过fdisk -l 查看
fdisk -l
#对添加的盘分区 n--p--回车--回车--w
fdisk /dev/sdb

#创建PV
pvcreate /dev/sdb1
#扩展VG vgextend 可以tab一下
vgextend cl /dev/sdb1 

#扩展LV lvextend 可以tab一下
lvextend -L +100G /dev/cl/root 

#扩展FS,在执行前要观察下 df -hT 的数据文件格式(我的/dev/mapper/cl-root是xfs 所以用xfs_growfs扩容)
# 是 ext3,、ext4、xfs, 文件格式不同,扩展命令是有差异的
# resize2fs -p /dev/mapper/vgoradata-lvoradata01
xfs_growfs /dev/mapper/cl-root

#检查 /etc/fstab 是否自动挂载

命令执行记录

[root@localhost ~]# fdisk -l

Disk /dev/sda: 85.9 GB, 85899345920 bytes, 167772160 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: dos
Disk identifier: 0x00064a33

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   167772159    82836480   8e  Linux LVM

Disk /dev/mapper/cl-root: 51.3 GB, 51308920832 bytes, 100212736 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/cl-swap: 8455 MB, 8455716864 bytes, 16515072 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/cl-home: 25.1 GB, 25052577792 bytes, 48930816 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

[root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host2/scan 
[root@localhost ~]# fdisk -l

Disk /dev/sda: 85.9 GB, 85899345920 bytes, 167772160 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: dos
Disk identifier: 0x00064a33

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   167772159    82836480   8e  Linux LVM

Disk /dev/mapper/cl-root: 51.3 GB, 51308920832 bytes, 100212736 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/cl-swap: 8455 MB, 8455716864 bytes, 16515072 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/cl-home: 25.1 GB, 25052577792 bytes, 48930816 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: 107.4 GB, 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

[root@localhost ~]# 
[root@localhost ~]#  fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

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
Building a new DOS disklabel with disk identifier 0xc75e3e5b.

Command (m for help): 
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-209715199, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): 
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# 
[root@localhost ~]# vgextend cl /dev/sdb1 
  Volume group "cl" successfully extended
[root@localhost ~]# 
[root@localhost ~]# lvextend -L +100G /dev/cl/root 
  Size of logical volume cl/root changed from <47.79 GiB (12233 extents) to <147.79 GiB (37833 extents).
  Logical volume cl/root successfully resized.
[root@localhost ~]# xfs_growfs /dev/mapper/cl-root 
meta-data=/dev/mapper/cl-root    isize=512    agcount=4, agsize=3131648 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=12526592, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6116, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 12526592 to 38740992
[root@localhost ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/cl-root  148G   47G  102G  32% /
devtmpfs             7.8G     0  7.8G   0% /dev
tmpfs                7.8G   88K  7.8G   1% /dev/shm
tmpfs                7.8G  570M  7.3G   8% /run
tmpfs                7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/sda1           1014M  173M  842M  18% /boot
/dev/mapper/cl-home   24G  270M   24G   2% /home
overlay              148G   47G  102G  32% /var/lib/docker/overlay2/ba8a99b9e033fbaa80e0845eca3ab045ed30381319b368be767ad398bcd201a2/merged
overlay              148G   47G  102G  32% /var/lib/docker/overlay2/3d5f7ac73c2a76e7f2222a5731e05c83d73ada1b6e00ba94f02bc59238fd4f55/merged
overlay              148G   47G  102G  32% /var/lib/docker/overlay2/74dd4d799ce42fe0830267f55646f039565b9e05f6ac66baa1458106aaa5c46c/merged
tmpfs                1.6G   16K  1.6G   1% /run/user/0
overlay              148G   47G  102G  32% /var/lib/docker/overlay2/a72e965edc3eab19d1895e8c9f13ffba9bb18d0c134443d66f10d24d55f8b856/merged
overlay              148G   47G  102G  32% /var/lib/docker/overlay2/3b6fdb59bb758e09fb2859c058e0da6dd8ea8e3e1df2b17dd0a23ea60ebc7319/merged
overlay              148G   47G  102G  32% /var/lib/docker/overlay2/eaaace97808b81579a3d491d17386f87a875655d63b11fdd15c76bee4695fb67/merged
overlay              148G   47G  102G  32% /var/lib/docker/overlay2/f84e8f3234ded65a6c16fb5fdd55de300b1a45f1f20d769fc156e056bcfa57f8/merged
overlay              148G   47G  102G  32% /var/lib/docker/overlay2/0adca765f0e7f569e0d852966bfae922422024dde04b122e4d47c5b873e9d063/merged
[root@localhost ~]# 


参考博客

vmware 中linux虚拟机动态添加硬盘:https://blog.csdn.net/ctypyb2002/article/details/51241907
Linux resize2fs: Bad magic number in super-block错误的解决方法:https://blog.csdn.net/csd753111111/article/details/100428614

posted @ 2020-09-29 10:11  鸣昊  阅读(359)  评论(0编辑  收藏  举报