Linux的硬盘分区
??? 關于硬盤分區,這里先只討論硬盤分區的方法在linux環境下,其他知識以后也會陸續說到
??? 首先我們通過 df 命令查看一下硬盤的使用情況,在安裝硬盤分區后做比較用
[root@bogon ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 16G 2.2G 13G 15% / tmpfs 1012M 444K 1012M 1% /dev/shm /dev/sda1 291M 29M 248M 11% /boot /dev/sr1 2.8G 2.8G 0 100% /media/RHEL_6.0 i386 Disc 1 /dev/sr0 45M 45M 0 100% /media/CDROM?
?我們再通過fdisk命令 查看一下硬盤的分區情況
[root@bogon ~]# fdisk -lDisk /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: 0x0008a5e5Device Boot Start End Blocks Id System /dev/sda1 * 1 39 307200 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 39 2093 16501760 83 Linux /dev/sda3 2093 2611 4161536 82 Linux swap / SolarisDisk /dev/sdb: 16.1 GB, 16106127360 bytes 255 heads, 63 sectors/track, 1958 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: 0x00000000Disk /dev/sdb doesn't contain a valid partition table?
?sda 表示已經完成分區并在使用,sdb表示尚未分區,也就是我們下邊要進行的操作對象,dev表示硬盤
我們使用 fdisk 命令來對 的 sdb來進行分區
[root@bogon ~]# 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 0xe3b900aa. 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 toswitch off the mode (command 'c') and change display units tosectors (command 'u').Command (m for help):?
?下面介紹一下 command (m for help): 常用的參數
Command (m for help): m Command actiona toggle a bootable flagb edit bsd disklabel c toggle the dos compatibility flagd delete a partition /*分區完成后,如果對分區不滿意可以通過此刪除分區,進行重新分區*/l list known partition types /*顯示分區的方式*/m print this menu /*回到這個菜單*/n add a new partition /*建立一個新的分區*/o create a new empty DOS partition tablep print the partition table /*顯示分區列表*/q quit without saving changes /*不保存退出*/s create a new empty Sun disklabel t change a partition's system idu change display/entry unitsv verify the partition tablew write table to disk and exit /*保存退出,分區完成后一定要輸入w,否則一切為空,回到解放前*/x extra functionality (experts only)?我們先創建一個新的分區
Command (m for help): n Command actione extended /*代表擴展分區,但擴展分區不可以直接使用,需要分為邏輯分區后方可使用,邏輯分區從5開始*/p primary partition (1-4) /*代表主分區,硬盤只能有四個主分區,從1-4 擴展分區也算一個主分區,主分區可以直接使用*/?先進行創建主分區
p primary partition (1-4) p Partition number (1-4): 1 /*表示盤符編碼,1-4任意都可以,我們符合習慣從1開始*/ First cylinder (1-1958, default 1): 1 /*此處表示此分區的開始位置,我們假設a,a屬于(1-1958) 可以直接回車,默認為1,我們同樣從1開始,符合習慣*/ Last cylinder, +cylinders or +size{K,M,G} (1-1958, default 1958): +5G /*此處表示此分區的結束位置,我們假設為b b屬于 (a+1 - 1958),此硬盤大小則為 b-a 或者更加便捷直接輸入我們想要的硬盤大小,前加上 "+" 即可*/?到此處第一個主分區已經完成,我們可以通過 參數 p 來查看一下
Command (m for help): pDisk /dev/sdb: 16.1 GB, 16106127360 bytes 255 heads, 63 sectors/track, 1958 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: 0xe3b900aaDevice Boot Start End Blocks Id System /dev/sdb1 1 654 5253223+ 83 Linux開始位置 結束位置 分區大小,單位是byte,小數點向前移動,三位,可以獲得大致大小
可以看到,第一個分區已經完成,下邊我們來分一個擴展分區,同樣 先通過參數 n 創建一個新的分區
Command (m for help): n Command actione extended p primary partition (1-4)此時我們要進行的擴展分區,所以我們輸入 參數? e?
Command (m for help): n Command actione extendedp primary partition (1-4) e Partition number (1-4): 2 First cylinder (655-1958, default 655): 655 /*因為我們第一個分區占用了 (1- 654) ,所以此時默認從655開始 */ Last cylinder, +cylinders or +size{K,M,G} (655-1958, default 1958): +6G此時擴展分區已經分區完成,同樣的我們通過 參數 p來查看一下
Command (m for help): pDisk /dev/sdb: 16.1 GB, 16106127360 bytes 255 heads, 63 sectors/track, 1958 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: 0xe3b900aaDevice Boot Start End Blocks Id System /dev/sdb1 1 654 5253223+ 83 Linux /dev/sdb2 655 1438 6297480 5 Extended /*表示這是一個邏輯分區*/擴展分區是不能直接使用的,我們必須把擴展分區分成邏輯分區后才可以使用,同樣的我們 采用參數 n 創建一個新的分區
Command (m for help): n Command actionl logical (5 or over) /*此時可以發現,此處有Extended 變成了 logical 因為擴展分區只能存在一個,logical表示邏輯分區*/p primary partition (1-4)我們選擇參數 l 分出邏輯分區
l First cylinder (655-1438, default 655): 655 Last cylinder, +cylinders or +size{K,M,G} (655-1438, default 1438): +3G此時邏輯分區已經完成 ,同樣的我們使用參數 p 來查看一下
Command (m for help): pDisk /dev/sdb: 16.1 GB, 16106127360 bytes 255 heads, 63 sectors/track, 1958 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: 0xe3b900aaDevice Boot Start End Blocks Id System /dev/sdb1 1 654 5253223+ 83 Linux /dev/sdb2 655 1438 6297480 5 Extended /dev/sdb5 655 1047 3156741 83 Linux /*邏輯分區從5開始,1-4只能是主分區*/這是分區已經完成,最后一步,參數 w 保存退出
Command (m for help): w The partition table has been altered!Calling ioctl() to re-read partition table. Syncing disks.硬盤完成分區之后,并不能直接使用,需要進行格式化以及掛載才能使用
格式化 我們采用 mkfs命令,簡單介紹一下mkfs命令
mkfs命令用于在設備上(通常為硬盤)創建Linux文件系統。mkfs本身并不執行建立文件系統的工作,而是去調用相關的程序來執行mkfs (選項) (參數)mkfs:指定建立文件系統時的參數;-t<文件系統類型>:指定要建立何種文件系統;-v:顯示版本信息與詳細的使用方法;-V:顯示簡要的使用方法;-c:在制做檔案系統前,檢查該partition是否有壞軌下面我們來進行格式化
[root@bogon ~]# mkfs -t ext4 /dev/sdb1 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 328656 inodes, 1313305 blocks 65665 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1346371584 41 block groups 32768 blocks per group, 32768 fragments per group 8016 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 27 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@bogon ~]# mkfs -t ext4 /dev/sdb2 mke2fs 1.41.12 (17-May-2010) mkfs.ext4: inode_size (128) * inodes_count (0) too big for afilesystem with 0 blocks, specify higher inode_ratio (-i)or lower inode count (-N).[root@bogon ~]# mkfs -t ext4 /dev/sdb5 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 197600 inodes, 789185 blocks 39459 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=809500672 25 block groups 32768 blocks per group, 32768 fragments per group 7904 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 25 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@bogon ~]# mkfs -t ext4 /dev/sdb2格式化完成之后我們來進行硬盤的掛載
[root@bogon ~]# mkdir /tmp1 [root@bogon ~]# mkdir /tmp2 [root@bogon ~]# mount /dev/sdb1 /tmp1 [root@bogon ~]# mount /dev/sdb5 /tmp2此時掛載完畢,我們來通過 df 命令來查看一下我們是否成功
[root@bogon ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 16G 2.2G 13G 15% / tmpfs 1012M 444K 1012M 1% /dev/shm /dev/sda1 291M 29M 248M 11% /boot /dev/sr1 2.8G 2.8G 0 100% /media/RHEL_6.0 i386 Disc 1 /dev/sr0 45M 45M 0 100% /media/CDROM /dev/sdb1 5.0G 139M 4.6G 3% /tmp1 /dev/sdb5 3.0G 69M 2.8G 3% /tmp2可以看到,我們已經完成了硬盤的分區以及格式化和掛載,但是由于linux的特殊性,若我們不把此分區寫入配置文件,我們下次重啟是,此分區是消失的
下面我們通過vim 將其寫入配置文件
[root@bogon ~]# vim /etc/fstab/dev/sdb1 /part1 ext4 defaults 0 0/dev/sdb5 /part1 ext4 defaults 0 0
# # /etc/fstab # Created by anaconda on Wed Mar 7 04:42:44 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 # UUID=93de9377-f280-4fad-959e-bfc88b5ffeff / ext4 defaults 1 1 UUID=d303ecd7-a97e-4dbe-a543-01b8db0362cc /boot ext4 defaults 1 2 UUID=5cb80a30-6c26-47d2-b82d-cef4de688119 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" 17L, 842C 3,1 All
?此時代表完成,輸入wq保存退出即可
到此關于硬盤分區的操作到此結束,其他方面自己學習之后會再進行分享,謝謝大家
文中如有錯誤不足,希望大家多多包涵指正
轉載于:https://www.cnblogs.com/nanlu/p/9003026.html
總結
以上是生活随笔為你收集整理的Linux的硬盘分区的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: cordova 打包工具
- 下一篇: MyBatis中调用存储过程和函数
