Linux磁盤管理

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)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市论笔,隨后出現(xiàn)的幾起案子幢尚,更是在濱河造成了極大的恐慌,老刑警劉巖翅楼,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件尉剩,死亡現(xiàn)場離奇詭異,居然都是意外死亡毅臊,警方通過查閱死者的電腦和手機理茎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來管嬉,“玉大人皂林,你說我怎么就攤上這事◎橇茫” “怎么了础倍?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長胎挎。 經(jīng)常有香客問我沟启,道長,這世上最難降的妖魔是什么犹菇? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任德迹,我火速辦了婚禮,結(jié)果婚禮上揭芍,老公的妹妹穿的比我還像新娘胳搞。我一直安慰自己,他們只是感情好称杨,可當(dāng)我...
    茶點故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布肌毅。 她就那樣靜靜地躺著,像睡著了一般姑原。 火紅的嫁衣襯著肌膚如雪悬而。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天页衙,我揣著相機與錄音摊滔,去河邊找鬼阴绢。 笑死,一個胖子當(dāng)著我的面吹牛艰躺,可吹牛的內(nèi)容都是我干的呻袭。 我是一名探鬼主播,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼腺兴,長吁一口氣:“原來是場噩夢啊……” “哼左电!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起页响,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤篓足,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后闰蚕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體栈拖,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年没陡,在試婚紗的時候發(fā)現(xiàn)自己被綠了涩哟。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,615評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡盼玄,死狀恐怖贴彼,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情埃儿,我是刑警寧澤器仗,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站童番,受9級特大地震影響精钮,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜妓盲,卻給世界環(huán)境...
    茶點故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一杂拨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧悯衬,春花似錦、人聲如沸檀夹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽炸渡。三九已至娜亿,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蚌堵,已是汗流浹背买决。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工沛婴, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人督赤。 一個月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓嘁灯,卻偏偏與公主長得像,于是被迫代替她去往敵國和親躲舌。 傳聞我的和親對象是個殘疾皇子丑婿,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,630評論 2 359

推薦閱讀更多精彩內(nèi)容