fdisk分區硬盤並shell腳本自動化

最近工做須要用到對硬盤進行shell腳本自動化分區和mount的操做,google了一些資料,下面作個總結。node

若是硬盤沒有進行分區(邏輯分區或者擴展分區,關於二者概念,自行google),咱們將沒法將使用該硬盤來進行讀寫。咱們要使用一塊硬盤須要進行下面三步:linux

  1. 將該硬盤進行分區;
  2. 對分區進行格式化;
  3. 將分區mount到系統某個目錄,即可以訪問。

本筆記會着重講一下第一步中涉及的fdisk分區功能以及如何來使用shell進行自動化處理,過程也會涉及後面兩步操做的簡單說明。git

fdisk對硬盤進行分區

fdisk是linux系統提供的硬盤分區工具。關於fdisk的詳細說明能夠自行google或者man fdisk。下面咱們直接說明操做步驟。github

先查看一下當前系統有哪些硬盤:shell

ubuntu@i-idh5qfpk:~$ ls /dev | grep sd
sda
sda1
sdb
sdc
sdd
sdd1

咱們就選擇/dev/sdc進行下手吧。經過fdisk -l查看一下該硬盤的分區表:ubuntu

ubuntu@i-idh5qfpk:~$ sudo fdisk -l /dev/sdc

Disk /dev/sdc: 10.7 GB, 10737418240 bytes
64 heads, 32 sectors/track, 10240 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xacb1d488

   Device Boot      Start         End      Blocks   Id  System

經過上面的信息,咱們能夠看到該硬盤總共有10.7GB,可是分區表爲空(命令最後面的輸出信息便是分區表信息)。下面咱們經過運行fdisk命令來對該硬盤進行交互式的分區操做。輸入fdisk /dev/sdc命令以後,會提示你進行什麼操做,若是不清楚的話,能夠輸入m而後回車查看操做說明。以下所示,bash

ubuntu@i-idh5qfpk:~$ sudo fdisk /dev/sdc

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): 

咱們要新建分區,輸入n,而後回車。提示該硬盤當前有0個主要分區(primary)、0個擴展分區(extended),能夠建立4個分區,而後讓咱們選擇建立主要分區仍是擴展分區。輸入p並回車(或者直接回車),選擇建立主要分區。ide

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p

而後提示輸入分區編號(只能1到4,默認爲1),咱們輸入1並回車,工具

Partition number (1-4, default 1): 1

輸入分區的開始扇區,咱們直接回車採用默認配置,ui

First sector (2048-20971519, default 2048): 
Using default value 2048

輸入分區的結束扇區,也直接回車採用默認配置,

Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): 
Using default value 20971519

此時咱們的分區建好了,但還須要輸入w並回車來進行保存。

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

該硬盤的分區創建完畢,經過fdisk -l查看一下最後的效果,

ubuntu@i-idh5qfpk:~$ sudo  fdisk -l /dev/sdc

Disk /dev/sdc: 10.7 GB, 10737418240 bytes
64 heads, 32 sectors/track, 10240 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xacb1d488

   Device Boot      Start         End      Blocks   Id  System
 /dev/sdc1            2048    20971519    10484736   83  Linux

咱們新建的分區就是/dev/sdc1。接下來,咱們對該分區進行格式化,

ubuntu@i-idh5qfpk:~$ sudo mkfs -t ext3 /dev/sdc1
mke2fs 1.42.9 (4-Feb-2014)
Discarding device blocks: done                            
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621184 blocks
131059 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

格式化完畢。而後將該分區掛載到目錄/mysdc,

ubuntu@i-idh5qfpk:~$ sudo mkdir -p /mysdc
ubuntu@i-idh5qfpk:~$ sudo mount /dev/sdc1 /mysdc

經過df命令查看一下。

ubuntu@i-idh5qfpk:~$ sudo df -k
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda1       20509308 13221580   6222872  68% /
none                   4        0         4   0% /sys/fs/cgroup
udev             2013184        4   2013180   1% /dev
tmpfs             404808      576    404232   1% /run
none                5120        0      5120   0% /run/lock
none             2024032     2084   2021948   1% /run/shm
none              102400        0    102400   0% /run/user
/dev/sdc1       10189112    23160   9641716   1% /mysdc

完成。

shell腳本自動化

上面咱們是經過fdisk工具交互式地進行對硬盤進行分區的。那咱們如何將其寫成shell腳本自動化。回顧一下,咱們剛剛交互式分區時輸入的操做序列,而後本身閱讀下面的腳本吧,太簡單了,沒什麼好講的。

#!/bin/bash

echo "n
p
1


w
" | fdisk /dev/sdc && mkfs -t /dev/sdc1

 

注意:1和w之間是兩個空行。

刪除分區

一樣經過fdisk來刪除分區,操做以下,不細說了,

ubuntu@i-idh5qfpk:~$ sudo fdisk /dev/sdc

Command (m for help): d
Selected partition 1

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

shell自動化腳本請參考建立分區的shell腳本自行編寫。

(done)

相關文章
相關標籤/搜索