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.

Revisions

  1. darkn3rd revised this gist May 12, 2024. 1 changed file with 43 additions and 2 deletions.
    45 changes: 43 additions & 2 deletions vagrant_qemu_command.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,45 @@
    MACHINE_ID="vq_bXP4-f1JhGc" # random gen id
    #####
    # 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"
    PLUGIN_PATH="$HOME/.vagrant.d/tmp/vagrant-qemu"

    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"
    @@ -27,6 +65,9 @@ qemu-system-x86_64 \
    -display none \
    -vga none

    #####################
    # SSH into guest
    ##########################################
    cat << EOF > my.ssh_config
    Host qemu_ubuntu
    HostName 127.0.0.1
  2. darkn3rd created this gist May 12, 2024.
    46 changes: 46 additions & 0 deletions vagrant_qemu_command.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    MACHINE_ID="vq_bXP4-f1JhGc" # random gen id
    PROJ_DIR="$HOME/myprojects/qemu_ubuntu"
    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

    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