디스크 파티션 자동 생성 및 마운트 스크립트 포맷

4145 단어
설명:
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
/dev/sdb2 /mnt/sdb2 ext3 defaults 0 0
/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

전재 대상:https://blog.51cto.com/6638225/1844621

좋은 웹페이지 즐겨찾기