一键自动创建磁盘分区以及格式化挂载脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一键自动创建磁盘分区以及格式化挂载脚本相关的知识,希望对你有一定的参考价值。
说明:
1、默认分3个区,其中一个是SWAP分区
2、分区的大小没有写成交互式,直接定义在脚本,需要在脚本修改
3、只是演示测试,sda磁盘没有设置自动分区功能,可把sdb分区代码拷贝到sda位置即可
4、分区前会把分区磁盘的分区表先重置,请提前做好数据备份!!
#!/bin/bash #Author: Hill.Mai #Contact: [email protected] #Create time: 2016-08-31 08:43:29 #Description:auto disks part ,will creat /,boot,swap three partitions defaultly. echo "the disks exist list:" fdisk -l |grep ‘Disk /dev/sd[a-z]‘ echo "==================================================" PS3="chose which disk you want to create:" select VAR in `ls /dev/sd*|grep -o ‘sd[a-z]‘|uniq` quit do case $VAR in sda) fdisk -l /dev/sda break ;; sdb) #create partitions dd if=/dev/zero of=/dev/sdb bs=512 count=1 &> /dev/null echo "n p 1 +100M n p 2 +200M n p 3 +300M t 3 82 w" | fdisk /dev/sdb echo -e "\033[31mfdisk done\033[0m" #make filesystem mkfs -t ext3 /dev/sdb1 &> /dev/null mkfs -t ext3 /dev/sdb2 &> /dev/null mkswap /dev/sdb3 &> /dev/null echo -e "\033[31mmkfs done\033[0m" #mount the system echo -e "/dev/sdb1 /mnt/sdb1 ext3 defaults 0 0\n/dev/sdb2 /mnt/sdb2 ext3 defaults 0 0\n/dev/sdb3 swap swap defaults 0 0" >> /etc/fstab mkdir -p /mnt/{sdb1,sdb2} &> /dev/null mount -a &> /dev/null swapon -a &> /dev/null echo -e "\033[31mmount done\033[0m" break ;; quit) break;; *) echo "wrong disk,please check again";; esac done
实例效果:
[10:21 [email protected]~/shells]# sh partitions.sh the disks exist list: Disk /dev/sda: 214.7 GB, 214748364800 bytes Disk /dev/sdb: 21.5 GB, 21474836480 bytes ================================================== 1) sda 2) sdb 3) quit chose which disk you want to create:2 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x30b93fb0. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won‘t be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to switch off the mode (command ‘c‘) and change display units to sectors (command ‘u‘). Command (m for help): Command action e extended p primary partition (1-4) Partition number (1-4): First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): Command (m for help): Command action e extended p primary partition (1-4) Partition number (1-4): First cylinder (15-2610, default 15): Using default value 15 Last cylinder, +cylinders or +size{K,M,G} (15-2610, default 2610): Command (m for help): Command action e extended p primary partition (1-4) Partition number (1-4): First cylinder (41-2610, default 41): Using default value 41 Last cylinder, +cylinders or +size{K,M,G} (41-2610, default 2610): Command (m for help): Partition number (1-4): Hex code (type L to list codes): Changed system type of partition 3 to 82 (Linux swap / Solaris) Command (m for help): The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks. fdisk done mkfs done mount done
以上是关于一键自动创建磁盘分区以及格式化挂载脚本的主要内容,如果未能解决你的问题,请参考以下文章