1 Introduction
When doing development, there are many virtual machines in use, and often encounter such problems: the disk space that was created when the virtual machine was created is relatively small, and the space occupied by more and more software installed with the virtual machine is also more and more Large, resulting in less and less disk space in the virtual machine, or even not enough. At this point, we can expand the disk size of the virtual machine . Simply put, we add another disk to the virtual machine.
2. Virtual machine expansion steps
The Linux system operates the disk through the fdisk command.
2.1 Current virtual machine disk usage
# fdisk -l
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: dos
Disk identifier: 0xb7a127db
Device Boot Start End Sectors Size Id Type 第一块磁盘分为三个扇区
/dev/sda1 * 2048 207620095 207618048 99G 83 Linux
/dev/sda2 207622142 209713151 2091010 1021M 5 Extended
/dev/sda5 207622144 209713151 2091008 1021M 82 Linux swap / Solaris
Disk /dev/sdb: 300 GiB, 322122547200 bytes, 629145600 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
Disklabel type: dos
Disk identifier: 0x9b2ba102
Device Boot Start End Sectors Size Id Type 第二块磁盘只有一个扇区
/dev/sdb1 2048 629145599 629143552 300G 83 Linux
#
It can be seen from the above information that the current virtual machine contains two disks, one is 100G and the other is 300G, and each disk is divided into different numbers of sectors (units). We can see this information in the "Virtual Machine Settings" on VMware. The two disks here are what we see in the virtual machine: "hard disk" = "/ dev / sda", "hard disk 2" = "/ "dev / sdb"
but no free partition can be used, so first need to allocate a certain amount of disk space to the virtual machine on the physical machine system . That is, add another "disk 3" in the figure above, so that an unused disk appears in the virtual machine, and we operate on the newly allocated disk.
2.2 Allocating disk space for virtual machines
Add a new disk to the VMware software on the physical machine (physical machine, host): all the way to 'next' can be
created, you can see the newly created disk information on the page (the disk size I created is 20G):
- The disk has been created, the next steps are to operate in the virtual machine
2.3 Current virtual machine disk usage
After the above steps, a new disk is created on the Windows physical machine. In order to operate the disk, you first need to check the latest disk allocation and usage:
# fdisk -l
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: dos
Disk identifier: 0xb7a127db
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 207620095 207618048 99G 83 Linux
/dev/sda2 207622142 209713151 2091010 1021M 5 Extended
/dev/sda5 207622144 209713151 2091008 1021M 82 Linux swap / Solaris
Disk /dev/sdb: 300 GiB, 322122547200 bytes, 629145600 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
Disklabel type: dos
Disk identifier: 0x9b2ba102
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 629145599 629143552 300G 83 Linux
Disk /dev/sdc: 20 GiB, 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
#
Compared with the viewing results at 2.1, there is an additional disk ** / dev / sdc ** with a size of 20G, and this disk is the newly allocated disk at 2.2. But it is not yet available, and it needs to be followed up for use.
- The follow-up operations are all newly allocated disks. My name here is / dev / sdc. Some virtual machines may only have the original one disk, so the newly created one may be / dev / sdb; A disk, then it may be / dev / sdd. In other words, this is just the disk name, and determine the disk name for subsequent operations based on the query results of your virtual machine .
2.3 Create partition
The disk / dev / sdc already exists, but it is not yet available. We use the fdisk command to create a partition on / dev / sdc:
Although it seems that there is a lot of information, in fact, you only need to enter a few letters: 'm', 'p', 'n', 'p', '1', 'w'. In addition, there are several commands with default values. If you do not need to view the help information, you only need to enter ['n': create partition], ['w': write to disk and push out] two letters, the rest Use the Enter key system to use the default values to create partitions
# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x5063a350.
Command (m for help): m 【m用来查看有哪些命令】
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
Command (m for help): p 【查询当前磁盘分区情况】
Disk /dev/sdc: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x5063a350
Command (m for help): n 【创建一个新分区】
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p 【分区类型默认主分区】
Partition number (1-4, default 1): 1 【分区编号】
First sector (2048-41943039, default 2048): 【分区大小】
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039):
Created a new partition 1 of type 'Linux' and of size 20 GiB.
Command (m for help): w 【写入磁盘并推出】
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
# 已经退出fdisk命令
2.4 Check whether the partition is created successfully
To view partition information, still use the command fdisk:
# fdisk -l
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: dos
Disk identifier: 0xb7a127db
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 207620095 207618048 99G 83 Linux
/dev/sda2 207622142 209713151 2091010 1021M 5 Extended
/dev/sda5 207622144 209713151 2091008 1021M 82 Linux swap / Solaris
Disk /dev/sdb: 300 GiB, 322122547200 bytes, 629145600 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
Disklabel type: dos
Disk identifier: 0x9b2ba102
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 629145599 629143552 300G 83 Linux
Disk /dev/sdc: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x5063a350
Device Boot Start End Sectors Size Id Type
/dev/sdc1 2048 41943039 41940992 20G 83 Linux
#
From the last line of information, we can see that / dev / sdc1 has been created and the size is exactly 20G.
2.5 Format partition
After creating the disk partition ** / dev / sdc1 , the partition cannot be used immediately. You need to use mkfs to format the file system of the partition / dev / sdc1 **: personal use is the ext4 file system
# mkfs.ext4 /dev/sdc1
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 5242624 4k blocks and 1310720 inodes
Filesystem UUID: 1cf52965-51df-451e-ae80-404cdf6a1568
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
#
2.6 Mounting the file system
After the partition is formatted, it can be used, but when using it, we need to mount the partition under a certain file (" everything is a file "), so that we can put the required files and so on in this directory The file is put on the new disk. Hang the command as follows:
# mkdir /sunzd
# mount /dev/sdc1 /sunzd
Through the above command, the disk / dev / sdc1 will be mounted to the / sunzd directory. The normal way is to put the above hang command in the system initialization script, so that every time the virtual machine starts, it will automatically hang on the disk to the specified directory.
From now on, the operation of the directory ** / sunzd ** is to operate the disk ** / dev / sdc1 **.
I put the mount command in the ** / etc / rc.local ** script:
root@book-virtual-machine:/home/book# cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
ifconfig ens33 192.168.1.54
mount /dev/sdc1 /home/sunzd/
exit 0
2.7 Checking the status of partitions
Use the df command to view the current disk hanging status:
root@book-virtual-machine:/home/book# df -l
Filesystem 1K-blocks Used Available Use% Mounted on
udev 1977632 0 1977632 0% /dev
tmpfs 401628 6380 395248 2% /run
/dev/sda1 102049120 7139108 89703180 8% /
tmpfs 2008124 216 2007908 1% /dev/shm
tmpfs 5120 4 5116 1% /run/lock
tmpfs 2008124 0 2008124 0% /sys/fs/cgroup
/dev/sdb1 309505024 951480 292808572 1% /work
vmhgfs-fuse 131853484 113761468 18092016 87% /mnt/hgfs
tmpfs 401628 68 401560 1% /run/user/1000
/dev/sdc1 20510332 44992 19400432 1% /home/sunzd
#