需求:有10台华为鲲鹏920云主机都需要加挂磁盘,所以非常需要脚本自动化进行操作


之前博主写过关于物理硬盘、云硬盘等磁盘扩容的文章(如下:)

PCL(鹏城实验室开发者云)虚机云硬盘扩容教程【图文】

linux服务器物理磁盘扩容

云主机磁盘centos7根目录扩容

注意:本次安利的脚本跟以上文章有所不同,希望大家能够融会贯通!


  • 未加挂磁盘前
  • ​[root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdd 8:48 0 500G 0 disk sdb 8:16 0 500G 0 disk sdc 8:32 0 500G 0 disk sda 8:0 0 500G 0 disk ├─sda2 8:2 0 79.8G 0 part / └─sda1 8:1 0 200M 0 part /boot/efi [root@localhost ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 8.0G 0 8.0G 0% /dev tmpfs tmpfs 8.0G 0 8.0G 0% /dev/shm tmpfs tmpfs 8.0G 86M 7.9G 2% /run tmpfs tmpfs 8.0G 0 8.0G 0% /sys/fs/cgroup /dev/sda2 ext4 79G 5.6G 69G 8% / /dev/sda1 vfat 200M 4.5M 196M 3% /boot/efi tmpfs tmpfs 1.6G 0 1.6G 0% /run/user/0 [root@localhost ~]# ​

  • 加挂磁盘后
  • ​[root@hadoop140 ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 8.0G 0 8.0G 0% /dev tmpfs tmpfs 8.0G 0 8.0G 0% /dev/shm tmpfs tmpfs 8.0G 94M 7.9G 2% /run tmpfs tmpfs 8.0G 0 8.0G 0% /sys/fs/cgroup /dev/sda2 ext4 79G 5.9G 69G 8% / /dev/sda1 vfat 200M 4.5M 196M 3% /boot/efi tmpfs tmpfs 1.6G 0 1.6G 0% /run/user/0 /dev/mapper/centos-root ext4 1.5T 24K 1.4T 1% /datafs [root@hadoop140 ~]# [root@hadoop140 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdd 8:48 0 500G 0 disk └─sdd1 8:49 0 500G 0 part └─centos-root 253:0 0 1.5T 0 lvm /datafs sdb 8:16 0 500G 0 disk └─sdb1 8:17 0 500G 0 part └─centos-root 253:0 0 1.5T 0 lvm /datafs sdc 8:32 0 500G 0 disk └─sdc1 8:33 0 500G 0 part └─centos-root 253:0 0 1.5T 0 lvm /datafs sda 8:0 0 500G 0 disk ├─sda2 8:2 0 79.8G 0 part / └─sda1 8:1 0 200M 0 part /boot/efi [root@hadoop140 ~]# ​

博主创作辛苦!

这是一个尊重知识的时代!

白嫖的老伙计们,记得打赏支持一下!(博主穷的叮当响了,饭都干不上了)


以下是脚本详细内容!

首先,编写扩容脚本

​vim extend-disk.sh​

#!/bin/bash
echo '--------第1块硬盘:sdb--------' && \
fdisk -u /dev/sdb && \
echo '--------Physical volume:sdb1--------' && \
pvcreate /dev/sdb1 && \
echo '--------Volume group centos--------'
vgcreate centos /dev/sdb1 && \
echo '' && \
echo '--------第2块硬盘:sdc--------' && \
fdisk -u /dev/sdc && \
echo '--------Physical volume:sdc1--------' && \
pvcreate /dev/sdc1 && \
echo '--------Volume group centos--------'
vgextend centos /dev/sdc1 && \
echo '' && \
echo '--------第3块硬盘:sdd--------' && \
fdisk -u /dev/sdd && \
echo '--------Physical volume:sdd1--------' && \
pvcreate /dev/sdd1 && \
echo '--------Volume group centos--------'
vgextend centos /dev/sdd1 && \
echo '' && \
echo '--------Logical volume root--------' && \
lvcreate -l 356 -n root centos && \
echo '' && \
echo '--------ext4--------' && \
mkfs -t ext4 /dev/centos/root && \
echo '' && \
echo '--------Logical volume centos/root--------' && \
lvextend -l +100%FREE /dev/centos/root && \
echo '' && \
echo '--------同步文件系统--------' && \
resize2fs /dev/centos/root && \
echo '' && \
echo '--------挂载到/datafs/--------' && \
mount /dev/centos/root /datafs/ && \
echo '' && \
echo '--------查看文件系统--------' && \
df -hT && \
echo '' && \
echo '扩容完毕!'

然后,给脚本授予可执行权限

​chmod +x extend-disk.sh​

最后,执行脚本

​./extend-disk.sh​

博主创作不易!

请尊重原创!

白嫖虽好,请勿抄袭!抄袭者将万劫不复!诸事不顺!(开个玩笑哈!)

感谢您的支持!


最后

本不打算贴出详细记录的,博主想想还是贴出来吧,不然又要有粉丝不会搞了

如有不明白之处,欢迎各位看官私信本博主!

[root@localhost ~]# vim extend-disk.sh
[root@localhost ~]# chmod +x extend-disk.sh
[root@localhost ~]# ./extend-disk.sh
--------第1块硬盘: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 0x64eaa985.

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

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

Calling ioctl() to re-read partition table.
Syncing disks.
--------Physical volume:sdb1--------
Physical volume "/dev/sdb1" successfully created.
--------Volume group centos--------
Volume group "centos" successfully created

--------第2块硬盘:sdc--------
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 0x696ccf4b.

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

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

Calling ioctl() to re-read partition table.
Syncing disks.
--------Physical volume:sdc1--------
Physical volume "/dev/sdc1" successfully created.
--------Volume group centos--------
Volume group "centos" successfully extended

--------第3块硬盘:sdd--------
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 0xda8362fd.

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

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

Calling ioctl() to re-read partition table.
Syncing disks.
--------Physical volume:sdd1--------
Physical volume "/dev/sdd1" successfully created.
--------Volume group centos--------
Volume group "centos" successfully extended

--------Logical volume root--------
Logical volume "root" created.

--------ext4--------
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
91200 inodes, 364544 blocks
18227 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=373293056
12 block groups
32768 blocks per group, 32768 fragments per group
7600 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done


--------Logical volume centos/root--------
Size of logical volume centos/root changed from 1.39 GiB (356 extents) to 1.46 TiB (383997 extents).
Logical volume centos/root successfully resized.

--------同步文件系统--------
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/centos/root to 393212928 (4k) blocks.
The filesystem on /dev/centos/root is now 393212928 blocks long.


--------挂载到/datafs/--------

--------查看文件系统--------
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 8.0G 0 8.0G 0% /dev
tmpfs tmpfs 8.0G 0 8.0G 0% /dev/shm
tmpfs tmpfs 8.0G 35M 8.0G 1% /run
tmpfs tmpfs 8.0G 0 8.0G 0% /sys/fs/cgroup
/dev/sda2 ext4 79G 4.7G 70G 7% /
/dev/sda1 vfat 200M 4.5M 196M 3% /boot/efi
tmpfs tmpfs 1.6G 0 1.6G 0% /run/user/0
/dev/mapper/centos-root ext4 1.5T 24K 1.4T 1% /datafs

扩容完毕!
[root@localhost ~]#