Expect is a free programming language that enables automated and interactive tasks to communicate without the need for human intervention.
The expect RPM package needs to be installed before use, the version on the centos6.5 64 bit is expect-5.44.1.15-5.el6_4.x86_64
In addition, the scalability is built on the premise of/Is the logical volume:
View disk information through Df-h, for example:
Filesystem Size used Avail use% mounted on
/dev/mapper/volgroup-logvol01 35G 9.3G 24G 29%/
Tmpfs 7.8G 0 7.8G 0%/dev/shm
/DEV/SDA1 194M 34M 150M 19%/boot
Then view the logical volume information through Lvdisplay:
---Logical volume---
LV path/dev/volgroup/logvol01
LV Name LogVol01
VG Name VolGroup
LV UUID Cxd3nz-8goa-pye4-shyn-78yn-scbk-oqcvcz
LV Write Access Read/write
LV Creation Host, Time Localhost.localdomain, 2015-03-27 08:16:39 +0800
LV Status Available
# open 1
LV Size 35.05 GiB
Current LE 8974
Segments 1
Allocation inherit
Read ahead Sectors Auto
-Currently set to 256
Block Device 253:0
---Logical volume---
LV path/dev/volgroup/logvol00
LV Name LogVol00
VG Name VolGroup
LV UUID tp3pqg-vw3i-rvnd-wvz3-mws0-caif-lmpecl
LV Write Access Read/write
LV Creation Host, Time Localhost.localdomain, 2015-03-27 08:16:50 +0800
LV Status Available
# open 1
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead Sectors Auto
-Currently set to 256
Block Device 253:1
You can see that the logical volume is /dev/mapper/volgroup-logvol01 and the disk capacity is 35.05G.
Assuming that the new disk is SDB and that 2 partitions already exist in the previous system, the following script will complete the partitioning action and the newly created partition is SDB1:
#!/usr/bin/expectspawn fdisk/dev/sdbexpect "):" Send "n\n" expect "4)" send "p\n" expect ":" Send "1\n" expect ":" send "\ n" Expect ":" send "\ n" expect "):" Send "p\n" expect "):" Send "w\n" expect Eofexitend
After the execution of the above script is complete, the system may need to be restarted.
The next step is to sdb1 into/
#!/usr/bin/expectset timeout 3600spawn mkfs.ext4 /dev/sdb1expect ' override ' sleep 5spawn pvcreate /dev/sda3expect ' successfully created ' sleep 5spawn vgextend volgroup /dev/sda3expect ' successfully extended ' Sleep 5spawn fdisk -l /dev/sda3 set accum {} expect { {:* gb} { set accum "${accum} $expect _out (0,string)"         } }puts "\ n" puts "###########################################" puts "##### # sda3 has $accum free space ###### "puts " ################################# ########## "puts " \ n "puts " Please type the percentage of free space (1 to 100,defaul 100), then type enter: "puts " Please enter a positive integer within 100, Sda3 free disk space by percentage add to / use, and end with carriage return. The direct carriage return defaults to 100%: "expect_user " \ n " {set raw $expect _out (buffer) # remove final carriage returnset response [string trimright "$raw" "\n%"]}if {$response = = "} {set response 100}puts " $response "spawn lvextend -l + $response%free /dev/mapper/VolGroup-LogVol01expect ' successfully resized ' sleep 5spawn resize2fs /dev/mapper/VolGroup-LogVol01expect ' Blocks long ' #set timeout 5puts Resize complete! " Exitend
The script above will let you enter a number and decide how much space will be added to/from the new disk, and the direct return defaults to 100%.
Attached: expect_out usage
Expect-re ". *" {}
Automatically empties content inside the Expect_out cache
Expect_out (buffer) all the content entered
Expect_out (0,string) matches the input content
Expect semiautomatic disk expansion script