Skip to content

Instantly share code, notes, and snippets.

@darkn3rd
Last active May 12, 2024 21:53
Show Gist options
  • Save darkn3rd/c8081c5e110613010d175ddb4dc5d209 to your computer and use it in GitHub Desktop.
Save darkn3rd/c8081c5e110613010d175ddb4dc5d209 to your computer and use it in GitHub Desktop.
Vagrant QEMU Plugin - qemu command
#####
# qemu command created by vagrant-qemu plugin example
#
# Description:
# This is an example of what the vagrant-qemu plugin creates.
# I am not sure if any of this works, this shell script is theory
# crafting for documentation and understanding.
##########################################
#####################
# Create System using vagrant
##########################################
vagrant plugin install vagrant-qemu
PROJ_DIR="$HOME/myprojects/qemu_ubuntu"
cd $PROJ_DIR
cat << 'EOF' > Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "generic/ubuntu2204"
config.vm.provider "qemu" do |qe|
qe.ssh_port = "50022" # change ssh port as needed
qe.qemu_dir = "/usr/local/share/qemu"
qe.arch = "x86_64"
qe.machine = "q35,accel=hvf"
qe.net_device = "virtio-net-pci"
end
end
EOF
# download, start, stop, destroy guest system
vagrant up --provider=qemu
vagrant halt
vagrant destroy
#####################
# Create System using qemu
##########################################
MACHINE_ID=$(ls -d .vagrant/machines/default/qemu/*/ \
| tr '/' ' ' \
| awk '{ print $NF }'
)
PLUGIN_PATH="$HOME/.vagrant.d/tmp/vagrant-qemu"
IMG_PATH="$PROJ_DIR/.vagrant/machines/default/qemu/$MACHINE_ID/linked-box.img"
PID_PATH="$PROJ/.vagrant/machines/default/qemu/$MACHINE_ID/qemu.pid"
SOCKET_PATH="$PLUGIN_PATH/$MACHINE_ID/qemu_socket"
SERIAL_PATH="$PLUGIN_PATH/$MACHINE_ID/qemu_socket_serial"
## create system using qemu command
qemu-system-x86_64 \
-machine virt,accel=hvf,highmem=on \
-cpu host \
-smp 2 \
-m 4G \
-device virtio-net-device,netdev=net0 \
-netdev user,id=net0,hostfwd=tcp::50022-:22 \
-drive if=virtio,format=qcow2,file="$IMG_PATH" \
-chardev socket,id=mon0,path=$SOCKET_PATH,server=on,wait=off \
-mon chardev=mon0,mode=readline \
-chardev socket,id=ser0,path=$SERIAL_PATH,server=on,wait=off \
-serial chardev:ser0 \
-pidfile $PID_PATH \
-daemonize \
-parallel null \
-monitor none \
-display none \
-vga none
#####################
# SSH into guest
##########################################
cat << EOF > my.ssh_config
Host qemu_ubuntu
HostName 127.0.0.1
User vagrant
Port 50022
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile $PROJ_DIR/vagrant/.vagrant/machines/default/qemu/private_key
IdentitiesOnly yes
LogLevel FATAL
PubkeyAcceptedKeyTypes +ssh-rsa
HostKeyAlgorithms +ssh-rsa
EOF
## Login
ssh -F my.ssh_config qemu_ubuntu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment