红联Linux门户
Linux帮助

SHELL脚本实现分区

发布时间:2016-09-15 09:42:19来源:linux网站作者:vincent_duan
写一个脚本(前提:请为虚拟机新增一块硬盘,架设它为/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
1
+20M 
n
p
2
+512M 
n
p
n
p
3
+128M
t
3
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
512 bytes (512 B) copied, 0.0304787 s, 16.8 kB/s
[root@localhost ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 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: 0x00051800
Device 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  Linux
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 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: 0x18756792
Device 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 / Solaris
Disk /dev/sdc: 21.5 GB, 21474836480 bytes
255 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
Disk /dev/sdd: 21.5 GB, 21474836480 bytes
255 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
 
本文永久更新地址:http://www.linuxdiyf.com/linux/24170.html