鍍金池/ 教程/ Linux/ 磁盤分區(qū),格式化與檢驗
Linux 學(xué)習(xí)記錄--開機(jī)掛載錯誤
日志系統(tǒng)
數(shù)據(jù)流重定向
內(nèi)存交換空間的構(gòu)建
文件系統(tǒng)簡介
Linux 學(xué)習(xí)記錄--軟件安裝 RPM|SRPM|YUM
文件特殊權(quán)限
目錄配置 FHS
文件內(nèi)容查閱
Boot Loader
文件壓縮
Linux 學(xué)習(xí)記錄--文件權(quán)限
Linux 命令縮寫
命令與文件的查詢
文件|目錄的默認(rèn)權(quán)限與隱藏權(quán)限
shell script
服務(wù)
Linux 學(xué)習(xí)記錄--程序編譯與函數(shù)庫
正則表達(dá)式與其應(yīng)用
關(guān)機(jī)相關(guān)指令
shell
vim 與 vi 常用命令
系統(tǒng)調(diào)用:進(jìn)程控制
文件系統(tǒng)簡單操作
磁盤掛載與卸載
有名管道通訊
磁盤分區(qū),格式化與檢驗
工作管理與進(jìn)程管理
匿名管道通訊
Linux 學(xué)習(xí)記錄--啟動流程
文件與目錄管理
管道命令
命名別名與歷史命令
文件備份|還原
shell變量
Linux 學(xué)習(xí)記錄--ACL 權(quán)限控制
內(nèi)核|內(nèi)核模塊編譯
文件管理相關(guān)系統(tǒng)編程

磁盤分區(qū),格式化與檢驗

磁盤分區(qū):fdisk
磁盤格式化:mkfs,mke2fs
磁盤檢測:fsck
大容量磁盤分區(qū):parted

磁盤分區(qū)

語法:fdisk[-l] 設(shè)備名稱
-l:輸出系統(tǒng)內(nèi)所有分區(qū)

舉例:

[root@localhost ~]# fdisk -l

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes

  Device Boot      Start         End      Blocks  Id  System
/dev/sda1  *           1          13     104391   83  Linux
/dev/sda2              14        1288   10241437+  83  Linux
/dev/sda3            1289        1925    5116702+  83  Linux
/dev/sda4            1926        2610    5502262+   5  Extended
/dev/sda5            1926        2052    1020096   82 Linux swap / Solaris
/dev/sda6            2053        2302    2008093+  83  Linux

1. 查看磁盤文件名
[root@localhost ~]# df /
文件系統(tǒng) 1K-塊 已用 可用 已用% 掛載點(diǎn)
/dev/sda2 9920624 4329108 5079448 47% /

2. 查看磁盤分區(qū)功能

[root@localhost ~]# fdisk /dev/sda  //這里不帶數(shù)字

The number of cylinders for this disk isset to 2610.
There is nothing wrong with that, but thisis larger than 1024,
and could in certain setups cause problemswith:
1) software that runs at boot time (e.g.,old versions of LILO)
2) booting and partitioning software fromother OSs
  (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): m
Command action
  a   toggle a bootable flag
  b   edit bsd disklabel
  c   toggle the dos compatibilityflag
  d   delete a partition //刪除磁盤分區(qū)
  l   list known partition types
  m   print this menu  //查看磁盤分區(qū)功能
  n   add a new partition //增加一個磁盤分區(qū)
  o   create a new empty DOSpartition table
  p   print the partition table //查看磁盤分區(qū)
  q   quit without saving changes
  s   create a new empty Sundisklabel
  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 (expertsonly)

刪除磁盤分區(qū)

[root@localhost ~]# fdisk /dev/sda

The number of cylinders for this disk isset to 2610.
There is nothing wrong with that, but thisis larger than 1024,
and could in certain setups cause problemswith:
1) software that runs at boot time (e.g.,old versions of LILO)
2) booting and partitioning software fromother OSs
  (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes

  Device Boot      Start         End     Blocks   Id  System
/dev/sda1  *           1          13      104391  83  Linux
/dev/sda2              14        1288   10241437+  83  Linux
/dev/sda3            1289        1925    5116702+  83  Linux
/dev/sda4            1926        2610    5502262+   5  Extended
/dev/sda5            1926        2052    1020096   82  Linux swap / Solaris
/dev/sda6            2053        2302    2008093+  83  Linux

由上可知我的磁盤主要分為6個分區(qū),1,2,3為主分區(qū),4為擴(kuò)展分區(qū),5為 swap 分區(qū),6是邏輯分區(qū)

Command (m for help): d
Partition number (1-6): 3

Command (m for help): p

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes

  Device Boot      Start         End      Blocks  Id  System
/dev/sda1  *           1          13      104391  83  Linux
/dev/sda2              14        1288   10241437+  83  Linux
/dev/sda4            1926        2610    5502262+   5  Extended
/dev/sda5            1926        2052    1020096   82  Linux swap / Solaris
/dev/sda6            2053        2302    2008093+  83  Linux

刪除主分區(qū) sad3 后可以看到磁盤信息不在包含 sad3
Command (m for help): d
Partition number (1-6): 4

Command (m for help): p

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes

  Device Boot      Start         End      Blocks  Id  System
/dev/sda1  *           1          13      104391  83  Linux
/dev/sda2              14        1288   10241437+  83  Linux

刪除擴(kuò)展分區(qū) sad4 后可以看到擴(kuò)展分區(qū),邏輯分區(qū)都被刪除(因為邏輯分區(qū)是由擴(kuò)展分區(qū)衍生而來的)。

增加磁盤分區(qū)

磁盤分區(qū)最多只能有4個主分區(qū)+擴(kuò)展分區(qū)組成,其中擴(kuò)展分區(qū)最多只能有一個,剩下在創(chuàng)建的分區(qū)都是由擴(kuò)展分區(qū)衍生出來的邏輯分區(qū)

舉例1. 由于磁盤現(xiàn)分區(qū)分為3個主分區(qū),1個擴(kuò)展分區(qū)。因此在創(chuàng)建時將直接創(chuàng)建邏輯分區(qū),而不在詢問是否創(chuàng)建主分區(qū)或者擴(kuò)展分區(qū)

[root@localhost ~]# fdisk /dev/sda

The number of cylinders for this disk isset to 2610.
There is nothing wrong with that, but thisis larger than 1024,
and could in certain setups cause problemswith:
1) software that runs at boot time (e.g.,old versions of LILO)
2) booting and partitioning software fromother OSs
  (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes

  Device Boot      Start         End      Blocks  Id  System
/dev/sda1  *           1          13      104391  83  Linux
/dev/sda2              14        1288   10241437+  83  Linux
/dev/sda3            1289        1925    5116702+  83  Linux
/dev/sda4            1926        2610    5502262+   5  Extended
/dev/sda5            1926        2052    1020096   82 Linux swap / Solaris
/dev/sda6            2053        2302    2008093+  83  Linux

Command (m for help): n
First cylinder (2303-2610, default 2303):

舉例2:創(chuàng)建主/擴(kuò)展分區(qū)

[root@localhost ~]# fdisk /dev/sda

The number of cylinders for this disk isset to 2610.
There is nothing wrong with that, but thisis larger than 1024,
and could in certain setups cause problemswith:
1) software that runs at boot time (e.g.,old versions of LILO)
2) booting and partitioning software fromother OSs
  (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): d //先將主分區(qū)和邏輯分區(qū)刪除(如果為4個則默認(rèn)創(chuàng)建邏輯分區(qū))
Partition number (1-6): 2

Command (m for help): d
Partition number (1-6): 4

Command (m for help): p

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes

  Device Boot      Start         End      Blocks  Id  System
/dev/sda1  *           1          13      104391  83  Linux
/dev/sda3            1289        1925    5116702+  83  Linux

Command (m for help): n
Command action
   e  extended
   p  primary partition (1-4)

提示用戶選擇是是創(chuàng)建主分區(qū)還是擴(kuò)展分區(qū)

舉例3.創(chuàng)建邏輯分區(qū)與擴(kuò)展分區(qū)

root@localhost ~]# fdisk /dev/sda

The number of cylinders for this disk isset to 2610.
There is nothing wrong with that, but thisis larger than 1024,
and could in certain setups cause problemswith:
1) software that runs at boot time (e.g.,old versions of LILO)
2) booting and partitioning software fromother OSs
  (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes

  Device Boot      Start         End      Blocks  Id  System
/dev/sda1  *           1          13      104391  83  Linux
/dev/sda2             14        1288   10241437+  83  Linux
/dev/sda3            1289        1925    5116702+  83  Linux
/dev/sda4            1926        2610    5502262+   5  Extended
/dev/sda5            1926        2052    1020096   82  Linux swap / Solaris
/dev/sda6            2053        2302    2008093+  83  Linux

Command (m for help): d
Partition number (1-6): 4

Command (m for help): n
Command action
  e   extended
  p   primary partition (1-4)
e
Selected partition 4
First cylinder (1926-2610, default 1926):
Using default value 1926
Last cylinder or +size or +sizeM or +sizeK(1926-2610, default 2610):
Using default value 2610

Command (m for help): p

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes

  Device Boot      Start         End      Blocks  Id  System
/dev/sda1  *           1          13      104391  83  Linux
/dev/sda2              14        1288   10241437+  83  Linux
/dev/sda3            1289        1925    5116702+  83  Linux
/dev/sda4            1926        2610    5502262+   5  Extended

sd4為新創(chuàng)建的擴(kuò)展分區(qū),大小為從柱面1926到2610

Command (m for help): n
Firstcylinder (1926-2610, default 1926):
Usingdefault value 1926
Lastcylinder or +size or +sizeM or +sizeK (1926-2610, default 2610): +500M

對于此處可以指定柱面號碼,以可以通過+XXM 指定大小,讓其自動分配柱面
Command (m for help): p

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes

  Device Boot      Start         End      Blocks  Id  System
/dev/sda1  *           1          13      104391  83  Linux
/dev/sda2              14        1288   10241437+  83  Linux
/dev/sda3            1289        1925    5116702+  83  Linux
/dev/sda4            1926        2610    5502262+   5  Extended
/dev/sda5            1926        1987      497983+ 83  Linux

sd5為新創(chuàng)建的邏輯分區(qū),大小為500M

內(nèi)核查找分區(qū)

當(dāng)我們增加分區(qū)后,系統(tǒng)讓我們 reboot 以加載分區(qū)。也可以不用重啟,只需要通知內(nèi)容重新查找分區(qū)即可

The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition tablefailed with error 16: 設(shè)備或資源忙.
The kernel still uses the old table.
The new table will be used at the nextreboot.
Syncing disks.
[root@localhost~]# partprobe

磁盤格式化

分區(qū)完畢后要進(jìn)行文件系統(tǒng)的格式化

mkfs
語法:mkfs[-t 文件系統(tǒng)格式] 設(shè)備文件名
選項與參數(shù):
-t:文件系統(tǒng)格式,例如 ext3,ext2,vfat 等

舉例

[root@localhost ~]# mkfs -t ext3 /dev/sda7
mke2fs 1.39 (29-May-2006)
Filesystemlabel=
OS type: Linux
Blocksize=1024 (log=0)
Fragment size=1024 (log=0)
50200 inodes, 200780 blocks
10039 blocks (5.00%) reserved for the superuser
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments pergroup
2008 inodes per group
Superblock backups stored on blocks:
       8193, 24577, 40961, 57345, 73729

Writing inode tables: done                           
Creating journal (4096 blocks): done
Writing superblocks and filesystemaccounting information: done

This filesystem will be automaticallychecked every 37 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

其中文件系統(tǒng) Label 以及 iBLOCK 大小均采用默認(rèn)大小。如果對于 EXT2/EXT3 我們對這些信息由特殊的需求,可以使用 mke2fs

mke2fs
語法:mke2fs[-b block大小] [-i inode 大小] [-L 卷標(biāo)] [-cj] 設(shè)備
選項與參數(shù):
-b:設(shè)置 block 大小,目前支持1024,2048,4096
-i:多少容量給予一個 inode
-c:檢查磁盤錯誤
-L:卷標(biāo)名稱(Label)
-j:自動加入日志系統(tǒng)成為 EXT3文件系統(tǒng),不加在默認(rèn)為 EXT2

舉例

[root@localhost ~]# mke2fs -b 2048 -i 4096-L "TKFDISK" -j /dev/sda7
mke2fs 1.39 (29-May-2006)
Filesystemlabel=TKFDISK
OS type: Linux
Blocksize=2048 (log=1)
Fragment size=2048 (log=1)
50288 inodes, 100390 blocks
5019 blocks (5.00%) reserved for the superuser
First data block=0
Maximum filesystem blocks=103809024
7 block groups
16384 blocks per group, 16384 fragments pergroup
7184 inodes per group
Superblock backups stored on blocks:
       16384, 49152, 81920

Writing inode tables: done                           
Creatingjournal (4096 blocks): done
Writing superblocks and filesystemaccounting information: done

This filesystem will be automaticallychecked every 31 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

磁盤檢測(fsck)

語法: fsck [-t 文件系統(tǒng)格式] [-ACay]
選項與參數(shù)
-t :文件系統(tǒng)格式。
-A :依據(jù)/etc/fstab 的內(nèi)容,將需要的裝置掃瞄一次。
-a :自動修復(fù)檢查到的有問題的扇區(qū).
-y :與 -a 類似,但是某些 filesystem 僅支持 -y 這個參數(shù)
-C :可以在檢驗的過程當(dāng)中,使用一個直方圖來顯示目前的進(jìn)度!

EXT2/EXT3 的額外選項功能:(e2fsck 這支命令所提供)
-f :強(qiáng)制檢查!一般來說,如果 fsck 沒有發(fā)現(xiàn)任何 unclean 的旗標(biāo),不會主動進(jìn)入細(xì)部檢查的,如果您想要強(qiáng)制 fsck 進(jìn)入細(xì)部檢查,就得加上 -f
-D :針對文件系統(tǒng)下的目錄進(jìn)行優(yōu)化配置。

舉例

[root@localhost ~]# fsck -Cf /dev/sda7
fsck 1.39 (29-May-2006)
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure                                          
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
TKFDISK: 11/50288 files (9.1%non-contiguous), 7673/100390 blocks 

說明:需要磁盤檢查的分區(qū)不能掛載在系統(tǒng)上,需要先被卸載才能磁盤檢測

大容量磁盤分區(qū)(parted)

由于 fdisk 無法支持到高于2 TB 以上的分區(qū),此時就需要 parted 來處理了

語法:parted [設(shè)備] [命令 [參數(shù)]]
選項與參數(shù):
新增分區(qū):mkpart [primary|logical|extended] [ext3|vfat]開始結(jié)束
分區(qū)表:print
刪除分區(qū):rm [partition]

舉例1:查看分區(qū)表

[root@bogon ~]# parted /dev/sda print

Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  標(biāo)志
 1      32.3kB  107MB   107MB   主分區(qū)    ext3         啟動
 2      107MB   10.6GB  10.5GB  主分區(qū)    ext3             
 3      10.6GB  15.8GB  5240MB  主分區(qū)    ext3             
 4      15.8GB  21.5GB  5634MB  擴(kuò)展分區(qū)                   
 5      15.8GB  16.9GB  1045MB  邏輯分區(qū)  linux-swap       

信息: 如果必要,不要忘記更新 /etc/fstab。  

通過以上信息可以看出,擴(kuò)展分區(qū)到21.5 G,邏輯分區(qū)使用到16.9 G,那么16.9 G~21.5 G只部分空間還為被使用(未被分區(qū))

舉例2:新增分區(qū)

[root@bogon ~]# parted /dev/sda mkpart logical ext3 16.9G 18.9G
信息: 如果必要,不要忘記更新 /etc/fstab。                                 

[root@bogon ~]# parted /dev/sda print

Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  標(biāo)志
 1      32.3kB  107MB   107MB   主分區(qū)    ext3         啟動
 2      107MB   10.6GB  10.5GB  主分區(qū)    ext3             
 3      10.6GB  15.8GB  5240MB  主分區(qū)    ext3             
 4      15.8GB  21.5GB  5634MB  擴(kuò)展分區(qū)                   
 5      15.8GB  16.9GB  1045MB  邏輯分區(qū)  linux-swap       
 6      16.9GB  18.9GB  2023MB  邏輯分區(qū)   

舉例3:刪除分區(qū)

[root@bogon ~]# parted /dev/sda rm 6
信息: 如果必要,不要忘記更新 /etc/fstab。                                 

[root@bogon ~]# parted /dev/sda print

Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  標(biāo)志
 1      32.3kB  107MB   107MB   主分區(qū)    ext3         啟動
 2      107MB   10.6GB  10.5GB  主分區(qū)    ext3             
 3      10.6GB  15.8GB  5240MB  主分區(qū)    ext3             
 4      15.8GB  21.5GB  5634MB  擴(kuò)展分區(qū)                   
 5      15.8GB  16.9GB  1045MB  邏輯分區(qū)  linux-swap       

信息: 如果必要,不要忘記更新 /etc/fstab。 

說明:parted 分區(qū)提交即執(zhí)行,因此使用起來需小心