# Virtualization on Linux
Most end-user machines (PC, laptop) run Windows or MacOS out of the box, but it always comes the day when one has to run a full-fledged Linux system be it to study operating systems, to run specific programs that are treated as second-class citizens on Windows and MacOS, or to simply chuang-bi.
One is naturally presented with three choices: bare-metal, virtual machine, container.
1. **Bare-Metal:** To boot directly into Linux, making it the OS running directly on one's machine.
2. **Virtual Machine:** Provision a virtual environment in the OS that is currently running to run a full Linux system *as if* a program.
3. **Container:** This option is very restricted in terms of hardware control but requires much less machine resources compared to the previous two.
[Debian Buster virt-manager 2.0.0](https://manpages.debian.org/buster/virtinst/virt-install.1.en.html)
`--memory` deprecates `-r/--ram`.
Create `test.yaml` with content:
```
variant: fcos
version: 1.0.0
passwd:
users:
- name: user
ssh_authorized_keys:
- ssh-rsa AAA...
```
Compile it to the `ignition` file `test.ign` via:
```
fcct -p -s -o test.ign test.yaml
```
Set up the correct `SELinux` label:
```
chcon -t svirt_home_t test.ign
```
```
virt-install \
--name test \
--memory 1024 \
--os-variant=fedora31 \
--import \
--graphics none \
--network bridge=virbr4 \
--disk size=8,backing_store="$HOME/.local/share/libvirt/images/fedora-coreos-32.20200715.3.0-qemu.x86_64.qcow2" \
--qemu-commandline="-fw_cfg name=opt/com.coreos/config,file=$HOME/test.ign"
```
## Fedora packages
```
dnf install \
qemu-kvm-core \
libvirt \
libvirt-bash-completion \
virt-install
```
## Install debian from serial console
[Ref.](https://weblog.retiisi.eu/2018/04/debian-install-virtual-serial-port/)
```
virt-install \
--name sim \
--memorys 1024 \
--hvm \
--os-variant debian10 \
--network bridge=virbr1 \
--cdrom /var/lib/libvirt/images/debian-10.4.0-amd64-netinst.iso \
--boot hd,cdrom,useserial=on \
--disk size=4 \
--graphics none \
--console target_type=serial
```
Upon menu splash, don't install just yet.
Pess `TAB`, and replace `--- quiet` with `console=ttyS0 vga=none`.
Before you finish the installation, go back to menu, and choose `Execute a shell`.
To make future boots use the serial consoel, mount the virtual file systems under `/target` and chroot into it:
```
mount -o bind /proc /target/proc
mount -o bind /sys /target/sys
mount -o bind /dev /target/dev
chroot /target /bin/bash
vi /etc/default/grub
```
Replace `GRUB_CMDLINE_LINUX=""` with `GRUB_CMDLINE_LINUX="console=ttyS0"`, and uncomment `GRUB_CONSOLE=serial`.
Save and exit `vi`.
Execute the following, and finish the installation.
```
update-grub
umount /target/sys /target/dev /target/proc
```
## Debian
Functional libvirt server side (debian) install.
```
apt install --no-install-recommends qemu-kvm libvirt-clients libvirt-daemon-system qemu-utils
```
If dnsmasq is missing on the server side:
```
apt install dnsmasq
```
The default network might not be up:
```
virsh -c qemu:///system net-start default
```
If the default network complains about some interface taking the default subnet, reassign the default network ip range:
```
virsh -c qemu:///system net-edit0 default
```
If the virt-manager client complains about:
```
The remote host requires a version of netcat/nc which supports the -U option.
```
Uninstall all possible netcat packages and use `netcat-openbsd`:
```
apt remove netcat netcat-traditional netcat-openbsd
apt install netcat-openbsd
```
```
apt install --no-install-recommends virt-manager
```
```
chmod u+s /usr/lib/qemu/qemu-bridge-helper
echo "allow virbr0" >> /etc/qemu/bridge.conf
```
## Check if cdrom is empty or not
## Reusing images
```
virt-install \
--os-variant debian10 \
--graphics none \
--network bridge=virbr0 \
--hvm \
--import \
--disk vol=default/deb1.qcow2
```
```
qemu-img -f qcow2 -b <from> <to>
```
## PfSense
```
virt-install \
--name pfsense \
--memory 1024 \
--hvm \
--graphics none \
--boot hd,useserial=yes \
--disk path=/var/lib/libvirt/images/pfSense-CE-memstick-serial-2.4.5-RELEASE-p1-amd64.img \
--disk size=2
echo 'console="comconsole"' >> /boot/loader.conf
```
## CentOS 7
Run everything in `screen`.
The `vt102` emulation of screen is so much better.
```
virt-install \
--name Undercloud \
--memory 16384 \
--vcpus 8 \
--os-variant centos7.0 \
--disk /dev/sdb10,cache=none,io=native,bus=virtio \
--disk vol=default/CentOS-7-x86_64-NetInstall-2003.iso,device=cdrom \
--graphics none \
--boot hd,cdrom,useserial=on \
--network bridge=virbr1,model=virtio \
--network bridge=virbr2,model=virtio
```
1. Press `TAB` on boot menu, and enter `console=ttyS0`.
2. Configure network.
3. NTP: `tw.pool.ntp.org`.
4. Mirror: `http://centos.cs.nctu.edu.tw/7/os/x86_64/`.
5. Use `LVM`.
6. Don't set `root` password.
```
umount /dev/mapper/centos_undercloud-home
lvremove /dev/mapper/centos_undercloud-home
lvresize /dev/mapper/centos_undercloud-root /dev/vda2
xfs_growfs /dev/mapper/centos_undercloud-root
sed -i '/\/home/d' /etc/fstab
```
### Install `virtualbmc`
```
pip3 install --upgrade --user pip
pip install --user virtualbmc
```