日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SHELL脚本实现硬盘分区

發(fā)布時(shí)間:2024/9/16 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SHELL脚本实现硬盘分区 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

寫一個(gè)腳本(前提:請(qǐng)為虛擬機(jī)新增一塊硬盤,假設(shè)它為/dev/sdb),為指定的硬盤創(chuàng)建分區(qū)

1,列出當(dāng)前系統(tǒng)上所有的磁盤,讓用戶選擇,如果選擇quit則退出腳本;如果用戶選擇錯(cuò)誤,就讓用戶重新選擇

2,檔用戶選擇后,提醒用戶確認(rèn)接下來的操作可能會(huì)損壞數(shù)據(jù),并請(qǐng)用戶確認(rèn):如果用戶選擇y就繼續(xù),n就退出;否則,讓用戶重新選擇;

3,抹除那塊硬盤上的所有分區(qū)(提示,抹除所有分區(qū)后執(zhí)行sync命令,并讓腳本睡眠3秒后在分區(qū)),并為其創(chuàng)建三個(gè)主分區(qū),第一個(gè)為20M,第二個(gè)為512M,第三個(gè)為128M,切第三個(gè)為swap分區(qū)類型;(提示:將分區(qū)命令通過echo傳給fdisk即可實(shí)現(xiàn))

#!/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

執(zhí)行過程:

[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位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的SHELL脚本实现硬盘分区的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。