How to add a virtual disk to Linux (CentOS7) on VirtualBox to free up space

I will introduce how to free up space by creating a virtual disk on Linux (CentOS7) on VirtualBox.

By default, CentOS7 manages disks with LVM (Logical Volume Manager). LVM (Logical Volume Manager) is a tool for disk (volume) management, and is a disk management function that can treat a storage area that spans multiple hard disks and partitions as if it were a single disk. (About LVM, "[[Basic knowledge of Linux] What is LVM? Let's understand LVM!](Https://www.pmi-sfbac.org/linux-lvm/" [Basic knowledge of Linux] What is LVM? Let's understand LVM! The explanation on the ")" page is easy to understand.)

There are roughly the following three components of LVM.

here ** 1. Create a virtual disk with VirtualBox ** ** 2. Partitioning ** ** 3. PV (Physical volume) creation ** ** 4. Add the created PV (Physical volume) to VG (Volume group) ** ** 5. Extend existing LV (Logical volume) from VG ** ** 6. File system (xfs) extensions ** Increase the free space under "/" by the method of. (Another method is to create a new LV and mount it on the file system.)

environment

[root@CENTOS7 ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[root@CENTOS7 ~]#

procedure

1. Create a virtual disk with VirtualBox

(1) Start "VirtualBox", select the target OS (here, "CentOS-7-1908"), and click "Settings". 01.png

(2) Select "Storage" and then "Controller: SATA". 02.png

(3) Click the "Add Hard Disk" icon. 00.png

(4) Click "Create". 03.png

(5) Select "VDI (VirtualBox Disk Image)" and click "Next". 04.png

(6) Select "Fixed size" and click "Next". 05.png

(7) Enter "32.00GB" and click "Create". 06.png

Wait for a while until the creation is completed. 07.png

(8) Select the added hard disk and click "Select". 08.png

(9) Confirm that the hard disk has been added to "Controller: SATA" of the storage device, and click "OK". 09.png

(10) Start the target OS (here, "CentOS-7-1908"). 10.png

2. Create partition

You can refer to the list of connected disks with the following command.

lsscsi

Execution result


[root@CENTOS7 ~]# lsscsi
[0:0:0:0]    cd/dvd  VBOX     CD-ROM           1.0   /dev/sr0
[1:0:0:0]    cd/dvd  VBOX     CD-ROM           1.0   /dev/sr1
[2:0:0:0]    disk    ATA      VBOX HARDDISK    1.0   /dev/sda
[3:0:0:0]    disk    ATA      VBOX HARDDISK    1.0   /dev/sdb
[root@CENTOS7 ~]#

Browse to the recognized disk with the following command.

ls -l /dev/sd*

Execution result


[root@CENTOS7 ~]# ls -l /dev/sd*
brw-rw---- 1 root disk 8,0 September 29 22:22 /dev/sda
brw-rw---- 1 root disk 8,1 September 29 22:22 /dev/sda1
brw-rw---- 1 root disk 8,2 September 29 22:22 /dev/sda2
brw-rw---- 1 root disk 8,16 September 29 22:22 /dev/sdb
[root@CENTOS7 ~]#

Add a new partition to "/ dev / sdb" with the fdisk command.

fdisk /dev/sdb

Execution result


[root@CENTOS7 ~]# fdisk /dev/sdb
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x32eaedf2.

command(Help with m):

Enter m for help.

Execution result


command(Help with m): m
Command behavior
   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)

command(Help with m):

Enter p to see the current partition.

Execution result


command(Help with m): p

Disk /dev/sdb: 34.4 GB, 34359738368 bytes, 67108864 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size(minimum/Recommendation):512 bytes/512 bytes
Disk label type: dos
Disc identifier: 0x32eaedf2

Device boot start / end block Id system

command(Help with m):

There is no partition because it is in the initial state.

Enter n to create a new partition.

Execution result


command(Help with m): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):

Enter p to create the primary partition.

Execution result


Select (default p): p
Partition number(1-4, default 1):

Enter the default 1 for the partition number.

Execution result


Partition number(1-4, default 1): 1
First sector(2048-67108863,Initial value 2048):

Enter 2048 for the first sector.

Execution result


First sector(2048-67108863,Initial value 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-67108863,Initial value 67108863):

Enter the full 67108863 in the Last sector.

Execution result


Last sector, +sectors or +size{K,M,G} (2048-67108863,Initial value 67108863): 67108863
Partition 1 of type Linux and of size 32 GiB is set

command(Help with m):

Enter p to check the partition again.

Execution result


command(Help with m): p

Disk /dev/sdb: 34.4 GB, 34359738368 bytes, 67108864 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size(minimum/Recommendation):512 bytes/512 bytes
Disk label type: dos
Disc identifier: 0x32eaedf2

Device boot start / end block Id system
/dev/sdb1            2048    67108863    33553408   83  Linux

command(Help with m):

Make sure that a new partition "/ dev / sdb1" has been created.

Enter w to write the partition.

Execution result


command(Help with m): w
The partition table has changed!

ioctl()To reload the partition table.
The disks are syncing.
[root@CENTOS7 ~]#

Make sure that "/ dev / sdb1" is added to the file system.

ls -l /dev/sd*

Execution result


[root@CENTOS7 ~]# ls -l /dev/sd*
brw-rw---- 1 root disk 8,0 September 29 22:22 /dev/sda
brw-rw---- 1 root disk 8,1 September 29 22:22 /dev/sda1
brw-rw---- 1 root disk 8,2 September 29 22:22 /dev/sda2
brw-rw---- 1 root disk 8,16 September 29 23:46 /dev/sdb
brw-rw---- 1 root disk 8,17 September 29 23:46 /dev/sdb1
[root@CENTOS7 ~]#

3. PV (Physical volume) creation

Create a physical volume for partition "/ dev / sdb1" with the pvcreate command.

pvcreate /dev/sdb1

Execution result


[root@CENTOS7 ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
[root@CENTOS7 ~]#

Confirm that PV (Physical volume) is created by using the pvdisplay command.

pvdisplay

Execution result


[root@CENTOS7 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <15.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              3839
  Free PE               0
  Allocated PE          3839
  PV UUID               bKwnMo-BTE9-BWlx-iXu2-cC6H-Dj0r-8bOowr

  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               centos
  PV Size               <32.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              8191
  Free PE               0
  Allocated PE          8191
  PV UUID               874u5U-1pDD-OpBx-dswz-n4fa-l05i-B4Cf2K

[root@CENTOS7 ~]#

You can see that "/ dev / sdb1" has been created.

4. Add the created PV (Physical volume) to VG (Volume group)

Check the name (VG Name) of the VG (Volume group) with the vgdisplay command.

vgdisplay

Execution result


[root@CENTOS7 ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <15.00 GiB
  PE Size               4.00 MiB
  Total PE              3839
  Alloc PE / Size       3839 / <15.00 GiB
  Free  PE / Size       0 / 0
  VG UUID               DpKrR4-ksQz-HctA-1NeF-SaVx-7bm6-FcC4Di

[root@CENTOS7 ~]#

I found that the VG Name is "centos".

Add PV (/ dev / sdb1) to VG (centos) with the vgextend command.

vgextend centos /dev/sdb1

Execution result


[root@CENTOS7 ~]# vgextend centos /dev/sdb1
  Volume group "centos" successfully extended
[root@CENTOS7 ~]#

Check the VG again with the vgdisplay command.

Execution result


[root@CENTOS7 ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               46.99 GiB
  PE Size               4.00 MiB
  Total PE              12030
  Alloc PE / Size       3839 / <15.00 GiB
  Free  PE / Size       8191 / <32.00 GiB
  VG UUID               DpKrR4-ksQz-HctA-1NeF-SaVx-7bm6-FcC4Di

[root@CENTOS7 ~]#

Notice that the VG Size has increased to "46.99 GiB".

5. Extend existing LV (Logical volume) from VG

Check the current "LV Path", "LV Name", and "LV Size" with the lvdisplay command.

lvdisplay

Execution result


[root@CENTOS7 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                hzKez7-P7zQ-eGFj-ByQd-6I9z-Z8Fv-EXBirC
  LV Write Access        read/write
  LV Creation host, time centos7, 2019-10-12 17:03:32 +0900
  LV Status              available
  # open                 2
  LV Size                1.60 GiB
  Current LE             410
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                ZCpYbq-N8Jm-Wlei-iN5X-TVYs-CEFc-XL9XvV
  LV Write Access        read/write
  LV Creation host, time centos7, 2019-10-12 17:03:32 +0900
  LV Status              available
  # open                 1
  LV Size                13.39 GiB
  Current LE             3429
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

[root@CENTOS7 ~]#

Extend the root LV (LV Path: / dev / centos / root, LV Name: root) with the lvextend command.

lvextend -l +100%FREE /dev/centos/root

Execution result


[root@CENTOS7 ~]# lvextend -l +100%FREE /dev/centos/root
  Size of logical volume centos/root changed from 13.39 GiB (3429 extents) to 45.39 GiB (11620 extents).
  Logical volume centos/root successfully resized.
[root@CENTOS7 ~]#

Check the LV again with the lvdisplay command.

Execution result


[root@CENTOS7 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                hzKez7-P7zQ-eGFj-ByQd-6I9z-Z8Fv-EXBirC
  LV Write Access        read/write
  LV Creation host, time centos7, 2019-10-12 17:03:32 +0900
  LV Status              available
  # open                 2
  LV Size                1.60 GiB
  Current LE             410
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                ZCpYbq-N8Jm-Wlei-iN5X-TVYs-CEFc-XL9XvV
  LV Write Access        read/write
  LV Creation host, time centos7, 2019-10-12 17:03:32 +0900
  LV Status              available
  # open                 1
  LV Size                45.39 GiB
  Current LE             11620
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

[root@CENTOS7 ~]#

The "LV Size" of "root" has increased to 45.39 GiB.

6. File system (xfs) extension

Check the file system type with df -T.

df -T

Execution result


[root@CENTOS7 ~]# df -T
Filesys type 1K-Block can be used can be used%Mount position
devtmpfs                devtmpfs      490768        0  490768    0% /dev
tmpfs                   tmpfs         507380        0  507380    0% /dev/shm
tmpfs                   tmpfs         507380     7588  499792    2% /run
tmpfs                   tmpfs         507380        0  507380    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs         14034944 12479804 1555140   89% /
/dev/sda1               xfs          1038336   218388  819948   22% /boot
tmpfs                   tmpfs         101480       12  101468    1% /run/user/42
tmpfs                   tmpfs         101480        0  101480    0% /run/user/0
[root@CENTOS7 ~]#

Use the xfs_growfs command to extend the file system (xfs) with a mount point of" / ".

xfs_growfs /

Execution result


[root@CENTOS7 ~]# xfs_growfs /
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=877824 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=3511296, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 3511296 to 11898880
[root@CENTOS7 ~]#

Check the file system type again with df -T.

Execution result


[root@CENTOS7 ~]# df -T
Filesys type 1K-Block can be used can be used%Mount position
devtmpfs                devtmpfs      490768        0   490768    0% /dev
tmpfs                   tmpfs         507380        0   507380    0% /dev/shm
tmpfs                   tmpfs         507380     7588   499792    2% /run
tmpfs                   tmpfs         507380        0   507380    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs         47585280 12480312 35104968   27% /
/dev/sda1               xfs          1038336   218388   819948   22% /boot
tmpfs                   tmpfs         101480       12   101468    1% /run/user/42
tmpfs                   tmpfs         101480        0   101480    0% /run/user/0
[root@CENTOS7 ~]#

The size of the mount point "/" has increased and the utilization has decreased.

reference

[Basic knowledge of Linux] What is LVM? Understand LVM! Try adding HDD & expanding partition using LVM of CentOS7. (Memo)


that's all

Recommended Posts

How to add a virtual disk to Linux (CentOS7) on VirtualBox to free up space
Steps to set up a VNC server on CentOS 8.3
Build a CentOS 8 virtual environment on your Mac with VirtualBox
How to install GNOME as a desktop environment on CentOS 7
How to add columns to a table
How to deploy Laravel on CentOS 7
How to install MariaDB 10.4 on CentOS 8
[Xcode] How to add a README.md file
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
How to add a new hash / array
How to install beta php8.0 on CentOS8
How to install kafkacat on Amazon Linux2
How to share on the host side (windows) and guest side (CentOS 7) with VirtualBox
How to deploy a container on AWS Lambda
How to add / remove Ruby on Rails columns
How to add a classpath in Spring Boot
How to build a Pytorch environment on Ubuntu
Set up a CentOS virtual server with Vagrant
How to build a Jenkins server with a Docker container on CentOS 7 of VirtualBox and access the Jenkins server from a local PC
Adding a network device to CentOS 8 on Hyper-V does not automatically create a virtual network interface
How to make JavaScript work on a specific page
Steps to set up Postfix and Dovecot on CentOS 8.3
[Java] How to execute tasks on a regular basis
Install MySQL 5.6 on CentOS6 [How to specify the version]
How to deploy a simple Java Servlet app on Heroku
How to get inside a container running on AWS Fargate
How to Burning a Install Disk of Windows from Ubuntu
How to deploy a kotlin (java) app on AWS fargate
How to display a graph in Ruby on Rails (LazyHighChart)
How to add the same Indexes in a nested array
How to set up a proxy with authentication in Feign
How to deploy a Rails application on AWS (article summary)