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
咱们再重启一次,查看是否挂载成功