安装准备 建立虚拟机这里是用一台有显示器有键盘的普通PC,通过网线 SSH 登录到一台没有键盘没有显示器的服务器进行安装的。服务器系统为 Debian GNU/Linux 9 (stretch) 64-bit 。 首先从 普通 PC 登录服务器 ssh nong@192.168.2.11 命令: egrep '(vmx|svm)' /proc/cpuinfo 查看一下 操作系统 是32位还是64位 file /bin/ls 安装 sudo apt-get install kvm qemu sudo apt-get install virtinst python-libvirt virt-viewer virt-manager sudo apt install qemu-kvm libvirt-clients 查看kvm内核模块是否已加载 sudo lsmod | grep kvm sudo modprobe kvm 添加用户到 kvm sudo adduser nong libvirt sudo adduser nong libvirt-qemu 如果添加不成功,可能你是老系统,需要执行如下 sudo adduser nong kvm sudo adduser nong libvirt 然后试试列出全部虚拟机 virsh list --all 网络设置 sudo nano /etc/network/interfaces -------------------------------------- # This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback -------------------------------------- 修改后的文件 -------------------------------------- # This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 iface eth0 inet manual auto br0 iface br0 inet static address 192.168.2.11 netmask 255.255.255.0 network 192.168.2.0 broadcast 192.168.2.255 gateway 192.168.2.1 bridge_ports eth0 bridge_stp off bridge_fd 0 bridge_maxwait 0 dns-nameservers 8.8.8.8 8.8.4.4 ------------------------------------------------------ 保存退出 重起机器 sudo reboot 测试网络 这个时候 eth0 已经没有具体的 IP 地址。IP地址出在了 br0 的虚拟网上面。我们 ping 一下公网 ip,看看是不是通的。 ping 8.8.8.8 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_req=1 ttl=46 time=51.7 ms 64 bytes from 8.8.8.8: icmp_req=2 ttl=46 time=50.8 ms 建立文件夹 mkdir /var/www/html/debian cd /var/www/html/debian wget -c http://192.168.2.31/debian-8.8.0-amd64-netinst.iso 创建 img 镜像文件(快照) qemu-img create debian8.img 6G qemu-img create ubuntuserver.img 12G qemu-img create centos.img 36G 下面的命令将会创建 10 GB 大小的空磁盘,并且它是 qcow2 格式的。 qemu-img create -f qcow2 /home/dev/images/alice.img 10G 系统软件也有了,这样硬盘也有了,那就开始安装系统到虚拟机的硬盘吧。 以下命令生成一个虚拟机。名字为 debian8 分配一个处理器内核,内存 1024M,硬盘用的是 debian8.img 这个是之前创建好了的,然后光驱的系统软件为 debian-8.8.0-amd64-netinst.iso。 网络为桥接模式,需要提前把路由器的 DHCP 服务打开,以便获得 IP 地址。支持安装期间用 VNC 控制。 sudo virt-install --name debian8 --vcpu=1 --memory 1024 --disk path=/dev/shm/debian8.img,size=4 --cdrom /dev/shm/debian-8.8.0-amd64-netinst.iso --network bridge:br0 --graphics=vnc,listen=0.0.0.0 [sudo] password for nong: WARNING Graphics requested but DISPLAY is not set. Not running virt-viewer. WARNING No console to launch for the guest, defaulting to --wait -1 Starting install... Creating
domain...
| 0 B 00:00:01 Domain installation still in progress. Waiting for installation to complete. Domain has shutdown. Continuing. Domain creation completed. Restarting guest. 运行完上面的命令之后,提示需要sudo 密码写进去吧。然后就可以用 VNC 程序连接进行继续安装了。我的 服务器 IP 地址为 192.168.2.11 所以我用一个 debian 系统自带的 VNC 程序 Remote Desktop Viewer 选择 VNC 方式链接,填入 IP 地址。连接。接下去,就和一般的安装系统没什么区别了。安装完成之后会经历一个重启。这个时候 VNC 会断开连接。需要 重现填写之前的 IP 地址 192.168.2.11 再次连接。以便登录系统。 登录系统之后,建议第一件事就是切换到 root 账户安装 sudo su - root apt install sudo 修改第一个普通用户获得 sudo 权限 visudo root ALL=(ALL:ALL) ALL 行下面 nong ALL=(ALL:ALL) ALL exit 然后你可以选择更新软件,安装 SSH 服务器 sudo apt install openssh-server 设置虚拟机的静态 IP 地址 sudo nano /etc/network/interfaces # This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # The primary network interface #allow-hotplug eth0 #iface eth0 inet dhcp auto eth0 iface eth0 inet static gateway 192.168.2.1 address 192.168.2.111 netmask 255.255.255.0 这样设置好后就可以重启虚拟机。然后关闭 VNC 程序,回到普通 PC 的命令行终端上执行 sudo virsh list --all Id Name State ---------------------------------------------------- - debianmini running 然后,对于虚拟机操作。 查看虚拟机的详细信息 sudo virsh dominfo debianmini sudo virsh edit debianmini sudo virsh start debianmini sudo virsh shutdown debianmini sudo virsh destroy debianmini 虚拟机克隆 克隆前确认主机已经关闭 sudo virsh list --all Id Name State ---------------------------------------------------- - ubuntuserver shut off 如果没有关闭的使用下面的命令关闭 sudo virsh shutdown ubuntuserver 查看虚拟机配置文件 sudo ls /etc/libvirt/qemu/ networks ubuntuserver.xml sudo cat /etc/libvirt/qemu/ubuntuserver.xml |grep 'source file'|grep img 开始克隆 virt-clone -o ubuntuserver -n gitlab -f /var/www/html/ubuntu/gitlab.img 好的,那我现在复制了这几个 virsh list --all Id Name State ---------------------------------------------------- - gitlab shut off - gitlab2 shut off - ubuntuserver shut off - ubuntuserver2 shut off ubuntuserver 作为母盘,所有镜像之母 IP 192.168.2.12 ubuntuserver2 继承母盘的 IP 地址,这个 IP 将被路由器 DMZ 暴露出去 gitlab 作为 gitlab 部署项目测试的机器 gitlab2 从母盘新建的第二个 gitlab 部署项目测试的机器 如何删除虚拟机 先列出所有虚拟机名字 sudo virsh list --all Id Name State ---------------------------------------------------- - gitlab shut off - gitlab2 shut off - ubuntuserver shut off - ubuntuserver2 shut off 然后取相应的名字,比如这里要删除 gitlab2 这个虚拟机 sudo virsh undefine gitlab2 rm /var/www/html/ubuntu/gitlab2.img 后记 这篇文章太长了,几个部分是分开在一个月中的不同时间来写的,有时写几分钟,有时写十几分钟,生活所迫,只能挤一挤。我自己已经检查运行一遍了,如果你在阅读和使用的时候,发现问题,欢迎联系我。到时候在做修改了。我的邮箱 nong@guoruei.net 参考。 https://wiki.debian.org/KVM https://wiki.debian.org/SSH |