Centos7 adds a hard disk online without restarting the system
CentOS 7 can add new disks online without restarting. The existing environment is basically offline servers and online virtual machines. Almost all of them support hot swapping and hot expansion, so it is particularly important to add new disks online, so that current services can be interrupted without interrupting. Or the process can also add a hard disk to it.
1. Add a hard drive:
Add a hard disk to the virtual machine while it is online.
Add 2 20G scsi disks here
The above are the complete fool-proof steps for adding a virtual machine disk.
Click OK after adding the two disks.
2. Enter the terminal to refresh
Switch back to the terminal to see if there are any newly added disks
From this it can be seen that there is no such thing.
[root@compute ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 160G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 158.9G 0 part ├─centos-root 253:0 0 103G 0 lvm / ├─centos-swap 253:1 0 5.9G 0 lvm [SWAP] └─centos-home 253:2 0 50G 0 lvm /home sr0 11:0 1 4.2G 0 rom [root@compute ~]#
Now refresh it using the following command:
ls /sys/class/scsi_host/ echo '- - -' >/sys/class/scsi_host/host0/scan echo '- - -' >/sys/class/scsi_host/host1/scan echo '- - -' >/sys/class/scsi_host/host2/scan
Refresh and check the disk again to find that the disk has been refreshed.
[root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 150G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 149G 0 part ├─centos-root 253:0 0 50G 0 lvm / ├─centos-swap 253:1 0 7.9G 0 lvm [SWAP] └─centos-home 253:2 0 91.1G 0 lvm /home sdb 8:16 0 60G 0 disk sr0 11:0 1 4.2G 0 rom
You will find that the added refresh is successful.
Note: The disk size in the picture and the disk size displayed by the command are inconsistent, but the operation does not affect
Centos7 adds hard drive online without restarting the system Success! ! !
Next, expand the root directory of the added disk.
[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 0xf94e704a. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) eextended Select (default p): Using default response p Partition number (1-4, default 1): First sector (2048-125829119, default 2048): Using default value 2048 Last sector, + sectors or + size{<!-- -->K,M,G} (2048-125829119, default 125829119): Using default value 125829119 Partition 1 of type Linux and of size 60 GiB is set Command (m for help): Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
Check whether the disk is created successfully. You will find that there is an additional sdb1 under sdb, which means success.
[root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 150G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 149G 0 part ├─centos-root 253:0 0 50G 0 lvm / ├─centos-swap 253:1 0 7.9G 0 lvm [SWAP] └─centos-home 253:2 0 91.1G 0 lvm /home sdb 8:16 0 60G 0 disk └─sdb1 8:17 0 60G 0 part sr0 11:0 1 4.2G 0 rom
Create pv volume
[root@localhost ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created.
Create extended vg volume
[root@localhost ~]# vgdisplay ---Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 1 Metadata Sequence No. 4 VG Access read/write VG Status resizable MAXLV 0 Cur LV 3 OpenLV3 Max PV 0 Cur PV 1 Act PV 1 VG Size <149.00 GiB PE Size 4.00 MiB Total PE 38143 AllocPE/Size 38142/148.99 GiB Free PE/Size 1/4.00 MiB VG UUID u2kTsr-4zBE-RIYe-gBgv-bC4W-Zfp0-Qp0r5o
[root@localhost ~]# vgextend centos /dev/sdb1 Volume group "centos" successfully extended
Expand LV volume: (Use lvdisplay to view the path of the LV volume and confirm which LV volume you want to expand)
[root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/centos/root LV Name root VG Name centos LV UUID E3AGUF-VK8W-j8CF-qPec-WyjO-uexw-g2wDvr LV Write Access read/write LV Creation host, time localhost, 2022-10-01 00:29:41 + 0800 LV Status available # open 1 LV Size 50.00 GiB Current LE 12800 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0 --- Logical volume --- LV Path /dev/centos/home LV Name home VG Name centos LV UUID sYivRD-1Uda-Qt8H-PoHf-zeS2-6Z7h-jP6VQY LV Write Access read/write LV Creation host, time localhost, 2022-10-01 00:29:41 + 0800 LV Status available # open 1 LV Size <91.12 GiB Current LE 23326 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 --- Logical volume --- LV Path /dev/centos/swap LV Name swap VG Name centos LV UUID 0HqPth-SCIV-iySF-VnOk-p0jA-jbm6-M9o6US LV Write Access read/write LV Creation host, time localhost, 2022-10-01 00:29:42 + 0800 LV Status available # open 2 LV Size <7.88 GiB Current LE 2016 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1
[root@localhost ~]# lvextend -L + 59G /dev/centos/root Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 109.00 GiB (27904 extents). Logical volume centos/root successfully resized.
After modification, you will find that it has not taken effect.
[root@localhost ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/centos-root xfs 50G 19G 32G 38% / devtmpfs devtmpfs 3.8G 0 3.8G 0% /dev tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs tmpfs 3.9G 12M 3.8G 1% /run tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/sda1 xfs 1014M 142M 873M 14% /boot /dev/mapper/centos-home xfs 92G 33M 92G 1% /home tmpfs tmpfs 781M 0 781M 0% /run/user/0
refresh it
centos6:
[root@localhost ~]# resize2fs /dev/mapper/centos-root resize2fs 1.42.9 (28-Dec-2013) resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-root Couldn't find valid filesystem superblock.
centos7:
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3276800 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=13107200, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=6400, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 13107200 to 28573696
Finally, if you check again, you will find that the centos-root directory has been expanded from 50G to 109G.
[root@localhost ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/centos-root xfs 109G 19G 91G 18% / devtmpfs devtmpfs 3.8G 0 3.8G 0% /dev tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs tmpfs 3.9G 12M 3.8G 1% /run tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/sda1 xfs 1014M 142M 873M 14% /boot /dev/mapper/centos-home xfs 92G 33M 92G 1% /home tmpfs tmpfs 781M 0 781M 0% /run/user/0
Hot expansion successful! ! !