關注上方浩道linux,回復資料,即可獲取海量linux、Python、網絡通信等學習資料!
前言
大家好,這里是浩道linux,主要給大家分享linux、python、網絡通信相關的IT知識平臺。
今天浩道跟大家分享幾個關于磁盤管理的shell腳本,助理自動化管理磁盤!?
一、實現磁盤分區的
只支持分配主分區和標準的linux文件系統(ext4/xfs)的分區
?
#!?/bin/bash #?Function:對硬盤進行分區,得到一個標準的linux文件系統(ext4/xfs)的主分區 cat?/proc/partitions?>?old read?-p?"請輸入你要分區的硬盤(寫絕對路徑,如:/dev/sda):"?A if?[?-e?$A?];then ??echo?"true" else ??echo?"該設備不存在!!" ??exit fi read?-p?"請輸入你要創建的磁盤分區類型(這里只能是主分區,默認按回車即可):"?B read?-p?"請輸入分區數字,范圍1-4,默認從1開始,默認按回車即可:"?C read?-p?"請輸入扇區起始表號,默認按回車即可:"?D read?-p?"請輸入你要分區的分區大小(格式:如?+5G ):"?E fdisk?$A?<?new F=`diff?new?old?|?grep?"<"?|?awk?'{print?$5}'` echo?"-------------------------------" echo?$F echo?"你想對新分區設定什么類型的文件系統?有以下選項:" echo?"A:ext4文件系統" echo?"B:xfs文件系統" read?-p?"請輸入你的選擇:"?G case?$G?in ????????a|A) ???????????mkfs.ext4?/dev/$F ???????????echo?"該分區將被掛載在?"/mnt/$F"?下"? ???????????m=`ls?/mnt/|grep?$F?|?wc?-l` ???????????if?[?$m?-eq?0?];then ????????????mkdir?/mnt/$F ???????????fi ???????????n=`cat?/etc/fstab?|?grep?/dev/$F|?wc?-l` ???????????if?[?$n?-eq?0?];then ??????????????echo?"/dev/$F?????/mnt/$F?????ext4?????????defaults??????????0??????0"?>>?/etc/fstab ???????????else ??????????????sed?-i?'/^/dev/$F/c/dev/$F?????/mnt/$F?????ext4?????????defaults??????????0??????0'?/etc/fstab ???????????fi ???????????mount?-a ???????????df?-Th ;; ????????b|B) ???????????mkfs.xfs?-f?/dev/$F ???????????echo?"該分區將被掛載在?"/mnt/$F"?下"? ???????????m=`ls?/mnt/|grep?$F?|?wc?-l` ???????????if?[?$m?-eq?0?];then ??????????????mkdir?/mnt/$F ???????????fi ???????????n=`cat?/etc/fstab?|?grep?/dev/$F?|?wc?-l` ???????????if?[?$n?-eq?0?];then ??????????????echo?"/dev/$F?????/mnt/$F??????xfs???????defaults??????????0??????0"?>>?/etc/fstab ???????????else ??????????????sed?-i?'/^/dev/$F/c/dev/$F?????/mnt/$F?????xfs?????????defaults??????????0??????0'?/etc/fstab ???????????fi ???????????mount?-a ???????????df?-Th ;; ????????*) ???????????echo?"你的輸入有誤!!" esac
?
二、使用一整塊硬盤創建邏輯卷
?
#!/bin/bash #?Function:使用一整塊硬盤創建LVM邏輯卷 read?-p?"請輸入你要做成邏輯卷的硬盤(寫絕對路徑,如:/dev/sda):"?path if?[?-e?$path?];then ??echo?"true" else ??echo?"該設備不存在!!" ??exit fi pvcreate?$path echo?"該硬盤已做成物理卷!" vgcreate?myvg?$path echo?"該物理卷已加入卷組?myvg?中" vgs free=`vgs|?awk?'$1~/myvg/{print}'|awk?'{print?$6}'` echo?"該物理卷剩余的空間大小為:$free?" read?-p?"請輸入你要創建邏輯卷的大小(如:1G):"?repy2 lvcreate?-L?$repy2?-n?mylv?myvg echo?"已成功創建邏輯卷mylv" echo?"------------------------" lvs echo?"------------------------" echo?"你想對新分區設定什么類型的文件系統?有以下選項:" echo?"A:ext4文件系統" echo?"B:xfs文件系統" read?-p?"請輸入你的選擇:"?repy3 case?$repy3?in ????????a|A) ???????????mkfs.ext4?/dev/myvg/mylv ???????????echo?"該分區將被掛載在?"/mnt/mylv"?下"? ???????????m=`ls?/mnt/|grep?mylv?|?wc?-l` ???????????if?[?$m?-eq?0?];then ????????????mkdir?/mnt/mylv ???????????fi ???????????echo?"/dev/myvg/mylv?????/mnt/mylv?????ext4?????????defaults??????????0??????0"?>>?/etc/fstab ???????????mount?-a ???????????df?-Th ;; ????????b|B) ???????????mkfs.xfs?-f?/dev/myvg/mylv ???????????echo?"該分區將被掛載在?"/mnt/mylv"?下"? ???????????m=`ls?/mnt/|grep?mylv?|?wc?-l` ???????????if?[?$m?-eq?0?];then ??????????????mkdir?/mnt/mylv ???????????fi ???????????echo?"/dev/myvg/mylv?????/mnt/mylv??????xfs???????defaults??????????0??????0"?>>?/etc/fstab ???????????mount?-a ???????????df?-Th ;; ????????*) ???????????echo?"你的輸入有誤!!" esac
?
三、將一塊硬盤分區,然后分區設置為虛擬卷
?
#!?/bin/bash #?Author:謝公子 #?Date:2018-10-13 #?Function:新建一個分區,并做成邏輯卷 cat?/proc/partitions?>?old read?-p?"請輸入你要分區的硬盤(寫絕對路徑,如:/dev/sda):"?A if?[?-e?$A?];then ??echo?"true" else ??echo?"該設備不存在!!" ??exit fi read?-p?"請輸入你要創建的磁盤分區類型(這里只能是主分區,默認按回車即可):"?B read?-p?"請輸入分區數字,范圍1-4,默認從1開始,默認按回車即可:"?C read?-p?"請輸入扇區起始表號,默認按回車即可:"?D read?-p?"請輸入你要分區的分區大小(格式:如?+5G ):"?E read?-p?"請輸入你要劃分為邏輯卷的分區盤符(默認回車即可):"?Z fdisk?$A?<?new F=`diff?new?old?|?grep?"<"?|?awk?'{print?$5}'` echo?"-------------------------------" echo?$F pvcreate?/dev/$F echo?"該硬盤已做成物理卷!" n=`vgs?|?grep?myvg?|wc?-l` if?[?$n?-eq?0?];then ???vgcreate?myvg?/dev/$F ???echo?"該物理卷已加入卷組myvg中" else ???vgextend?myvg?/dev/$F ???echo??"該物理卷已加入卷組myvg中" ???vgs ???free=`vgs|?awk?'$1~/myvg/{print}'|awk?'{print?$7}'` ???echo?"該卷組剩余的空間大小為:$free?" ???lvs ???exit fi vgs free=`vgs|?awk?'$1~/myvg/{print}'|awk?'{print?$6}'` echo?"該卷組剩余的空間大小為:$free?" read?-p?"請輸入你要創建邏輯卷的大小(如:1G):"?repy2 lvcreate?-L?$repy2?-n?mylv?myvg echo?"已成功創建邏輯卷mylv" echo?"------------------------" lvs echo?"------------------------" echo?"你想對新分區設定什么類型的文件系統?有以下選項:" echo?"A:ext4文件系統" echo?"B:xfs文件系統" read?-p?"請輸入你的選擇:"?G case?$G?in ????????a|A) ???????????mkfs.ext4?/dev/myvg/mylv ???????????echo?"該分區將被掛載在?"/mnt/$F"?下"? ???????????m=`ls?/mnt/|grep?$F?|?wc?-l` ???????????if?[?$m?-eq?0?];then ????????????mkdir?/mnt/$F ???????????fi ???????????echo?"/dev/myvg/mylv?????/mnt/$F?????ext4?????????defaults??????????0??????0"?>>?/etc/fstab ???????????mount?-a ???????????df?-Th ;; ????????b|B) ???????????mkfs.xfs?-f?/dev/myvg/mylv ???????????echo?"該分區將被掛載在?"/mnt/$F"?下"? ???????????m=`ls?/mnt/|grep?$F?|?wc?-l` ???????????if?[?$m?-eq?0?];then ??????????????mkdir?/mnt/$F ???????????fi ???????????echo?"/dev/myvg/mylv?????/mnt/$F??????xfs???????defaults??????????0??????0"?>>?/etc/fstab ???????????mount?-a ???????????df?-Th ;; ????????*) ???????????echo?"你的輸入有誤!!" esac
?
審核編輯:湯梓紅
?
評論
查看更多