### Install text only Debian GNU/Linux as qemu guest

* Create the hdd disk image.
    qemu-img create -f qcow2 debian.qcow2 16G

* Basic networking: NAT
    As root:
        tunctl -t tap0 -u username
        ifconfig tap0 192.168.45.1 netmask 255.255.255.0 up
        echo 1 > /proc/sys/net/ipv4/ip_forward
        iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

* Run qemu to install the guest operating system.
    qemu-system-x86_64 \
        -name DEBIAN1 \
        -m 512 \
        -k en-us \
        -smp cpus=1 \
        -enable-kvm \
        -hda debian.qcow2 \
        -cdrom /path/to/debian-x.x.x-amd64-DVD-1.iso \
        -serial telnet::2000,server,nowait \
        -monitor tcp:127.0.0.1:4000,server,nowait \
        -curses \
        -boot d \
        -net none \
        -net nic,model=e1000,vlan=1,macaddr=00:01:00:ff:01:01 \
        -net tap,vlan=1,ifname=tap0,script=no,downscript=no

* The text "640 x 480 Graphic mode" appears in the center of screen.
    Press ESC to see the prompt "boot:".
    Enter "help" to see the menu.
    Press F3 to see the available boot methods.
    Press F8 to see how to disable framebuffer.
    To install in text mode, use:
        "install vga=normal fb=false" or
        "expert vga=normal fb=false"

* Make a normal installation.
    Select keyboard: EN US

* When the installation is done, the guest machine reboot and
  goes back to the CD installer.
  Enter to monitor mode with ESC+2 or "telnet 127.0.0.1 4000"
  from another terminal and execute "quit", or kill the qemu
  process to poweroff the guest machine.

* Disable framebuffer in the guest operating system.

    * Mount the guest hdd file.
        modprobe nbd max_part=63
        qemu-nbd -c /dev/nbd0 debian.qcow2
        mount /dev/nbd0p1 /mnt/

    * Disable framebuffer and enable serial console in grub.
        Edit /mnt/etc/default/grub.
            GRUB_TERMINAL="console serial"
            GRUB_TERMINAL_OUTPUT="console serial"
            GRUB_CMDLINE_LINUX="vga=normal nomodeset console=tty1 console=ttyS0,115200"
            GRUB_CMDLINE_LINUX_DEFAULT="quiet nomodeset console=tty1 console=ttyS0,115200"
            GRUB_GFXPAYLOAD_LINUX="text"
            GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"

    * Update grub.
        mount --bind /dev /mnt/dev
        mount --bind /dev/pts /mnt/dev/pts
        mount --bind /proc /mnt/proc
        mount --bind /sys /mnt/sys
        chroot /mnt
        grub-install /dev/nbd0
        grub-install --recheck /dev/nbd0
        update-grub
        exit
        umount /mnt/sys
        umount /mnt/proc
        umount /mnt/dev/pts
        umount /mnt/dev

    * Umount the guest hdd file.
        umount /mnt/
        qemu-nbd -d /dev/nbd0
        rmmod nbd

* Boot the virtual machine.
    qemu-system-x86_64 \
        -name DEBIAN1 \
        -m 512 \
        -k en-us \
        -smp cpus=1 \
        -enable-kvm \
        -hda debian.qcow2 \
        -serial telnet::2000,server,nowait \
        -monitor tcp:127.0.0.1:4000,server,nowait \
        -curses \
        -boot c \
        -net none \
        -net nic,model=e1000,vlan=1,macaddr=00:01:00:ff:01:01 \
        -net tap,vlan=1,ifname=tap0,script=no,downscript=no

* In the grub menu, edit the first entry.
    * Change root=/dev/nbd0p1 to root=/dev/sda1
    * CTRL-x to boot

* Login as root.

* Update grub.
    grub-mkconfig -o /boot/grub/grub.cfg
    update-grub

* To change the keyboard layout:
    dpkg-reconfigure keyboard-configuration
    It is convenient to leave it in English.

* Reboot.
    reboot

* Change /etc/apt/sources.list to a mirror near you.
  Disable the "deb cdrom" entry.

* Update & upgrade.
    apt-get update
    apt-get dist-upgrade

* Install packages.
    apt-get install aria2 bridge-utils bmon bzip2 ccrypt \
        dnsutils elinks ethtool file flip fping ftp git \
        hdparm hping3 ifstat iperf3 iperf iptraf \
        joe less locate lsb-release lshw lsof lynx \
        mc mtr netdiag net-tools nload nmap nmon \
        p7zip psutils rar rename rsync screen socat \
        ssh tcpdump tcptraceroute telnet time traceroute \
        tshark uml-utilities unzip vlan wput wipe \
        zip man dstat ifstat nicstat sysstat

* Clean.
    apt-get clean
    apt-get autoremove

* Disable daemons.
    systemctl disable cron.service
    systemctl disable rsync.service
    systemctl disable rsyslog.service
    systemctl disable sysstat.service
    systemctl disable uml-utilities.service
    systemctl disable systemd-timesyncd.service

* If necessary, edit /etc/network/interfaces to
    configure a fixed IP address.

* Reboot.
    reboot

* Remove unnecessary files.

* Zero out the disk.
    dd if=/dev/zero of=/tmp/zero
    sync
    rm -f /tmp/zero
    sync
    poweroff

* Shrink the disk file.
    mv debian.qcow2 debian.backup
    qemu-img convert -O qcow2 debian.backup debian.qcow2

* The debian.qcow2 file contain a clean Debian GNU/Linux.
  You can make a backup for future use.

* To start in daemon mode:
    qemu-system-x86_64 \
        -name DEBIAN1 \
        -m 512 \
        -k en-us \
        -smp cpus=1 \
        -enable-kvm \
        -hda debian.qcow2 \
        -serial telnet::2000,server,nowait \
        -monitor tcp:127.0.0.1:4000,server,nowait \
        -display none \
        -daemonize \
        -boot c \
        -net none \
        -net nic,model=e1000,vlan=1,macaddr=00:01:00:ff:01:01 -net tap,vlan=1,ifname=tap0,script=no,downscript=no \
        -net nic,model=e1000,vlan=2,macaddr=00:01:00:ff:01:02 -net socket,vlan=2,udp=127.0.0.1:9012,localaddr=:9002 \
        -net nic,model=e1000,vlan=3,macaddr=00:01:00:ff:01:03 -net socket,vlan=3,udp=127.0.0.1:9013,localaddr=:9003 \
        -net nic,model=e1000,vlan=4,macaddr=00:01:00:ff:01:04 -net socket,vlan=4,udp=127.0.0.1:9014,localaddr=:9004

* Done!
