1. 程式人生 > >Linux學習之 fdisk分割槽

Linux學習之 fdisk分割槽

1、新增新硬碟用於練習分割槽

操作注意:

1)先給Linux虛擬機器斷電

2)虛擬機器設定選項,選擇新增硬碟,完成後,重啟,

3)查詢當前配置

[[email protected] ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e9453

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2611    20458496   8e  Linux LVM

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/VolGroup-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/VolGroup-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

如上,發現新增的硬碟被識別了

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

3、使用fdisk 命令分割槽

[[email protected]

~]# fdisk  /dev/sdb

注意fdisk互動指令說明   

fdisk互動指令說明
命令 說明
a 設定可引導標記
b 編輯bsd磁碟標籤
c 設定DOS作業系統相容標記
d 刪除一個分割槽
l 顯示已知的檔案系統型別。82為Linux swap分割槽,83為Linux分割槽
m 顯示幫助選單
n 新建分割槽
o 建立空白DOS分割槽表
p 顯示分割槽列表
q 不儲存退出
s 新建空白SUN磁碟標籤
t 改變一個分割槽的系統ID
u 改變顯示記錄單位
v 驗證分割槽表
w 儲存退出
x 附加功能(僅專家)
[[email protected] ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x4aac79af.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

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): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4aac79af

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): 

如下,我們決定採取如下分割槽策略要求:

     a、一個主分割槽,2G空間;

    b、 一個擴充套件分割槽,剩餘所有空間;

     c、在擴充套件分割槽中,分配第一個邏輯分割槽,分2G空間;

分割槽步驟如下所示:

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): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4aac79af

   Device Boot      Start         End      Blocks   Id  System

#檢視當前分割槽號列表
Command (m for help): l

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data    
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1

#建立一個分割槽型別
Command (m for help): n
#提示選擇分割槽型別:主分割槽還是擴充套件分割槽
Command action
   e   extended
   p   primary partition (1-4)

#根據提示輸入p,表示建立一個主分割槽
p

#該主分割槽的編號,建議按照順序來
Partition number (1-4): 1

#表示把該硬碟分成1到1305塊,從哪一個資料塊開始,預設從1開始,此處建議從1開始
First cylinder (1-1305, default 1): 

#提示表示第一個主分割槽建立完成
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +2G   

#繼續輸命令,此處繼續建立一個分割槽
Command (m for help): n    
Command action
   e   extended
   p   primary partition (1-4)

#根據提示,選擇建立一個擴充套件分割槽
e

#因為主分割槽和擴充套件分割槽一共四個,已經建立了一個主分割槽,此處就依次序,選2
Partition number (1-4): 2

#表示目前擴充套件分割槽的可選空間從263開始,預設為263,選擇預設(不建議斷開設定)
First cylinder (263-1305, default 263): 

#提示表示一個擴充套件分割槽建立完成
Using default value 263
Last cylinder, +cylinders or +size{K,M,G} (263-1305, default 1305): 
Using default value 1305

#查詢下當前分割槽狀態
Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4aac79af

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux
/dev/sdb2             263        1305     8377897+   5  Extended

#如上,符合我們的建立目標

#在此,在擴充套件分割槽中,試著建立一個邏輯分割槽(因為擴充套件分割槽內無法寫人檔案,但可以在#擴充套件分割槽中建立邏輯分割槽來實現分割槽寫入資料的目的)
Command (m for help): n

#提示要建立一個邏輯分割槽還是主分割槽
Command action
   l   logical (5 or over)
   p   primary partition (1-4)

#選擇l表示要建立一個邏輯分割槽
l

#如下,同上,選擇預設
First cylinder (263-1305, default 263): 

#提示邏輯分割槽建立完成
Using default value 263
Last cylinder, +cylinders or +size{K,M,G} (263-1305, default 1305): +2G

#查詢當前磁碟分割槽狀況
Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4aac79af

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux
/dev/sdb2             263        1305     8377897+   5  Extended
/dev/sdb5             263         524     2104483+  83  Linux

#注意,只有儲存,更改才有效果
Command (m for help): w

#如下提示表示修改並儲存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] ~]# 

4、重新讀取分割槽表資訊

[[email protected] ~] # partprobe

[[email protected] ~]# partprobe
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (裝置或資源忙).  As a result, it may not reflect all of your changes until after reboot.
Warning: 無法以讀寫方式開啟 /dev/sr0 (只讀檔案系統)。/dev/sr0 已按照只讀方式開啟。
Warning: 無法以讀寫方式開啟 /dev/sr0 (只讀檔案系統)。/dev/sr0 已按照只讀方式開啟。
Error: 無法讓分割槽超出磁碟!
[[email protected] ~]# 

注意:如果上方儲存退出時,正常,表示一切成功,如果出現錯誤,因為分割槽表被佔用(比如分割槽完成,儲存了兩次),提示要重啟系統,這時,可以通過該命令,代替重啟系統。如上雖然有警告,但屬於正常提示,分割槽正常完成,只是光碟等不存在。

5、格式化分割槽

[[email protected] ~ ]# mkfs -t ext4  /dev/sdb1

示例:

在完成上方後,通過fdisk -l  命令查詢下當前分割槽格式化

[[email protected] ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e9453

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2611    20458496   8e  Linux LVM

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4aac79af

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux
/dev/sdb2             263        1305     8377897+   5  Extended
/dev/sdb5             263         524     2104483+  83  Linux

Disk /dev/mapper/VolGroup-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/VolGroup-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

[[email protected] ~]# 

注意如上,sdb2 是擴充套件分割槽,不能格式化,也不能寫入資料!!!

現在格式化sdb1  和sdb5

[[email protected] ~]# mkfs -t ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
檔案系統標籤=
作業系統:Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131648 inodes, 526120 blocks
26306 blocks (5.00%) reserved for the super user
第一個資料塊=0
Maximum filesystem blocks=541065216
17 block groups
32768 blocks per group, 32768 fragments per group
7744 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

正在寫入inode表: 完成                            
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[[email protected] ~]# mkfs -t ext4 /dev/sdb5
mke2fs 1.41.12 (17-May-2010)
檔案系統標籤=
作業系統:Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131648 inodes, 526120 blocks
26306 blocks (5.00%) reserved for the super user
第一個資料塊=0
Maximum filesystem blocks=541065216
17 block groups
32768 blocks per group, 32768 fragments per group
7744 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

正在寫入inode表: 完成                            
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[[email protected] ~]# 

如上,沒有報錯,表示格式化完成

6、建立掛載點並掛載

[[email protected] ~]#mkdir /disk1

[[email protected] ~]#mount  /dev/sdb1   /disk1/

[[email protected] ~]# mkdir /disk1
[[email protected] ~]# mkdir /disk5
[[email protected] ~]# mount /dev/sdb1 /disk1
[[email protected] ~]# mount /dev/sdb5 /disk5
[[email protected] ~]# mount
/dev/mapper/VolGroup-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/sr0 on /media/Ubuntu 14.04.5 LTS amd64 type iso9660 (ro,nosuid,nodev,uhelper=udisks,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500)
/dev/sdb1 on /disk1 type ext4 (rw)
/dev/sdb5 on /disk5 type ext4 (rw)
[[email protected] ~]# df
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      17938864 8059304   8961648  48% /
tmpfs                   953092      76    953016   1% /dev/shm
/dev/sda1               487652   41364    420688   9% /boot
/dev/sr0               1078176 1078176         0 100% /media/Ubuntu 14.04.5 LTS amd64
/dev/sdb1              2005848    3096   1897528   1% /disk1
/dev/sdb5              2005848    3096   1897528   1% /disk5

如上,掛載完成並通過檢查

但是,注意如上方法,只能臨時生效,每次重啟系統需要重新掛載,那如何長久生效呢?

二、fdisk分割槽——自動掛載與fstab檔案修復

1、自動掛載(一般比如硬碟、boot  mnt分割槽等等):

 實質就是在/etc/fstab檔案中完成自動掛載配置:

》第一欄位:分割槽裝置檔名或UUID(硬碟通用唯一識別碼)

》第二欄位:掛載點

》第三欄位:檔案系統名稱

》第四欄位:掛載引數

》第五欄位:指定分割槽是否被dump備份,0代表不備份,1代表每天備份,2代表不定期備份,備份就是分割槽自動備份內容到lost-found 檔案;

》第六欄位:指定分割槽是否被fsck檢測,0代表不檢測,其他數字代表檢測的優先順序,那麼當然1的優先順序比2高,系統會先檢測根目錄,個人建立的分割槽,

一般應該為2

注意:該檔案一旦出錯,系統可能無法啟動,則需要fsck自動修復

[[email protected] ~]# vim /etc/fstab 


#
# /etc/fstab
# Created by anaconda on Sun Nov 25 22:51:34 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=ae089b3b-ead9-401d-b17f-80d4575f207f /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
~                                                                                       
                                                            
~                                                                                       
~                                                                                       
~                                                                                       
"/etc/fstab" 15L, 779C                                                1,0-

1)如何檢視分割槽的UUID?

使用如下命令檢視:

[[email protected] ~]# dumpe2fs  -h  裝置檔名            (-h表示只檢視超級塊兒資訊) 

示例:如下,裡面會有UUID選項和值

[[email protected] ~]# dumpe2fs -h /dev/sdb1
dumpe2fs 1.41.12 (17-May-2010)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          042f7bd6-d557-4137-b881-02b0b0cb14d6
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash 
Default mount options:    (none)
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              131648
Block count:              526120
Reserved block count:     26306
Free blocks:              500688
Free inodes:              131637
First block:              0
Block size:               4096
Fragment size:            4096
Reserved GDT blocks:      128
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         7744
Inode blocks per group:   484
Flex block group size:    16
Filesystem created:       Mon Dec 10 08:02:59 2018
Last mount time:          Mon Dec 10 08:08:40 2018
Last write time:          Mon Dec 10 08:08:40 2018
Mount count:              1
Maximum mount count:      32
Last checked:             Mon Dec 10 08:02:59 2018
Check interval:           15552000 (6 months)
Next check after:         Sat Jun  8 08:02:59 2019
Lifetime writes:          97 MB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:	          256
Required extra isize:     28
Desired extra isize:      28
Journal inode:            8
Default directory hash:   half_md4
Directory Hash Seed:      9f7752db-881a-4005-9985-1cf59fd4c88b
Journal backup:           inode blocks
Journal features:         (none)
日誌大小:             64M
Journal length:           16384
Journal sequence:         0x00000001
Journal start:            0

[[email protected] ~]# 

 

2、分割槽自動掛載

[[email protected] ~] #vi /etc/fstab

省略部分輸出

/dev/sdb1               /disk1                  ext4    defaults        1 2

寫這個檔案時特別注意,千萬別寫錯

所以,不要先重啟,應該先測試是否有問題,方法:

mount -a

如果回車無錯誤提示,表示修改正確

[[email protected] ~]# mount -a
[[email protected] ~]# 
如上,表示正確

故意出錯修改示例:

  1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sdb                /disk1                  ext4    defaults        1 2
/dev/sdb5               /disk5                  ext4    defaults        1 2
~                                                                          

如上,故意將disk1對應的裝置檔名寫錯,然後儲存

之後重啟虛擬機器,可以通過命令 reboot,檢視虛擬機器

提示輸入root密碼,根據提示進入,發現無法修改,但有提示錯誤在哪一行

3、/etc/fstab 檔案修復

根據如上提示,我們可以通過如下方式進行

重新掛載根分割槽,賦予讀寫許可權

[[email protected] ~] mount -o remount.rw /

然後再vim  /etc/fstab 命令開啟,修改正確,然後儲存退出

再reboot命令驗證,應該是正確的。

但特別注意:該修復不是萬能的,如果連根分割槽都錯了,無法掛載根分割槽,無法出現

如上錯誤提示資訊,就真的崩潰了,所以,修改該檔案特別要小心!!!!!!!!!!!!!!!!!