[root@localhost ~]#fdisk -l #查看设备使用状况 [root@localhost ~]#fdisk /dev/sda Command (m forhelp): 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 adda 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): n #创建一个新的分区 Partition type: p primary (2 primary, 0 extended, 2 free) #创建一个主分区 e extended #创建一个扩展分区 Select (default p): e Partition number (3,4,default 3): #指定分区号 First sector(26208256-41943039, default 26208256): #指定开始柱面,默认回车就可以 Using default value26208256 Last sector, +sectorsor +size{K,M,G} (26208256-41943039, default 41943039): +1G #指定结束柱面,默认不指定的话是把剩下的所有空间分配给分区,等于说指定分区大小 Partition 3 of typeExtended and of size 5 GiB is set Command (m for help): p #打印分区表 Disk /dev/sda: 21.5 GB,21474836480 bytes, 41943040 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:0x000a705c Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 26208255 12591104 8e Linux LVM /dev/sda3 26208256 36694015 5242880 5 Extended #表明是扩展 分区 Command (m for help): n Partition type: p primary (2 primary, 1 extended, 1 free) l logical (numbered from 5) #创建一个逻辑分区 Select (default p): l Adding logicalpartition 5 First sector(26210304-36694015, default 26210304): Using default value26210304 Last sector, +sectorsor +size{K,M,G} (26210304-36694015, default 36694015): +1G Partition 5 of typeLinux and of size 1 GiB is set Command (m for help): p Disk /dev/sda: 21.5 GB,21474836480 bytes, 41943040 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:0x000a705c Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 26208255 12591104 8e Linux LVM /dev/sda3 26208256 36694015 5242880 5 Extended /dev/sda5 26210304 28307455 1048576 83 Linux #逻辑分区的分区号从5开始,不管之前有没有第四或者第三个分区 Command (m for help): w #保存 The partition table hasbeen altered! Calling ioctl() tore-read partition table. WARNING: Re-reading thepartition table failed with error 16: Device or resource busy. The kernelstill uses the old table. The new table will be used at the nextreboot or after you run partprobe(8) or kpartx(8) Syncingdisks.
[root@localhost ~]# parted --help 或 [root@localhost ~]# parted GNU Parted 3.1 Using /dev/sda Welcome to GNU Parted!Type 'help' to view a list of commands. (parted) help (parted) quit #退出
[root@localhost ~]#parted /dev/sdb GNU Parted 3.1 Using /dev/sdb Welcome to GNU Parted!Type 'help' to view a list of commands. (parted) p #查看磁盘分区状态 Error: /dev/sdb:unrecognised disk label (parted) mklabel #指定创建分区表类型为GPT New disk label type? `gpt` (parted) mkpart #创建分区 Partition name? []? `mydisk1` File system type? [ext2]? #指定分区文件系统类型,默认就可以,因为后期对分区进行格式化的时候,同样可以指定 Start? `1` #指定分区起始位置 End? `100M` #指定分区结束位置 (parted) `p` #查看磁盘分区状态 Number Start End Size File system Name Flags 1 1049kB 99.6MB 98.6MB mydisk1 (parted) `mkpart` Partition name? []? `2` File system type? [ext2]? Start? `100M` End? `200M` (parted) `p` Model: VMware, VMwareVirtual S (scsi) Disk /dev/sdb: 21.5GB Sector size(logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 99.6MB 98.6MB mydisk1 2 99.6MB 200MB 101MB 2 (parted) `quit` Information: You mayneed to update /etc/fstab.
重新获取分区表
1 2 3 4
[root@localhost ~]#partprobe /dev/sda
[root@localhost ~]# ls /dev/sda* /dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sda5
[root@localhost ~]#umount /dev/sda5 umount: /sda5: targetis busy. (In some cases useful info aboutprocesses that use the device is found by lsof(8) or fuser(1)) [root@localhost ~]#lsof /sda5/ COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODENAME bash 3967 root cwd DIR 8,5 6 128 /sda5 [root@localhost ~]#kill -9 3967 [root@localhost ~]#fuser -m -u -v /sda5/ USER PID ACCESS COMMAND /sda5: root kernel mount (root)/sda5 root 36749 ..c.. (root)bash [root@localhost ~]#kill -9 36749 [root@localhost ~]#umount /dev/sda5
扩展swap
当swap分区空间不够时, 可以通过以下方式扩展swap大小。
新建swap 分区
1 2 3 4
#格式化sdb1分区为swap格式 [root@localhost ~]# mkswap /dev/sdb1 Setting up swapspaceversion 1, size = 96252 KiB no label,UUID=d6e96d3b-9d74-4eda-8bdf-387754dca90e
查看虚拟内存大小
1 2 3 4
[root@localhost ~]#free -m total used free shared buff/cache available Mem: 3939 517 2942 9 479 3191 Swap: 2047 0 2047
启动swap分区
1
[root@localhost ~]#swapon /dev/sdb1
再次查看
1 2 3 4
[root@localhost ~]#free -m total used free shared buff/cache available Mem: 3939 516 2942 9 479 3191 Swap: 2141 0 2141
实现开机自动挂载swap分区
1 2
[root@localhost~]# vim /etc/fstab /dev/sdb1 swap swap defaults 0 0