前提:为虚拟机新增一块磁盘,对其进行操作
要求
1.列出当前系统上所有的磁盘,让用户选择要操作的磁盘,如果选择quit或者exit则退出程序,选择错误则重新选择;
2.当前用户选择后,提醒用户接下来的操作可能会损坏磁盘是否继续操作,若用户选择y或Y则继续进行,选择n或N则退出程序,选择其他则让用户重新选择;
3.如果需要则抹除要操作磁盘的所有分区信息(提示:抹除分区信息后执行sync 命令,以便让硬盘和内存同步,而且让脚本睡眠几秒钟后再创建分区),为其分别创建3个大小以此为100M 200M 300M的主分区并将第三个分区类型改为swap(也可以按照自己的想法创建,无固定要求) 提示:可以将分区的命令用echo传送给fdisk即可实现分区
代码:
[root@localhost shell]# cat format_fdisk
#!/bin/bash
#format fdisk and create fdisk with some suggestions
echo "fdisk messages are:"
fdisk -l 2> /dev/null | grep "^Disk /dev/[sh]d[a-z]"
read -p "input you chioce:" chioce
if [ $chioce == 'quit' -o $chioce == 'exit' ]
then
echo "quiting!!!"
exit 7
fi
until fdisk -l 2> /dev/null | grep "^Disk /dev/[sh]d[a-z]" | grep "$chioce"
do
read -p "Worng select!Tyr again: " chioce
if [ $chioce == 'quit' -o $chioce == 'exit' ]
then
echo "quiting!!!"
exit 7
fi
done
echo -e "\e[1;31m Warnning! if not do following things? This maybe destory your fdisk! \e[0m"
read -p "input your select,continue or quit? " select
until [ $select == 'y' -o $select == 'Y' -o $select == 'n' -o $select == 'N' ]
do
read -p "Wrong select!Try again: " select
done
if [ $select == 'y' -o $select == 'Y' ]
then
echo "continue......"
else
echo "quiting......."
exit 9
fi
echo "format......"
dd if=/dev/zero of=$chioce bs=512 count=1
echo "sync......"
sync
echo "Loading......"
sleep 5
echo "Starting format_fdisk......"
echo '
n
p
1
+100M
n
p
2
+200M
n
p
3
+300M
t
3
82
w' | fdisk $chioce &> /dev/null #这里是不让其将输出信息显示到屏幕上,若果想看一下过程可以把红色部分去掉,当然也可以在其他某些地方加上
echo "fdisk complete......"
echo "kernel is reading......"
partprobe $chioce
echo "syncing....."
sync
echo "Loading....."
sleep 5
echo "starting create filesysten type..."
mke2fs -j ${chioce}1
mke2fs -j ${chioce}2
mke2fs -j ${chioce}3
echo "Complete!!!"
执行结果:(有点长,不想看可以跳过)
[root@localhost shell]# ./format_fdisk
fdisk messages are:
Disk /dev/sda: 21.5 GB, 21474836480 bytes
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
Disk /dev/sdc: 19.3 GB, 19327352832 bytes
input you chioce:/dev/sdc
Disk /dev/sdc: 19.3 GB, 19327352832 bytes
Warnning! if not do following things? This maybe destory your fdisk!
input your select,continue or quit? y
continue......
format......
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.0390401 s, 13.1 kB/s
sync......
Loading......
Starting format_fdisk......
fdisk complete......
kernel is reading......
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sdc (Device or resource busy). As a result, it may not reflect all of your changes until after reboot.
syncing.....
Loading.....
starting create filesysten type...
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
28112 inodes, 112420 blocks
5621 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
14 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
52416 inodes, 208844 blocks
10442 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
26 block groups
8192 blocks per group, 8192 fragments per group
2016 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
78624 inodes, 313264 blocks
15663 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
39 block groups
8192 blocks per group, 8192 fragments per group
2016 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
Complete!!!
测试看一下各个分区能否挂载
[root@localhost shell]# mount /dev/sdc1 /mnt/
[root@localhost shell]# ls /mnt/
lost+found
[root@localhost shell]# umount /mnt/
[root@localhost shell]# mount /dev/sdc2 /mnt/
[root@localhost shell]# ls /mnt/
lost+found
[root@localhost shell]# umount /mnt/
[root@localhost shell]# mount /dev/sdc3 /mnt/
[root@localhost shell]# ls /mnt/
lost+found
[root@localhost shell]# umount /mnt/
先到这,以后再补充!
阅读(651) | 评论(0) | 转发(0) |