首页 > 其他分享 >kvm基本操作命令

kvm基本操作命令

时间:2024-04-26 09:37:16浏览次数:24  
标签:虚拟机 kvm bytes dev 命令 virsh 基本操作 512

1.查看虚拟机列表

virsh list

2.查看正在运行的虚拟机列表

virsh list --all

3.查看所有虚拟机

virsh list --all

4.查看虚拟机详细信息 域

virsh dominfo [虚拟机名称]

5.虚拟机域的开关机操作

virsh start [虚拟机名称](开机)

virsh shutdown [虚拟机名称](温柔的关机)

virsh shutdown [ID号](关机)

virsh destroy [虚拟机名称](强制关机,相当于拔电源)

6.导出配置文件

virsh dumpxml test01 > test01.xml

7.删除虚拟机

virsh undefine test01(删除虚拟机后磁盘文件还在)

8.根据配置文件恢复虚拟机

virsh define test01.xml

9.修改配置文件

virsh edit [虚拟机名称](自带语法检查功能)

10.修改虚拟机名称(7.2版本之前不支持)

virsh domrename [虚拟机旧名称] [虚拟机新名称]

11.查看虚拟机对应的vnc端口

virsh vncdisplay [虚拟机名称]

12.挂起虚拟机

virsh suspend [虚拟机名称]

13.恢复挂起的虚拟机

virsh resume [虚拟机名称]

14.开机自启

virsh autostart [虚拟机名称]

15.取消开机自启

virsh autostart --disable [虚拟机名称]

16.显示虚拟机分区信息

virt-df [虚拟机名称]

17.显示虚拟机内存和cpu的使用情况

yum install virt-top -y

virt-top

18.通过控制窗口登录虚拟机

virsh console [虚拟机名称]

19.给虚拟机添加硬盘

# 添加硬盘(lvm卷)或者USB到虚拟机上
virsh attach-disk kvm-1 /dev/sdb vbd --driver qemu --mode shareable

# 使用完成之后可以卸载usb
virsh detach-disk kvm vdb

# 添加lvm卷,并挂载
[root@sh-kvm-1 ~]# lvcreate -n kvm-1-data -L 50G vg_shkvm1
[root@sh-kvm-1 ~]# virsh attach-disk kvm-1 /dev/vg_shkvm1/kvm-1-data vdb --driver qemu --mode shareable
Disk attached successfully
# 登录到kvm-1上查看lvm是否已经被挂载
[root@sh-kvm-1 ~]# virsh console kvm-1 # 输入kvm-1的用户和密码
[root@sh-kvm-1-1 ~]# fdisk -l # 查看硬盘挂载情况

Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00058197

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

Disk /dev/mapper/VolGroup-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/VolGroup-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/vdb: 53.7 GB, 53687091200 bytes  # 新添加的硬盘
16 heads, 63 sectors/track, 104025 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000



# 格式化新添加的vdb,并添加到lvm组中
# 对新添加的硬盘分区
[root@sh-kvm-1-1 ~]# fdisk /dev/vdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xf04b6807.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

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
   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 (m for help): n  #添加一个分区
Command action
   e   extended
   p   primary partition (1-4)
p  #选择添加一个扩展分区
Partition number (1-4):
Value out of range.
Partition number (1-4): 1
First cylinder (1-104025, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-104025, default 104025):
Using default value 104025

Command (m for help): t  #改变分区的格式
Selected partition 1
Hex code (type L to list codes): 8e  #改成lvm
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): w  # 保存更改
root@sh-kvm-1-1 ~]# mkfs.ext4 /dev/vdb1  # 格式化分区
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3276800 inodes, 13107142 blocks
655357 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
400 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000, 7962624, 11239424

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@sh-kvm-1-1 ~]# pvc reate /dev/vdb1   # 创建pv
vdb   vdb1
[root@sh-kvm-1-1 ~]# vgextend VolGroup /dev/vdb  # 扩展lvm vg
vdb   vdb1
[root@sh-kvm-1-1 ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree
  VolGroup   2   2   0 wz--n- 69.50g 50.00g
# 从上面能看出,新添加的 已经加到lvm组中


20.改变虚拟机的参数

# 更改内存
# 1. 查看虚拟机当前内存
[root@sh-kvm-1 ~]# virsh dominfo kvm-1 | grep memory
Max memory:     4194304 KiB
Used memory:    4194304 KiB

# 2、动态设置内存为512MB,内存减少
virsh setmem kvm-1 524288
# 注意单位必须是KB

# 3、查看内存变化
# virsh dominfo kvm-1 | grep memory
Max memory: 14194304 KiB
Used memory: 524288 kiB

# 4、内存增加
virsh shutdown kvm-1
virsh edit kvm-1  # 直接更改memory
virsh create /etc/libvirt/demu/kvm-1/xml



# 更改CPU(需要修改配置文件,因此需要停止虚拟机)
virsh shutdown kvm-1
virsh edit kvm-1
#  <vcpu>2</vcpu>  # 4 > 2
virsh create /etc/libvirt/demu/kvm-1/xml



# 硬盘扩容
1. Create a 10-GB non-sparse file:
# dd if=/dev/zero of=/vm-images/vm1-add.img bs=1M count=10240
2. Shutdown the VM:
 # virsh shutdown vm1
3. Add an extra entry for ‘disk’ in the VM's XML file in /etc/libvirt/qemu. You can look copy & paste
the entry for your mail storage device and just change the target and address tags. For example:
 # virsh edit vm1
 <disk type='file' device='disk'>
 <driver name='qemu' type='raw' cache='none' io='threads'/>
 <source file='/vm-images/vm1.img'/>
 <target dev='vda' bus='virtio'/>
 <address type='pci' domain='0x0000' bus='0x00' slot='0x04'
function='0x0'/>
 </disk>
 Add:
 <disk type='file' device='disk'>
 <driver name='qemu' type='raw' cache='none' io='threads'/>
 <source file='/vm-images/vm1-add.img'/>
 <target dev='vdb' bus='virtio'/>
 <address type='pci' domain='0x0000' bus='0x00' slot='0x06'
function='0x0'/>
 </disk>
 # 这里建议使用上面的添加硬盘的方式添加


标签:虚拟机,kvm,bytes,dev,命令,virsh,基本操作,512
From: https://www.cnblogs.com/wg0717/p/18159235

相关文章

  • kvm修改虚拟机名称
    关闭虚拟机注意:如果虚拟机有快照需要先删除快照!virshlist--allvirshshutdowntest_66.124正常关闭后,虚拟机状态state为:shutoff修改配置文件名称mvtest_66.124.xmlwugang_test_66.124.xml修改配置文件内容修改图中标注的名称,即虚拟机的名称这两处为虚拟机的磁盘......
  • PEcmd是一个命令行工具,用于执行各种操作系统和文件系统相关的任务。通常,它用于在Windo
    PECMD命令帮助-PECMD技术社区:www.pecmd.netPEcmd是一个命令行工具,用于执行各种操作系统和文件系统相关的任务。通常,它用于在Windows操作系统上执行各种文件和目录操作,例如文件复制、移动、删除等。PEcmd提供了一种简单而强大的方式来管理文件和目录,尤其是在自动化和批处......
  • mountvol命令是Windows操作系统中的一个命令行工具,用于管理卷挂载点和卷名称。通过mou
    mountvol|MicrosoftLearnmountvol命令是Windows操作系统中的一个命令行工具,用于管理卷挂载点和卷名称。通过mountvol命令,用户可以查看当前系统中的卷挂载点信息、创建新的卷挂载点、删除已存在的卷挂载点等操作。具体来说,mountvol命令的主要功能包括:列出卷挂载点:通过......
  • 利用SpringBoot的CommandLineRunner编写命令行jar程序
    1.项目pom<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.......
  • nvidia官方AI框架软件的命令行操作接口 —— NVIDIA GPU Cloud (NGC) CLI
    NVIDIAGPUCloud(NGC)CLI安装介绍地址:https://org.ngc.nvidia.com/setup/installers/cli安装好后需要输入自己的NVIDIANGC的APIKEY,该信息在下面地址中生成:https://org.ngc.nvidia.com/setup/api-key......
  • git命令下,mac环境下载依赖相关报错问题解决方案
    1.安装fundry框架curl-Lhttps://foundry.paradigm.xyz|bash2.写入环境变量source/Users/xx/.bashrc3.foundryup问题1报错:致命错误:无法访问'https://github.com/foundry-rs/forge-std解决方案:设置hosts文件:添加指定url的ip地址:140.82.112.4github.com185.1......
  • docker的基本命令
    基础命令https://yeasy.gitbook.io/docker_practice/data_management/volume//容器命令1.dockercommand--help查看对应命令的用法2.dockerps-a查看所有docker进程(无论是否在运行)-l最后一次创建的容器3.dockerlogscontainerId查看容器日志4.dockerstar......
  • mpcmdrun.exe 是 Windows Defender 的命令行界面工具,用于运行 Windows Defender 的扫
    mpcmdrun.exe是WindowsDefender的命令行界面工具,用于运行WindowsDefender的扫描和其他安全操作,如更新病毒定义文件、运行快速扫描等。它是WindowsDefender的一部分,用于保护系统免受恶意软件和其他安全威胁的侵害。这个程序通常自动运行,并在后台处理安全任务,以......
  • CentOS 常用命令详解
    前言CentOS是一种基于Linux内核的开源操作系统,广泛应用于服务器环境和个人电脑中。在使用CentOS进行系统管理和维护时,掌握一些常用的命令是至关重要的。本文将介绍一些在CentOS中经常使用的命令,帮助你更好地管理和操作系统。ls-列出文件和目录ls命令用于列出当前......
  • npm命令完整使用指南
    前言在我们的工作中,npm是我们会经常使用到的工具,比如我们在App自动化测试中使用到的appium,就是通过npm命令来安装的。但是有许多人表示,自己并不清楚npm命令的使用,本文就给大家介绍一下npm命令的使用。安装配置在我们安装配置好node.js之后,npm也是配置好的,无需我们再进行安装,我......