KVM : Management tools for VM2021/05/10 |
|
Install useful tools for virtual machine management.
|
|
| [1] | Install required packages. |
|
root@dlp:~# apt -y install libguestfs-tools virt-top
|
| [2] | Get official OS image and Create a Virtual Machine. ( If you'd like to do from OS installation, refer to here of [1] ) |
|
# display available OS template root@dlp:~# virt-builder -l ..... ..... ubuntu-10.04 x86_64 Ubuntu 10.04 (Lucid) ubuntu-12.04 x86_64 Ubuntu 12.04 (Precise) ubuntu-14.04 x86_64 Ubuntu 14.04 (Trusty) ubuntu-16.04 x86_64 Ubuntu 16.04 (Xenial) ubuntu-18.04 x86_64 Ubuntu 18.04 (bionic) ubuntu-20.04 x86_64 Ubuntu 20.04 (focal) ..... ..... # create an image of Ubuntu 18.04 root@dlp:~# virt-builder ubuntu-18.04 --format qcow2 --size 10G -o /var/kvm/images/ubuntu1804.qcow2 --root-password password:myrootpassword
[ 6.1] Downloading: http://builder.libguestfs.org/ubuntu-18.04.xz
[ 111.2] Planning how to build this image
[ 111.2] Uncompressing
[ 116.2] Resizing (using virt-resize) to expand the disk to 10.0G
.....
.....
Output file: /var/kvm/images/ubuntu1804.qcow2
Output size: 10.0G
Output format: qcow2
Total usable space: 9.8G
Free space: 8.3G (84%)
# to configure VM with the image above, run virt-install
root@dlp:~# virt-install \
--name ubuntu-18.04 \
--ram 4096 \
--disk path=/var/kvm/images/ubuntu1804.qcow2 \
--vcpus 2 \
--os-variant ubuntu18.04 \
--network bridge=br0 \
--graphics none \
--noautoconsole \
--boot hd \
--noreboot \
--import
Starting install...
Domain creation completed.
You can restart your domain by running:
virsh --connect qemu:///system start ubuntu-18.04
|
| [3] | [ls] a directory in a virtual machine. |
|
root@dlp:~# virt-ls -l -d ubuntu2104 /root total 24 drwx------ 4 0 0 4096 May 10 07:23 . drwxr-xr-x 20 0 0 4096 May 10 07:22 .. -rw-r--r-- 1 0 0 3106 Aug 14 2019 .bashrc -rw-r--r-- 1 0 0 161 Sep 16 2020 .profile drwx------ 2 0 0 4096 May 10 07:23 .ssh drwxr-xr-x 3 0 0 4096 May 10 07:23 snap |
| [4] | [cat] a file in a virtual machine. |
|
root@dlp:~# virt-cat -d ubuntu2104 /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync ..... ..... |
| [5] | Edit a file in a virtual machine. |
|
root@dlp:~# virt-edit -d ubuntu2104 /etc/fstab # /etc/fstab: static file system information. # # Use 'blkid' to print the universally unique identifier for a # device; this may be used with UUID= as a more robust way to name devices # that works even if disks are added and removed. See fstab(5). # # <file system> <mount point> <type> <options> <dump> <pass> # / was on /dev/ubuntu-vg/ubuntu-lv during curtin installation /dev/disk/by-id/dm-uuid-LVM-6D3tbR3TfSkLkLkemx4PxN9peqK8TBmjWJ9T0YnT01tM4rDCU9fm3rVFubgeQY3d / ext4 defaults 0 1 # /boot was on /dev/vda2 during curtin installation /dev/disk/by-uuid/cf728476-f8c7-4245-85bb-a3d85b704886 /boot ext4 defaults 0 1 /swap.img none swap sw 0 0 |
| [6] | Display disk usage in a virtual machine. |
|
root@dlp:~# virt-df -d ubuntu2104 Filesystem 1K-blocks Used Available Use% ubuntu2104:/dev/sda2 999320 130172 800336 14% ubuntu2104:/dev/ubuntu-vg/ubuntu-lv 19475088 6447324 12015440 34% |
| [7] | Mount a disk for a virtual machine. |
|
root@dlp:~# guestmount -d ubuntu2104 -i /mnt root@dlp:~# ll /mnt total 4021344 drwxr-xr-x 20 root root 4096 May 10 07:22 ./ drwxr-xr-x 20 root root 4096 May 10 01:56 ../ lrwxrwxrwx 1 root root 7 Apr 21 23:17 bin -> usr/bin/ drwxr-xr-x 4 root root 4096 May 10 07:22 boot/ drwxr-xr-x 2 root root 4096 May 10 07:21 cdrom/ ..... ..... |
| [8] | Display the status of virtual machines. |
|
root@dlp:~# virt-top
virt-top 07:27:33 - x86_64 8/8CPU 2593MHz 16100MB
1 domains, 0 active, 0 running, 0 sleeping, 0 paused, 1 inactive D:0 O:0 X:0
CPU: 0.0% Mem: 0 MB (0 MB by guests)
ID S RDRQ WRRQ RXBY TXBY %CPU %MEM TIME NAME
- (ubuntu2104)
|
| Sponsored Link |
|
|