1.磁盤管理常用命令【1】
fdisk
是一個創(chuàng)建和維護分區(qū)表的程序,它兼容DOS類型的分區(qū)表灌危、BSD或者SUN類型的磁盤列表璃吧。
語法
fdisk [必要參數(shù)][選擇參數(shù)]
必要參數(shù):
-l 列出素所有分區(qū)表
-u 與"-l"搭配使用漓骚,顯示分區(qū)數(shù)目
選擇參數(shù):
-s<分區(qū)編號> 指定分區(qū)
-v 版本信息
菜單操作說明
m :顯示菜單和幫助信息
a :活動分區(qū)標記/引導(dǎo)分區(qū)
d :刪除分區(qū)
l :顯示分區(qū)類型
n :新建分區(qū)
p :顯示分區(qū)信息
q :退出不保存
t :設(shè)置分區(qū)號
v :進行分區(qū)檢查
w :保存修改
x :擴展應(yīng)用,高級功能
mkfs
(英文全拼:make file system)命令用于在特定的分區(qū)上建立 linux 文件系統(tǒng)闰歪。
使用方式?:
mkfs [-V] [-t fstype] [fs-options] filesys [blocks]
參數(shù)?:
device : 預(yù)備檢查的硬盤分區(qū)嚎研,例如:/dev/sda1
-V : 詳細顯示模式
-t : 給定檔案系統(tǒng)的型式,Linux 的預(yù)設(shè)值為 ext2
-c : 在制做檔案系統(tǒng)前课竣,檢查該partition 是否有壞軌
-l bad_blocks_file : 將有壞軌的block資料加到 bad_blocks_file 里面
block : 給定 block 的大小
mount
命令是經(jīng)常會使用到的命令嘉赎,它用于掛載Linux系統(tǒng)外的文件。
語法
mount [-hV]mount -a [-fFnrsvw] [-t vfstype]mount [-fnrsvw] [-o options [,...]] device | dir
mount [-fnrsvw] [-t vfstype] [-o options] device dir
參數(shù)說明:
-V:顯示程序版本
-h:顯示輔助訊息
-v:顯示較訊息于樟,通常和 -f 用來除錯公条。
-a:將 /etc/fstab 中定義的所有檔案系統(tǒng)掛上。
-F:這個命令通常和 -a 一起使用迂曲,它會為每一個 mount 的動作產(chǎn)生一個行程負責(zé)執(zhí)行靶橱。在系統(tǒng)需要掛上大量 NFS 檔案系統(tǒng)時可以加快掛上的動作。
-f:通常用在除錯的用途。它會使 mount 并不執(zhí)行實際掛上的動作关霸,而是模擬整個掛上的過程传黄。通常會和 -v 一起使用。
-n:一般而言队寇,mount 在掛上后會在 /etc/mtab 中寫入一筆資料膘掰。但在系統(tǒng)中沒有可寫入檔案系統(tǒng)存在的情況下可以用這個選項取消這個動作。
-s-r:等于 -o ro
-w:等于 -o rw
-L:將含有特定標簽的硬盤分割掛上佳遣。
-U:將檔案分割序號為 的檔案系統(tǒng)掛下识埋。-L 和 -U 必須在/proc/partition 這種檔案存在時才有意義。
-t:指定檔案系統(tǒng)的型態(tài)零渐,通常不必指定窒舟。mount 會自動選擇正確的型態(tài)。
-o async:打開非同步模式诵盼,所有的檔案讀寫動作都會用非同步模式執(zhí)行惠豺。
-o sync:在同步模式下執(zhí)行。
-o atime风宁、-o noatime:當(dāng) atime 打開時洁墙,系統(tǒng)會在每次讀取檔案時更新檔案的『上一次調(diào)用時間』。當(dāng)我們使用 flash 檔案系統(tǒng)時可能會選項把這個選項關(guān)閉以減少寫入的次數(shù)杀糯。
-o auto扫俺、-o noauto:打開/關(guān)閉自動掛上模式苍苞。
-o defaults:使用預(yù)設(shè)的選項 rw, suid, dev, exec, auto, nouser, and async.
-o dev固翰、-o nodev-o exec、-o noexec允許執(zhí)行檔被執(zhí)行羹呵。
-o suid骂际、-o nosuid:
允許執(zhí)行檔在 root 權(quán)限下執(zhí)行。
-o user冈欢、-o nouser:使用者可以執(zhí)行 mount/umount 的動作歉铝。
-o remount:將一個已經(jīng)掛下的檔案系統(tǒng)重新用不同的方式掛上。例如原先是唯讀的系統(tǒng)凑耻,現(xiàn)在用可讀寫的模式重新掛上太示。
-o ro:用唯讀模式掛上。
-o rw:用可讀寫模式掛上香浩。
-o loop=:使用 loop 模式用來將一個檔案當(dāng)成硬盤分割掛上系統(tǒng)类缤。
2.磁盤管理實例
2.1 使用fdisk -l命令查詢磁盤信息
(base) zfy@zfy-PC:~$ sudo fdisk -l
Disk /dev/sda: 80 GiB, 85899345920 bytes, 167772160 sectors
#塊設(shè)備名稱為/dev/sda,此設(shè)備的大小為80?GB邻吭,85899345920?bytes是轉(zhuǎn)換成字節(jié)后的大小餐弱,共167772160 個扇區(qū)
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x3b1ffeba
Device? ? Boot? ? Start? ? ? End? Sectors? Size Id Type
#分區(qū)序列 | 引導(dǎo)?| 開始?| 終止?| Sectors?|?容量?| 分區(qū)類型ID?| 分區(qū)類型
/dev/sda1? *? ? ? ? 2048? 3147775? 3145728? 1.5G 83 Linux
/dev/sda2? ? ? ? 3147776 138412031 135264256 64.5G? 5 Extended
/dev/sda3? ? ? 138412032 161480703? 23068672? 11G 83 Linux
/dev/sda4? ? ? 161480704 167770111? 6289408? ? 3G 82 Linux swap / Solaris
/dev/sda5? ? ? ? 3149824? 34605055? 31455232? 15G 83 Linux
/dev/sda6? ? ? ? 34607104? 66062335? 31455232? 15G 83 Linux
/dev/sda7? ? ? ? 66064384 138412031? 72347648 34.5G 83 Linux
Partition table entries are not in disk order.
2.2添加新硬盤并分區(qū)【2】
a. 在虛擬機中添加硬盤,添加后使用fdisk -l命令查看,磁盤變成兩個膏蚓,在sda中還沒有分區(qū)
root@zfy-PC:/home/zfy# fdisk -l
Disk /dev/sda: 2 GiB, 2147483648 bytes, 4194304 sectors
Disk model: VMware Virtual S
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 /dev/sdb: 80 GiB, 85899345920 bytes, 167772160 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x3b1ffeba
Device? ? Boot? ? Start? ? ? End? Sectors? Size Id Type
/dev/sdb1? *? ? ? ? 2048? 3147775? 3145728? 1.5G 83 Linux
/dev/sdb2? ? ? ? 3147776 138412031 135264256 64.5G? 5 Extended
/dev/sdb3? ? ? 138412032 161480703? 23068672? 11G 83 Linux
/dev/sdb4? ? ? 161480704 167770111? 6289408? ? 3G 82 Linux swap / Solaris
/dev/sdb5? ? ? ? 3149824? 34605055? 31455232? 15G 83 Linux
/dev/sdb6? ? ? ? 34607104? 66062335? 31455232? 15G 83 Linux
/dev/sdb7? ? ? ? 66064384 138412031? 72347648 34.5G 83 Linux
Partition table entries are not in disk order.
b.下面對sda進行分區(qū)
root@zfy-PC:/home/zfy# fdisk /dev/sda
Welcome to fdisk (util-linux 2.33.1).? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
Changes will remain in memory only, until you decide to write them.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xb985e226.
Command (m for help): m
#輸入m查看幫助
Help:
? DOS (MBR)
? a? toggle a bootable flag
? b? edit nested BSD disklabel
? c? toggle the dos compatibility flag
? Generic
? d? delete a partition
? F? list free unpartitioned space
? l? list known partition types
? n? add a new partition
? p? print the partition table
? t? change a partition type
? v? verify the partition table
? i? print information about a partition
? Misc
? m? print this menu
? u? change display/entry units
? x? extra functionality (experts only)
? Script
? I? load disk layout from sfdisk script file
? O? dump disk layout to sfdisk script file
? Save & Exit
? w? write table to disk and exit
? q? quit without saving changes
? Create a new label
? g? create a new empty GPT partition table
? G? create a new empty SGI (IRIX) partition table
? o? create a new empty DOS partition table
? s? create a new empty Sun partition table
Command (m for help): n
#輸入n創(chuàng)建一個分區(qū)
Partition type
? p? primary (0 primary, 0 extended, 4 free)
? e? extended (container for logical partitions)
Select (default p): p
#選擇主分區(qū)
Partition number (1-4, default 1): 1
#選擇編號為1
First sector (2048-4194303, default 2048): 2500000
#起始sector?設(shè)為2500000
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2500000-4194303, default 4194303): 4194303
#終止sector?設(shè)為4194303
Created a new partition 1 of type 'Linux' and of size 827.3 MiB.
#分區(qū)完成瓢谢,分區(qū)大小為827.3 MiB
Command (m for help): n
#繼續(xù)創(chuàng)建一個分區(qū)
Partition type
? p? primary (1 primary, 0 extended, 3 free)
? e? extended (container for logical partitions)
Select (default p): e
#新增擴展分區(qū)
Partition number (2-4, default 2): 2
#選擇編號為2
First sector (2048-4194303, default 2048): 2048
#起始sector為2048
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2499999, default 2499999):
#終止sector為2499999
Created a new partition 2 of type 'Extended' and of size 1.2 GiB.
#新增完成,大小為1.2GiB
Command (m for help): w
#保存
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
c.查看分區(qū)后磁盤信息驮瞧,此時sda磁盤已經(jīng)有兩個分區(qū)
Disk /dev/sda: 2 GiB, 2147483648 bytes, 4194304 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xb985e226
Device? ? Boot? Start? ? End Sectors? Size Id Type
/dev/sda1? ? ? 2500000 4194303 1694304 827.3M 83 Linux
/dev/sda2? ? ? ? ? 2048 2499999 2497952? 1.2G? 5 Extended
Partition table entries are not in disk order.
Disk /dev/sdb: 80 GiB, 85899345920 bytes, 167772160 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x3b1ffeba
Device? ? Boot? ? Start? ? ? End? Sectors? Size Id Type
/dev/sdb1? *? ? ? ? 2048? 3147775? 3145728? 1.5G 83 Linux
/dev/sdb2? ? ? ? 3147776 138412031 135264256 64.5G? 5 Extended
/dev/sdb3? ? ? 138412032 161480703? 23068672? 11G 83 Linux
/dev/sdb4? ? ? 161480704 167770111? 6289408? ? 3G 82 Linux swap / Solaris
/dev/sdb5? ? ? ? 3149824? 34605055? 31455232? 15G 83 Linux
/dev/sdb6? ? ? ? 34607104? 66062335? 31455232? 15G 83 Linux
/dev/sdb7? ? ? ? 66064384 138412031? 72347648 34.5G 83 Linux
Partition table entries are not in disk order.
d.用ext3的文件系統(tǒng)對分區(qū)進行格式化
root@zfy-PC:/home/zfy# mkfs -t ext3 /dev/sda1
mke2fs 1.44.5 (15-Dec-2018)
Creating filesystem with 211788 4k blocks and 52976 inodes
Filesystem UUID: db384338-4af1-4eec-a62d-f2b2f8e0ec96
Superblock backups stored on blocks:
? ? ? ? 32768, 98304, 163840
Allocating group tables: done? ? ? ? ? ? ? ? ? ? ? ? ? ?
Writing inode tables: done? ? ? ? ? ? ? ? ? ? ? ? ? ?
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
e.新建一個分區(qū)目錄 /data氓扛,并掛載
root@zfy-PC:/home/zfy# mkdir data
root@zfy-PC:/home/zfy# mount /dev/sda1 /data
root@zfy-PC:/home/zfy# df -TH /data/#查看是否掛載成功
文件系統(tǒng)? ? ? 類型? 容量? 已用? 可用 已用% 掛載點
/dev/sda1? ? ? ext3? 838M? 881k? 793M? ? 1% /data
f. 用vim打開?etc/fstab并假如 /dev/sda1/data ext3 defaults 0 0,這樣重啟后便不需要再掛載
參考文獻:
【1】Linux mount命令 | 菜鳥教程 (runoob.com)
【2】linux添加新磁盤和創(chuàng)建分區(qū) - IT(小菜雞) - 博客園 (cnblogs.com)