SHELL脚本实现硬盘分区
生活随笔
收集整理的這篇文章主要介紹了
SHELL脚本实现硬盘分区
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
寫一個腳本(前提:請為虛擬機新增一塊硬盤,假設它為/dev/sdb),為指定的硬盤創建分區
1,列出當前系統上所有的磁盤,讓用戶選擇,如果選擇quit則退出腳本;如果用戶選擇錯誤,就讓用戶重新選擇
2,檔用戶選擇后,提醒用戶確認接下來的操作可能會損壞數據,并請用戶確認:如果用戶選擇y就繼續,n就退出;否則,讓用戶重新選擇;
3,抹除那塊硬盤上的所有分區(提示,抹除所有分區后執行sync命令,并讓腳本睡眠3秒后在分區),并為其創建三個主分區,第一個為20M,第二個為512M,第三個為128M,切第三個為swap分區類型;(提示:將分區命令通過echo傳給fdisk即可實現)
#!/bin/bash # echo "Initial a disk..." echo -e "\033[31mWarning:\033[0m" fdisk -l 2> /dev/null | grep -o "^Disk /dev/[sh]d[a-z]"read -p "Your Choose:" PARTDISK if [ $PARTDISK == 'quit' ];then echo "quit" exit 7; fi until fdisk -l 2> /dev/null | grep -o "Disk /dev/[sh]d[a-z]" | grep "^Disk $PARTDISK$" &> /dev/null;do read -p "Wrong option,Your choice aging:" PARTDISK done read -p "will destroy all data,continue:" CHOICE until [ $CHOICE == 'y' -o $CHOICE == 'n' ];do read -p "Will destroy all data,continue:" CHOICE done if [ $CHOICE == 'n' ];then echo "QUIT" exit 9; else dd if=/dev/zero of=$PARTDISK bs=512 count=1 &> /dev/null sync sleep 3 echo 'n p+20M n p+512M n pn p+128M t 82 w' | fdisk $PARTDISK &> /dev/null partprobe $PARTDISK sync sleep 2 mke2fs -j ${PARTDISK}1 &> /dev/null mke2fs -j ${PARTDISK}2 &> /dev/null mkswap ${PARTDISK}3 &> /dev/null fi執行過程:
[root@localhost ~]# ./partdisk.sh Initial a disk... Warning: Disk /dev/sda Disk /dev/sdb Disk /dev/sdc Disk /dev/sdd Your Choose:/dev/sdb will destroy all data,continue:y 1+0 records in 1+0 records out bytes (512 B) copied, 0.0304787 s, 16.8 kB/s [root@localhost ~]# fdisk -lDisk /dev/sda: 21.5 GB, 21474836480 bytes heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00051800Device Boot Start End Blocks Id System /dev/sda1 * 1 39 307200 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 39 549 4096000 82 Linux swap / Solaris Partition 2 does not end on cylinder boundary. /dev/sda3 549 2611 16567296 83 LinuxDisk /dev/sdb: 21.5 GB, 21474836480 bytes heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x18756792Device Boot Start End Blocks Id System /dev/sdb1 1 4 32098+ 83 Linux /dev/sdb2 5 70 530145 83 Linux /dev/sdb3 71 87 136552+ 82 Linux swap / SolarisDisk /dev/sdc: 21.5 GB, 21474836480 bytes heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000Disk /dev/sdd: 21.5 GB, 21474836480 bytes heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000[root@localhost ~]# mount /dev/sdb1 /mnt [root@localhost ~]# ls /mnt lost+found [root@localhost ~]# vi partdisk.sh [root@localhost ~]# umount /mnt?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的SHELL脚本实现硬盘分区的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mkswap,swapon, swapo
- 下一篇: sed 中如何替换换行符