linux分區的過程由三步組成:linux
一、將磁盤硬件進行分區centos
二、建立文件系統服務器
三、將目錄掛載到分區上ide
例如咱們將磁盤分區爲sda1,而後將sda1掛載到/usr/local/目錄上,那麼/usr/local/ 目錄的文件將會保存在sda1磁盤分區中。工具
linux全部設備都會以文件方式映射在系統中。通常狀況設備驅動存放於/dev目錄下,因此咱們能夠在dev目錄下找到咱們的設備。磁盤設備主要有如下幾種ui
xvd[*][*] | Xen虛擬磁盤。阿里雲ECS雲盤是這種類型。this
|
sd[*][*] | SCSI 總線磁盤包括,ISCSI、SAS、SATA、SSD、U盤等阿里雲 |
hd[*][*] | IDE 磁盤 |
mmcblk[*][*] | SD卡。 |
設備編碼編碼
上表列舉了幾種磁盤類型,其中後面的第一組[*]表明設備編碼。也就是說一種類型的磁盤可能存在多個,好比說一個主機上添加兩塊硬盤。spa
xvd、sd、hd類型的磁盤設備編碼按照a、b、c、d...的順序排列。列如第一塊硬盤就是sda,第二塊就是sdb以此類推。
mmcblk類型的磁盤設備編碼按照0、一、二、三、4...的順序排列。列如第張SD卡就是mmcblk0,第張SD卡就是mmcblk1以此類推。
分區編碼
linux規定磁盤上最多擁有四個主分區,若是咱們還想擁有更多的分區就要建立擴展分區和邏輯分區。
主分區按照一、二、三、4...的順序排列。例如第一塊硬盤的主分區也就是sda一、sda二、sda三、sda4。邏輯分區一樣使用數字排列從5開始。也就是sda五、sda六、sda7......sda無窮大。
主分區 | 最多隻能有4個。 |
擴展分區 | 最多隻能有一個,而且佔用一個主分區編號。擴展分區並非真實的分區,只是用於包含邏輯分區使用。 |
邏輯分區 | 在擴展分區下建立的實際分區。編號從5開始,不限制數量。 |
特殊的mmcblk類型磁盤的分區編碼形式爲p一、p二、p三、p4...。例如mmcblk0p1
示例
下面提供一個分區示例
咱們可使用 df -h命令查看主機當前分區狀況
[root@centos-rpi3 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/root 2.0G 781M 1.1G 43% / devtmpfs 460M 0 460M 0% /dev tmpfs 464M 0 464M 0% /dev/shm tmpfs 464M 12M 452M 3% /run tmpfs 464M 0 464M 0% /sys/fs/cgroup /dev/mmcblk0p1 500M 45M 455M 9% /boot tmpfs 93M 0 93M 0% /run/user/0
顯然系統默認的分區並無徹底使用個人磁盤容量
下面咱們將使用fdisk工具進行分區
查看目前磁盤分區狀況fdisk -l
[root@centos-rpi3 dev]# fdisk -l Disk /dev/mmcblk0: 31.8 GB, 31812747264 bytes, 62134272 sectors 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 label type: dos Disk identifier: 0x000c3c9b Device Boot Start End Blocks Id System /dev/mmcblk0p1 2048 1026047 512000 c W95 FAT32 (LBA) /dev/mmcblk0p2 1026048 2074623 524288 82 Linux swap / Solaris /dev/mmcblk0p3 2074624 6268927 2097152 83 Linux
使用 fdisk /dev/mmcblk0 命令對磁盤進行分區操做(在這裏咱們可使用m參數查看命令幫助)
[root@centos-rpi3 dev]# fdisk /dev/mmcblk0 Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. 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 g create a new empty GPT partition table G create an IRIX (SGI) partition table 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)
使用 n建立分區
Command (m for help): n Partition type: p primary (3 primary, 0 extended, 1 free) e extended Select (default e): e Selected partition 4 First sector (6268928-62134271, default 6268928): Using default value 6268928 Last sector, +sectors or +size{K,M,G} (6268928-62134271, default 62134271): Using default value 62134271 Partition 4 of type Extended and of size 26.7 GiB is set
因爲主分區已經存在3個了因此咱們建立一個擴展分區,才能容納下更多的分區。咱們建立了一個26.7G大小的擴展分區。
擴展分區不可以存儲數據,因此咱們還要在擴展分區中建立邏輯分區。
Command (m for help): n All primary partitions are in use Adding logical partition 5 First sector (6270976-62134271, default 6270976): Using default value 6270976 Last sector, +sectors or +size{K,M,G} (6270976-62134271, default 62134271): 27242496 Partition 5 of type Linux and of size 10 GiB is set Command (m for help): n All primary partitions are in use Adding logical partition 6 First sector (27244545-62134271, default 27246592): Using default value 27246592 Last sector, +sectors or +size{K,M,G} (27246592-62134271, default 62134271): Using default value 62134271 Partition 6 of type Linux and of size 16.7 GiB is set
咱們建立了兩個邏輯分區5和6分別是10G和16.7G。
結束扇區計算:開始扇區 + 2097152(1G扇區數) * n
使用 p 命令查看當前建立好的分區
Command (m for help): p Disk /dev/mmcblk0: 31.8 GB, 31812747264 bytes, 62134272 sectors 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 label type: dos Disk identifier: 0x000c3c9b Device Boot Start End Blocks Id System /dev/mmcblk0p1 2048 1026047 512000 c W95 FAT32 (LBA) /dev/mmcblk0p2 1026048 2074623 524288 82 Linux swap / Solaris /dev/mmcblk0p3 2074624 6268927 2097152 83 Linux /dev/mmcblk0p4 6268928 62134271 27932672 5 Extended /dev/mmcblk0p5 6270976 27242496 10485760+ 83 Linux /dev/mmcblk0p6 27246592 62134271 17443840 83 Linux
這裏須要注意若是如今退出,分區是沒有建立成功的。還要執行w 命令進行保存並退出。
Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.
從新啓動(reboot)啓用新的分區。
修改分區類型
若是咱們對分區類型有要求,好比移動硬盤但願使用exFAT類型。那麼我麼可使用fdisk修改分區類型。 查看分區類型及對應的編碼 Command (m for help): l 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx 5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data 6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility 8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt 9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi ea Rufus alignment e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT 10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/ 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor 14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto 1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep 1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT 修改分區類型 Command (m for help): t Selected partition 1 Partition type (type L to list all types): 7 Changed type of partition 'HPFS/NTFS/exFAT' to 'HPFS/NTFS/exFAT'.
|
完成分區後咱們還要爲新的分區建立文件系統
linux支持的文件系統有幾十種,下面列舉一些經常使用的文件系統類型。
ext | 專門爲Linux設計的,爲linux核心所作的第一個文件系統。單個文件最大限制:未知;該文件系統最大支持2GB的容量。 |
ext2 | 由Rémy Card設計,用以代替ext,是LINUX內核所用的文件系統。單個文件最大限制2TB;該文件系統最大支持32TB的容量。 |
ext3 | 一個日誌文件系統。單個文件最大限制16TB,該文件系統最大支持32TB的容量。 |
ext4 | Theodore Tso領導的開發團隊實現,Linux系統下的日誌文件系統。單個文件最大限制16TB,該文件系統最大支持1EB的容量。 |
JFS2 | 一種字節級日誌文件系統,該文件系統主要是爲知足服務器的高吞吐量和可靠性需求而設計、開發的。單個文件最大限制16TB,該文件系統最大支持1PB的容量。 |
xfs | XfS文件系統是SGI開發的高級日誌文件系統,XFS極具伸縮性,很是健壯。它能夠支持上百萬T字節的存儲空間。對特大文件及小尺寸文件的支持都表現出衆,支持特大數量的目錄。 |
示例
咱們使用mkfs命令在設備上(一般爲硬盤)建立Linux文件系統。
mkfs自己並不執行創建文件系統的工做,而是去調用相關的程序來執行。
[root@centos-rpi3 dev]# mkfs.xfs /dev/mmcblk0p6 meta-data=/dev/mmcblk0p6 isize=512 agcount=4, agsize=1090240 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=4360960, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
建立好的分區須要掛載到目錄上纔可以使用,存儲到該目錄的文件也就存放在了對應的磁盤分區中。
通常狀況下咱們須要掛載的目錄主要有
/ | 根目錄 是必須單獨分區的,稱之爲跟分區 |
swap | 交換分區,至關於虛擬內存。當內存小的時候(4G如下)能夠份內存的兩倍,當內存大的時候,swap跟內存同樣大就能夠了。 |
/boot | 啓動分區(200MB足以)。不是必須,但強烈建議。若不單獨分區,當磁盤存放文件滿的時候,佔用了啓動空間,系統就起不來了,單獨分區後,其它分區存滿,不會影響此分區的空間。 |
示例
咱們使用mount命令將分區/dev/mmcblk0p6掛載到/self目錄
[root@centos-rpi3 dev]# mount /dev/mmcblk0p6 /self/
最後咱們使用df -h查看磁盤分區時就多了一個17G的分區
[root@centos-rpi3 dev]# df -h Filesystem Size Used Avail Use% Mounted on /dev/root 2.0G 782M 1.1G 43% / devtmpfs 460M 0 460M 0% /dev tmpfs 464M 0 464M 0% /dev/shm tmpfs 464M 12M 452M 3% /run tmpfs 464M 0 464M 0% /sys/fs/cgroup /dev/mmcblk0p1 500M 45M 455M 9% /boot tmpfs 93M 0 93M 0% /run/user/0 /dev/mmcblk0p6 17G 33M 17G 1% /self
這裏了有一個問題mount命令掛載的目錄重啓後就會消失
因此咱們要將掛載配置加入到/etx/fstab文件中才會永久有效
[root@centos-rpi3 self]# vi /etc/fstab #Generated by RootFS Build Factory LABEL=4644C237 /boot vfat noatime 0 0 UUID=0980ad33-0130-4c75-98e0-b03975a1d929 swap swap noatime 0 0 UUID=fe41dacc-3232-43fc-a020-16d3c27b0390 / ext4 noatime 0 0 /dev/mmcblk0p5 /root ext4 defaults 0 0 /dev/mmcblk0p6 /self ext4 defaults 0 0
咱們再重啓一次,查看是否掛載成功