giovtorres / kvm-install-vm Goto Github PK
View Code? Open in Web Editor NEWBash script to build local virtual machines using KVM/libvirt and cloud-init.
License: MIT License
Bash script to build local virtual machines using KVM/libvirt and cloud-init.
License: MIT License
any plan on adding CoreOS into selection?
Instead of remove cloud-init, what about disable the cloud-init service only @giovtorres ?
Something like
ln-sf /dev/null /etc/systemd/system/cloud-init.service
#or
systemctl disable cloud-init
I think this may be the same issue as #20 but for the Amazon Linux 2 image.
If I create a vm with the default disk options it works fine.
If I create a vm with the -d
option then it hangs waiting for the IP address. Looking at the console I see:
error: unknown filesystem.
Entering rescue mode...
grub rescue>
I was able to repro this manually by creating a vm with the disk defaults, stopping it, and running virt-resize
. After running virt-resize
the vm fails to boot with the above error.
I'll try the qemu-img resize
workaround and see if that does the trick. If so maybe we should change the if test to include Amazon Linux 2.
Hi,
After run the kvm-install-vm script, it stopped at following step for few hours.
⚡ root@fedora ~/kvm-install-vm master ./kvm-install-vm -n kvm0
[03:21:43 PM]----> Copying cloud image (CentOS-7-x86_64-GenericCloud.qcow2)...
[03:22:18 PM]----> Generating ISO for cloud-init...
[03:22:18 PM]----> Creating Storage Pool with the following command:
virsh pool-create-as \
--name kvm0 \
--type dir \
--target /root/virt/images/kvm0
Pool kvm0 created
[03:22:18 PM]----> Installing the domain and adjusting the configuration...
[03:22:18 PM]----> Installing with the following command:
virt-install \
--import \
--name kvm0 \
--ram 1024 \
--vcpus 1 \
--cpu host \
--disk kvm0.qcow2,format=qcow2,bus=virtio \
--disk kvm0-cidata.iso,device=cdrom \
--network bridge=virbr0,model=virtio \
--os-type=linux \
--os-variant=centos7.0 \
--graphics spice \
--noautoconsole
Starting install...
Domain creation completed.
[03:22:49 PM]----> Cleaning up cloud-init...
[03:22:49 PM]----> Waiting for domain to get an IP address ...
The additional info:
⚡ root@fedora ~ uname -a
Linux fedora 4.12.9-300.fc26.x86_64 #1 SMP Fri Aug 25 13:09:43 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
⚡ root@fedora ~ virsh dominfo kvm0
Id: 1
Name: kvm0
UUID: 0a267f80-c3a4-4e9e-88a0-1530ef03f0a7
OS Type: hvm
State: running
CPU(s): 1
CPU time: 1575.5s
Max memory: 1048576 KiB
Used memory: 1048576 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0
⚡ root@fedora ~ virsh domiflist kvm0
Interface Type Source Model MAC
-------------------------------------------------------
vnet0 bridge virbr0 virtio 52:54:00:93:62:d9
⚡ root@fedora ~ systemctl status dnsmasq.service
● dnsmasq.service - DNS caching server.
Loaded: loaded (/usr/lib/systemd/system/dnsmasq.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2017-09-09 15:09:24 CST; 33min ago
Main PID: 861 (dnsmasq)
Tasks: 1 (limit: 19660)
CGroup: /system.slice/dnsmasq.service
└─861 /usr/sbin/dnsmasq -k
Sep 09 15:09:24 fedora systemd[1]: Started DNS caching server..
Sep 09 15:09:24 fedora dnsmasq[861]: started, version 2.76 cachesize 150
Sep 09 15:09:24 fedora dnsmasq[861]: compile time options: IPv6 GNU-getopt DBus no-i18n IDN DHCP DHCPv6 no-Lua TFTP no-conntrack ipset auth DNSSEC loop-detect inotify
Sep 09 15:09:24 fedora dnsmasq[861]: read /etc/hosts - 6 addresses
Sep 09 15:09:28 fedora dnsmasq[861]: failed to access /etc/resolv.conf: No such file or directory
Sep 09 15:09:35 fedora dnsmasq[861]: reading /etc/resolv.conf
Sep 09 15:09:35 fedora dnsmasq[861]: using nameserver 192.168.10.2#53
⚡ root@fedora ~
This is what I try:
[root@lab1 ~]# kvm-install-vm create -c 2 -m 2048 -d 20 -v foo4
- Copying cloud image (CentOS-8-GenericCloud-8.1.1911-20200113.3.x86_64.qcow2) ... OK
- Resizing the disk to 20G ... OK
- Generating ISO for cloud-init ... OK
- Creating storage pool with the following command
virsh \
pool-create-as \
--name=foo4 \
--type=dir \
--target=/data/virt/vms/foo4
OK
- Installing the domain with the following command
virt-install \
--import \
--name=foo4 \
--memory=2048 \
--vcpus=2 \
--cpu=host \
--disk=foo4.qcow2,format=qcow2,bus=virtio \
--disk=foo4-cidata.iso \
--network=bridge=bridge99,model=virtio \
--os-type=linux \
--os-variant=centos8 \
--noautoconsole \
--graphics=spice,port=-1,listen=localhost
ERR: Could not create domain with virt-install.
Even with -v, not that much of information. Let's try it manually by copying the command from above:
[root@lab1 ~]# virt-install \
> --import \
> --name=foo4 \
> --memory=2048 \
> --vcpus=2 \
> --cpu=host \
> --disk=foo4.qcow2,format=qcow2,bus=virtio \
> --disk=foo4-cidata.iso \
> --network=bridge=bridge99,model=virtio \
> --os-type=linux \
> --os-variant=centos8 \
> --noautoconsole \
> --graphics=spice,port=-1,listen=localhost
ERROR Fehler: --disk foo4.qcow2,format=qcow2,bus=virtio: Size must be specified for non existent volume 'foo4.qcow2'
Is it me, using it wrong or is it a bug?
Any way to start the shut off vm through kvm-install-vm command? currently i am starting it by accessing my gnome-boxes...
Hypervisor OS: Ubuntu-18.04
Guest OS: Ubuntu-18.04
kvm-install-vm: latest github version
I want to add an additional user with a specified password and without NOPASSWD in sudoers. By default kvm-install-vm
uses ALL=(ALL) NOPASSWD:ALL
for additional users.
I tried to create a user-data.sh
and run kvm-install-vm create -s "$HOME/user-data.sh" my-vm01
, but it seems to have no effect. I can't login with ssh
, nor through tty with virt-manager
's graphical interface.
My guess is the config is not picked up. What might I be missing?
user-data.sh
# Users
users:
- default
- name: my-user
groups: ['sudo']
shell: /bin/bash
sudo: ALL=(ALL) ALL
ssh-authorized-keys:
- ssh-ed25519 <my-pub-key>
lock-passwd: false
passwd: <my-salted-password-hash>
.kivrc
AUTOSTART=true
CPUS=1
MEMORY=1024
DISK_SIZE=20
IMAGEDIR="/mnt/500g01/virtualbox/image"
VMDIR="/mnt/500g01/virtualbox"
BRIDGE=br2
PUBKEY="${HOME}/.ssh/user01_ed25519.pub"
DISTRO=ubuntu1804
MACADDRESS=$(printf '%02x' $((0x$(od /dev/urandom -N1 -t x1 -An | cut -c 2-) & 0xFE | 0x02)); od /dev/urandom -N5 -t x1 -An | sed 's/ /:/g')
TIMEZONE=Berlin/Europe
VERBOSE=1
Hi first off all i love this little script, use it for all my vm creations on kvm.
In your script there is the runcmd which supposedly should remove cloud-init but on my system it does not. When i shutdown the vm and start it again, i see cloud-init is starting all over again. And the boot time is very long.
This is the output from: /var/log/cloud-init.log
2017-11-06 08:05:25,660 - helpers.py[DEBUG]: Running config-runcmd using lock (<FileLock using file '/var/lib/cloud/instances/luckyme/sem/config_runcmd'>)
Cloud-init v. 0.7.9 running 'modules:config' at Mon, 06 Nov 2017 13:05:24 +0000. Up 13.55 seconds.
2017-11-06 08:05:25,660 - util.py[WARNING]: Failed to shellify [['apt-get', '-y', 'remove', 'cloud-init'], None] into file /var/lib/cloud/instances/luckyme/scripts/runcmd
2017-11-06 08:05:25,660 - util.py[WARNING]: Failed to shellify [['apt-get', '-y', 'remove', 'cloud-init'], None] into file /var/lib/cloud/instances/luckyme/scripts/runcmd
2017-11-06 08:05:25,660 - util.py[DEBUG]: Failed to shellify [['apt-get', '-y', 'remove', 'cloud-init'], None] into file /var/lib/cloud/instances/luckyme/scripts/runcmd
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/cloudinit/config/cc_runcmd.py", line 55, in handle
content = util.shellify(cmd)
File "/usr/lib/python3/dist-packages/cloudinit/util.py", line 1889, in shellify
% (type_utils.obj_name(args)))
RuntimeError: Unable to shellify type NoneType which is not a list or string
Could a generous license (Apache v2?) be applied to this code base (and ultimately noted as the same license on the source blog article as sourced here: http://giovannitorres.me/create-a-linux-lab-on-kvm-using-cloud-images.html)?
Reason for asking is I'd like to utilize this in my own open source project in good faith.
Hi Giovanni,
I have create vm by using command "kvm-install-vm create foo", the vm was created, but the host "foo" cant be extracted:
$ ./kvm-install-vm list
Id Name State
----------------------------------------------------
6 foo running
$ ping foo
ping: unknown host foo
$cat /etc/nsswitch.conf |grep hosts
hosts: files libvirt libvirt_guest mdns4_minimal [NOTFOUND=return] dns
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.3 LTS (Xenial Xerus)"
I have set the "hosts" options in file /etc/nsswitch.conf as the guide in README.md .
Thanks,
Ayakashi
Hi,
First of all, thanks for your work, really nice assembled, (however i'm trying master on a debian host, as a non root user, with all permissions given to libvirt, some vars fail to get assigned properly; i'll open a separate issue for that).
So in the light of the above, i had to cut down to a working solution and also solve static networking.
Wrt networking:
i'm using centos-atomic which at this moment has cloud-init version 18.2 https://cloudinit.readthedocs.io/en/18.2/
I'm adding to the ISO cloud-init image the following yaml file named network-config
which gets picked up. Be aware that in cloud-init NoCloud this should NOT be under YAML node networking:
just the: version:1 config: .... works perfectly fine.
Again this is Cloud-Init dependent and works fine in my ATM latest centos atomic.
export NETWORK_CONFIG=/opt/kvm/ci/${VMNAME}/network-config
export CI_ISO=/opt/kvm/images/${VMNAME}-cidata.iso
sudo rm -rf ${NETWORK_CONFIG}
sync
sudo cat > ${NETWORK_CONFIG} << _EOF_
version: 1
config:
- type: physical
name: eth0
mac_address: "${MAC}"
subnets:
- type: static
address: 192.168.0.101
netmask: 255.255.255.0
gateway: 192.168.0.1
network: 192.168.0.0
- type: nameserver
address:
- 192.168.0.254
search:
- mylocaldnsname.dev
_EOF_
sudo genisoimage -output ${CI_ISO} -volid cidata -joliet -rock ${USER_DATA} ${META_DATA} ${NETWORK_CONFIG}
There is no such function in the code.
Am i wrong?
I am new for KVM and trying to add TWO Network Adaptor, i am able to configure bridge0 successfully but i need to also add bridge1 to the same VM, please advice me how can i make it done
Rocky Linux is missing.
I tried to add it myself, to make a pull request. As far as I can tell, it may work, but it's not in the osinfo-db:
ERR: Unknown OS variant 'rocky84'. Please update your osinfo-db. See https://libosinfo.org/download for more information.
Question: How to proceed? Add an exception?
Line 1010 in 2358282
The variable $SOURCE
is not used
I'm facing the issue that debian9 is working perfectly, but debian10 is not working. On debian10 the hostname is not set and also logging in via ssh is not working/enabled. I'm not sure if this is an issue with this script/debian butt image or my local setup.
I'm using Arch Linux with QEMU 4.2.0 / virt-install 2.2.1
scampos@cetus:~/src/kvm-install-vm (master)$ ./kvm-install-vm create -i /home/scampos/os-images/cloud/centos7.6/CentOS-7-x86_64-GenericCloud-1811.qcow2 centos7.6
- Using custom QCOW2 image: /home/scampos/os-images/cloud/centos7.6/CentOS-7-x86_64-GenericCloud-1811.qcow2.
- Copying cloud image (CentOS-7-x86_64-GenericCloud-1811.qcow2) ... OK
- Generating ISO for cloud-init ... OK
- Creating storage pool ... OK
- Installing the domain ... OK
- Cleaning up cloud-init files ... OK
- Waiting for domain to get an IP address ... OK
- Checking for 192.168.124.115 in known_hosts file
- No entries found for 192.168.124.115
- SSH to centos7.6: 'ssh <use the default account in your custom image>@192.168.124.115' or 'ssh <use the default account in your custom image>@centos7.6'
- Console at spice://localhost:5901
- DONE
scampos@cetus:~/src/kvm-install-vm (master)$ virsh shutdown centos7.6
Domain centos7.6 is being shutdown
scampos@cetus:~/src/kvm-install-vm (master)$ virsh snapshot-create-as centos7.6 --name test-snapshot
Domain snapshot test-snapshot created
scampos@cetus:~/src/kvm-install-vm (master)$ ./kvm-install-vm remove centos7.6
- Destroying centos7.6 domain ... (Domain is not running.)
- Undefining centos7.6 domain ... ERR: Could not undefine domain.
scampos@cetus:~/src/kvm-install-vm (master)$ virsh snapshot-delete centos7.6 --children --current
Domain snapshot test-snapshot deleted
scampos@cetus:~/src/kvm-install-vm (master)$ ./kvm-install-vm remove centos7.6
- Destroying centos7.6 domain ... (Domain is not running.)
- Undefining centos7.6 domain ... OK
- Deleting centos7.6 files ... OK
- Destroying centos7.6 storage pool ... OK
Hello
How can I provide my own network settings?
Hello there,
The ubuntu1804 image does not seem to work. I think it's because the downloaded image is not a drop-in replacement for ubuntu1604. I'm not quite familiar enough with all this to fix - as I attempted to work toward a pull request a few months ago, but ran into this issue (the image for 1804 has extra partitions I think compared to 1604?)
Here is creating a new machine:
$ ./kvm-install-vm create -c 2 -m 2048 -d 10 -t ubuntu1804 -T UTC u1804-test1
- Copying cloud image (ubuntu-18.04-server-cloudimg-amd64.img) ... OK
- Resizing the disk to 10G ... OK
- Generating ISO for cloud-init ... OK
- Creating storage pool ... OK
- Installing the domain ... OK
- Enabling autostart ... OK
- Cleaning up cloud-init files ... OK
- Waiting for domain to get an IP address ...
This where the failure occurs because the machine cannot boot, here is the console output from the machine:
Booting from Hard Disk...
error: unknown filesystem.
Entering rescue mode...
grub rescue>
I will attempt to add more info in an edit, looking at the u1604 vs u1804 images being used.
Thanks!
Thanks for this tool, it seems pretty handy.
I'm not that familiar with using kvm, qemu, and virsh. On Fedora 26 I'm used to having VMs show up in virt-manager for an easy GUI experience. Do you know a way to get VMs created by this tool to show up there?
Also, I'm finding that after shutting them down, when I restart them (virsh start) they don't seem to come up successfully. I can't connect to them via ssh, though a screenshot doesn't show any obvious problems. If they came up with a different IP I'm not sure how to find out what that is. Any advice you can offer?
In vagrant, we share a directory from host machine to virtual machine using rsync or sshfs. What is the way to enable the same functionality here.
Looking through the script, it seems that the default user is supposed to have nopasswd sudo set, but when logging in and issuing any sudo commands, it prompts for a password and does not accept anything (as I'm assuming none have been set). I can get SSHd through root but it displays a warning telling me to log in as centos instead. Seems like I'm probably missing something obvious here, but I just don't get how to get root.
I'll try and figure this out myself, but I was wondering if you've encountered an issue with a failure on disk resizing? Here's what I'm seeing:
./kvm-install-vm create ovirtnode2
- Copying cloud image (CentOS-7-x86_64-GenericCloud.qcow2) ... OK
- Resizing the disk to 50 ... ERR: Could not resize disk.
The logfile shows this:
Formatting 'ovirtnode2.qcow2.new', fmt=qcow2 size=50 cluster_size=65536 preallocation=metadata lazy_refcounts=off refcount_bits=16
virt-resize: error: ovirtnode2.qcow2.new: file is too small to be a disk
image (512 bytes)
If reporting bugs, run virt-resize with debugging enabled and include the
complete output:
virt-resize -v -x [...]
New install without specifying disk size and I get a ubuntu instance with only 2.1 GB.
Filesystem Size Used Avail Use% Mounted on
udev 238M 0 238M 0% /dev
tmpfs 49M 1.9M 47M 4% /run
/dev/vda1 2.1G 1.5G 574M 73% /
tmpfs 245M 0 245M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 245M 0 245M 0% /sys/fs/cgroup
tmpfs 49M 0 49M 0% /run/user/1000
My use case for kvm-install-vm is to perform test run in multiple versions of a given distro (centos, fedora and rhel). While I don't think it would make sense to create a PR to add centos-1611, centos1811, centos1907, etc., those names are meaningful and helpful in my tests.
For that reason I'd like to suggest that the mapping of distros could be customized in .kvirc or somewhere else.
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.