天天看點

linux下使用fdisk指令進行硬碟分區

添加一塊硬碟

# fdisk -l
Disk /dev/vdb: 53.7 GB, 53687091200 bytes
16 heads, 63 sectors/track, 104025 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000      

用fdisk指令進入分區界面

# fdisk /dev/vdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xb5f18769.
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):      
Command (m for help): n    // 輸入n建立一個分區
Command action
   e   extended    //e是擴充分區
   p   primary partition (1-4)    //p是主分區
p   //選擇p
Partition number (1-4): 1     //分區号
First cylinder (1-104025, default 1):     //預設回車就行
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-104025, default 104025): 25600M  //可以輸入具體要分多少空間給這個分區,這裡我分25G

參考:
          //Supported: 10^N: KB (KiloByte), MB (MegaByte), GB (GigaByte)
            2^N: K  (KibiByte), M (MebiByte), G  (GibiByte)      

分第二個分區

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (52015-104025, default 52015): 
Using default value 52015
Last cylinder, +cylinders or +size{K,M,G} (52015-104025, default 104025):        //這裡直接回車預設所有空間
Using default value 104025      

檢視剛剛分的2個區。可以看到vdb1和vdb2

Command (m for help): p    //輸入p檢視分區
Disk /dev/vdb: 53.7 GB, 53687091200 bytes
16 heads, 63 sectors/track, 104025 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb7a8f045
   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1               1       52014    26215024+  83  Linux
/dev/vdb2           52015      104025    26213544   83  Linux      

儲存分區退出(一定要儲存,不然得重來了。)

Command (m for help): w    //輸入w儲存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.      

檢視分好的分區

# fdisk -l
Disk /dev/vdb: 53.7 GB, 53687091200 bytes
16 heads, 63 sectors/track, 104025 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb7a8f045
   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1               1       52014    26215024+  83  Linux
/dev/vdb2           52015      104025    26213544   83  Linux      

格式化分區

# mkfs.ext3 /dev/vdb1      
# mkfs.ext3 /dev/vdb2      

建立挂載檔案夾

# mkdir /vdb1 /vdb2      

挂載分區到檔案夾

# mount /dev/vdb1 /vdb1
# mount /dev/vdb2 /vdb2      

分區完成,資料寫入挂載的檔案夾就寫入到硬碟了。

但是重新開機之後linux不會自動挂載,需要在/etc/fstab檔案添加後面2條。儲存之後下次重新開機就會自動挂載了。

# vim /etc/fstab
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/vdb1        /vdb1          ext3   defaults        1 2
/dev/vdb2        /vdb2          ext3   defaults        1 2      
[root@myslaver ~]# cat /proc/partitions
major    minor  #blocks name
 252        0   52428800 vda
 252        1     512000 vda1
 252        2    7875584 vda2
 252       16   52428800 vdb
 252       17   26215024 vdb1
 252       18   26213544 vdb2
 253        0    7036928 dm-0
 253        1     835584 dm-1      

繼續閱讀