最近工作需要用到對硬盤進行shell腳本自動化分區和mount的操作,google了一些資料,下面做個總結。
如果硬盤沒有進行分區(邏輯分區或者擴展分區,關於兩者概念,自行google),我們將無法將使用該硬盤來進行讀寫。我們要使用一塊硬盤需要進行下面三步:
- 將該硬盤進行分區;
- 對分區進行格式化;
- 將分區mount到系統某個目錄,便可以訪問。
本筆記會着重講一下第一步中涉及的fdisk分區功能以及如何來使用shell進行自動化處理,過程也會涉及后面兩步操作的簡單說明。
fdisk對硬盤進行分區
fdisk是linux系統提供的硬盤分區工具。關於fdisk的詳細說明可以自行google或者man fdisk。下面我們直接說明操作步驟。
先查看一下當前系統有哪些硬盤:
ubuntu@i-idh5qfpk:~$ ls /dev | grep sd sda sda1 sdb sdc sdd sdd1
我們就選擇/dev/sdc進行下手吧。通過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
通過上面的信息,我們可以看到該硬盤總共有10.7GB,但是分區表為空(命令最后面的輸出信息即是分區表信息)。下面我們通過運行fdisk命令來對該硬盤進行交互式的分區操作。輸入fdisk /dev/sdc命令之后,會提示你進行什么操作,如果不清楚的話,可以輸入m然后回車查看操作說明。如下所示,
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並回車(或者直接回車),選擇創建主要分區。
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
輸入分區的開始扇區,我們直接回車采用默認配置,
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)