User Tools

Site Tools


proxmox:kvm:kvm-automated

This is an old revision of the document!


Speedy KVM Creation

I created a special Debian 10 preseed iso just for my Proxmox kvm machines. I can go from zero to ready to rock in under 10 minutes!


Create the preseeded iso

The preseeded iso starts automatically, installs a few key packages, sets vim as my default editor and copies my ssh public key to my authorized_keys file so I can run the Ansible playbook right after install.


Create the Ansible Playbook

I created a small playbook to configure the system for me so it's set up the way I like.

  • Run it like so:
    ansible-playbook -v -b --ask-become-pass -i xxx.xxx.xxx.xxx, ansible.yml
  • The file:
    kvm_init.yml
    ---
    - name: Initialize KVM VM
      hosts: all
      vars:
        admin_user: chuck
        admin_group: chuck
      tasks:
        - name: Create ssh-user group
          group:
            name: ssh-user
            gid: 1010
            state: present
     
        - name: Add {{ admin_user }} to ssh-user group
          user:
            name: "{{ admin_user }}"
            groups: ssh-user
            append: yes
     
        - name: Create dotfiles directory
          become: yes
          become_user: "{{ admin_user }}"
          file:
            path: "/home/{{ admin_user }}/.dotfiles"
            owner: "{{ admin_user }}"
            group: "{{ admin_group }}"
            state: directory
            mode: '0755'
     
        - name: Delete default dotfiles
          become: yes
          become_user: "{{ admin_user }}"
          file:
            path: "/home/{{ admin_user }}/{{ item }}"
            state: absent
          with_items:
            - .bashrc
            - .bash_profile
            - .bash_logout
     
        - name: Clone dotfiles
          become: yes
          become_user: "{{ admin_user }}"
          git:
            repo: "{{ item.repo }}"
            dest: "/home/{{ admin_user }}/.dotfiles/{{ item.name }}"
          loop:
            - { name: 'bash', repo: 'https://gitlab.com/dotfiles1/dotfiles-bash.git' }
            - { name: 'screen', repo: 'https://gitlab.com/dotfiles1/dotfiles-screen.git' } 
            - { name: 'vim', repo: 'https://gitlab.com/dotfiles1/dotfiles-vim.git' }
            - { name: 'git', repo: 'https://gitlab.com/dotfiles1/dotfiles-git.git' }
            - { name: 'motd', repo: 'https://gitlab.com/dotfiles1/dotfiles-motd.git' }
     
        - name: Stow dotfiles
          become: yes
          become_user: "{{ admin_user }}"
          command: stow "{{ item }}"
          args:
            chdir: "/home/{{ admin_user }}/.dotfiles"
          loop: 
            - bash
            - git
            - screen
            - vim
     
        - name: Install MOTD
          become: yes
          copy:
            src: "/home/{{ admin_user }}/.dotfiles/motd/{{ item }}"
            dest: "/etc/update-motd.d/{{ item }}"
            remote_src: yes
            owner: root
            group: root
            mode: 0755
          loop:
            - 10-uname
            - 20-sysinfo
            - 90-fortune
     
        - name: Set PAM motd
          become: yes
          lineinfile:
            backup: yes
            path: /etc/pam.d/sshd
            regexp: 'noupdate$'
            line: '#session    optional      pam_motd.so noupdate'
     
        - name: Disable password ssh
          become: yes
          lineinfile:
            path: /etc/ssh/sshd_config
            regexp: "#PasswordAuthenticaiton yes"
            line: "PasswordAuthentication no"
     
        - name: Restrict ssh to ssh-user group
          become: yes
          blockinfile:
            path: /etc/ssh/sshd_config
            block: "AllowGroups ssh-user"
     
        - name: Add {{ admin_user }} to /etc/security/access.conf
          become: yes
          blockinfile:
            path: /etc/security/access.conf
            block: '+:{{ admin_user }}:ALL'
     
        - name: Configure PAM
          become: yes
          lineinfile:
            path: /etc/pam.d/sshd
            regexp: 'pam_access.so$'
            line: 'account required    pam_access.so'

Create a VM

GUI

  • Open the Proxmox web interface
  • Upload the newly created preseed-iso to your Proxmox storage
  • Create a VM with whatever settings you want, using the preseed-iso for the CD/DVD disk image.
  • Start the virtual machine.
  • Wait for it to complete. It took roughly 7 minutes for mine.
  • Run the ansible playbook.
  • Enjoy!

CLI

  • ssh into Proxmox
  • The iso's on my system are located at /media/sas/data/template/iso so you could scp them there (or wherever they are stored on your system) if you don't want to use the gui.
  • List vm's:
    sudo qm list
  • Pick a free vmid and create the vm with your preferred settings:
    sudo qm create 150 --cdrom sas-storage:iso/preseed-debian-10.4.iso \
    --name preseed --numa 0 --ostype l26 \
    --cpu cputype=host --cores 2 --sockets 2 \
    --memory 4096  \
    --net0 bridge=vmbr90,virtio \
    --bootdisk scsi0 --scsihw virtio-scsi-pci --scsi0 file=ssd-lvm:32 \
    --serial0 socket --vga qxl --audio0 device=ich9-intel-hda,driver=spice
  • Start the vm:
    sudo qm start <vmid>
  • Wait for it to complete.

proxmox/kvm/kvm-automated.1590616181.txt.gz · Last modified: by chuck