Potreboval jsem jednoduche testovaci prostredi. Ve virtualu kvm/qemu mit nastaveny Debian a v libovolne jine distribuci na pc si ho spustit a snadno pouzivat. Chtel jsem vse automatizovane, abych mohl psat v textovem editoru prikazy a ty se po ulozeni ihned provedly ve virtualu.
Dnes uz bych asi pouzil tty0 a virtualni seriovy port do virtualu, avsak nize popsane reseni je taky funkcni – a napr. propojeni Samby je zajimave.
Ukazka
Instalace
1 2 3 4 5 6 7 8 |
mkdir -p /home/martin/qemu/data cd /home/martin/qemu/data qemu-img create -f qcow2 /home/martin/qemu/debian.qcow2 8G wget http://debian.superhosting.cz/debian-cd/8.5.0/amd64/iso-cd/debian-8.5.0-amd64-CD-1.iso -O /home/martin/Stažené/debian-8.5.0-amd64-CD-1.iso qemu-system-x86_64 -enable-kvm -m 512 -smp 2 -boot once=d -drive file=/home/martin/qemu/debian.qcow2,media=disk,\ format=qcow2 -drive file=/home/martin/Stažené/debian-8.5.0-amd64-CD-1.iso,media=cdrom -localtime -device e1000,\ netdev=user.0 -netdev user,id=user.0,hostfwd=tcp::2222-:22,smb=/home/martin/qemu/data |
Nyni je potreba do virutalu nainstalovat Debian s SSH serverem.
Po instalaci Debianu do virtualu se do nej prihlasit a vytvorit soubor /usr/local/bin/asdf s obsahem:
1 2 3 4 5 6 7 8 9 10 11 |
#!/bin/bash MD5="" while true ; do MD5_TMP=`md5sum /mnt/qemu/cmd.sh` if [ "$MD5" != "$MD5_TMP" ]; then MD5=$MD5_TMP cat /mnt/qemu/cmd.sh | bash > /dev/tty1 fi sleep 1 done |
Dalsi potrebny kroky:
1 2 3 4 5 6 7 8 9 |
chmod a+x /usr/local/bin/asdf sed -i 's/deb cdrom/#deb cdrom/g' /etc/apt/sources.list apt-get update apt-get install cifs-utils mkdir /mnt/qemu sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/g' /etc/ssh/sshd_config systemctl restart ssh.service sed -i 's/GRUB_TIMEOUT=5/GRUB_TIMEOUT=0/g' /etc/default/grub update-grub |
do /etc/rc.local pridat pred radek exit 0:
1 |
/usr/local/bin/asdf & |
do /etc/fstab pridat (nakonec dat prazdny radek):
1 2 |
<span style="font-size: 12.16px;">#mount -t cifs -o guest //10.0.2.4/qemu /mnt/qemu //10.0.2.4/qemu /mnt/qemu cifs guest 0 0</span> |
Pridat do virtualu svuj SSH klic:
1 |
ssh-copy-id -p 2222 root@localhost |
Pouzivani
Spustit virtual
1 2 |
qemu-system-x86_64 -enable-kvm -m 512 -smp 2 -drive file=/home/martin/qemu/debian.qcow2,media=disk,format=qcow2\ -localtime -device e1000,netdev=user.0 -netdev user,id=user.0,hostfwd=tcp::2222-:22,smb=/home/martin/qemu/data |
a do souboru /home/martin/qemu/data/cmd.sh pridat libovolne prikazy:
1 2 |
echo "echo 'Hello world z virtualu'" > /home/martin/qemu/data/cmd.sh echo "echo 'text z virtualu' > /mnt/qemu/vystup.txt" >> /home/martin/qemu/data/cmd.sh |
ihned po ulozeni souboru se prikazy ve virtualu vykonaji (posilaji se pres rouru do bashe pod rootem).
Do virtualu se da prihlasit pres ssh: ssh -p 2222 root@localhost (bez hesla, prihlasovani klicem).
Odkaz na puvodni gist – je to tam lepe formatovane 🙂