Shell script programming: Linux automated logical volume creation script

Source: Internet
Author: User

Write a script to create and manage LV:
Note: During script execution, a menu is displayed to the user, as shown in the following figure:
A create an LV.
B create an LV in an existing VG.
C extend an LV.
D reduce an LV.

If you select item A, the following functions are completed:
1) display information about all disks and disk cylinders on the current system: Total number of cylinders and the range of idle cylinders. Then, the system prompts you to select a specific disk,
As the disk for creating the LV;
2) After the user selects a disk, the partition information of the selected disk is displayed to the user;
3) Ask the user to create the VG name, the number of PVS, and the size of each pv. Then, create a new shard. The partition size corresponds to the PV size;
4) Create the corresponding PV and create the VG accordingly;
5) Create LV:
A) The user is prompted to enter the LV size and name to create the LV;
B) format the LV;
6) The user is prompted to specify a mount point.
A) if the mount point already exists and there are other storage devices mounted, you are prompted to change one until you change to another available mount point;
B) Otherwise, use this mount point to mount the LV;

If option B is selected, the following functions are completed:
1) The user is prompted to enter the name, size, and VG of the LV to be created;
2) Create the LV. (Note: before creating the LV, test whether the remaining space in the VG is sufficient for the specified LV ;)

If you select item C, the following functions are completed:
1) prompt the user to select the LV, then display the current LV size, and prompt the user to specify the Expanded size;
2) Extend the LV to the size specified by the user. (Note: Before expansion, test whether sufficient disk space is available in the current VG ;)

If you select item d, the following functions are completed:
1) The user is prompted to select the LV, and the current LV size is displayed, and the user is prompted to specify the reduced LV size;
2) scale down the LV to the size specified by the user. (Note: Before the scale-down, you must test whether the space after the scale-down can accommodate all data in the current LV ;)

 

#! /Bin/bash #: Title: lvm3.sh #: Synopsis: #: Date: 20:48:38 #: Version: 1.0 #: Author: Dean #: Options: function A {# disk information requires you to select only one empty hard disk for Operation d = 'fdisk-L | grep-V extended | grep di | awk '{print $2}' | cut -D: -F1 '// extract all disk names for I in $ D; do // print the cylindrical information of each disk in a for loop c = 'fdisk-L $ I | grep-V extended | head-3 | tail-1 | awk' {print $ 5} ''// capture the total cylindrical information Cn = 'fdisk-L $ I | grep-V extend Ed | tail-1 | awk '{print $3} ''// capture the last cylinder and exclude the extend partition if [$ Cn = 'start']; then // exclude new hard disks with no partitions. Let Cn = 0 echo "Disk: $ I total: $ c free: $ [$ C-$ cn]" else echo "Disk: $ I total: $ c free: $ [$ C-$ cn] "fidone # user input read-P" Please chose a free disk for creating LV: "disk fdisk-L $ disk read-P" input the VG's name; "VG read-P" the number of PV; "NPV # fdisk PV uses the for loop to implement the PV required by the user to create, accurate to each for (I = 1; I <= $ NPV; I + +); Do read-P "the size of $ I PV: "spvfdisk/dev/SDA <endnp + $ spv t $ i8ew enddone partprobe $ disk # PV $ VG create vg dpv = 'fdisk-L/dev Based on the created PV /SDA | tail-$ NPV | awk '{print $1} ''for I in $ DPV; do pvcreate $ idone DPV = 'echo $ dpv' vgcreate $ VG $ DPV # LV create asks the name of the created LV, and VG, create LV read-P "input the name of LV:" LV read-P "input the size of LV; "SLV lvcreate-L $ SLV-N $ LV $ VG mke2fs-r/dev /$ LV # Mount ask the user about the mount point and mount the LV read-P "input the Mount dir: "dir Mount | grep" dir "test = $? Until [$ test-EQ 0]; do echo "the Dir is exist, try another! "Read-P" input the Mount dir: "dir Mount | grep" dir "test = $? Done Mount/dev/$ VG/$ LV} function B {###################### create lvuntil [$ test-EQ 0]; do read-P "input the name of LV:" LV read-P "input the size of LV:" SLV vgdisplay read-P "input the VG's name: "VG lvcreate/dev/$ VG/$ LV test = $? Done ##################### scale up the LV. If the capacity is not enough, reselect the size to expand, until function c {until [$ test-EQ 0] can be extended; do read-P "which Lv You want extend:" LV lvdisplay $ LV read-P "input the VG; s name: "VG read-P" the size of extend: "Sen lvextend-L $ sen/dev/$ VG/$ LV test =$? Resize-P/dev/$ VG/$ lvdone }######################## scale down LV, if the capacity limit cannot be reduced, reselect the size until it can be reduced, and pre-check function d {until [$ test-EQ 0]; do read-P "which Lv You want reduce:" LV lvdisplay $ LV read-P "input the VG's name:" VG read-P "the size of reduce: "Sre umount/dev/$ VG/$ LV e2fsck-F/dev/$ VG/$ LV resize2fs/dev/$ VG/$ LV $ SRE test =$? Lvreduce-L $ SRE/dev/$ VG/$ lvdone} ### Program Selection interface echo "outputs" Echo '----------- program for LVM ---------------- 'echo "A create an LV. B create an LV in an existing VG. c extend an LV. d reduce an LV. "Read-P" your choise, others for quite: "chcase $ ch in 'A') A; 'B') B; 'C') C ;; 'D ') D; *) Exit; esac

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.