]> gitweb.fluxo.info Git - kvmx.git/commitdiff
Serial console support thanks to kvm-manager
authorSilvio Rhatto <rhatto@riseup.net>
Sat, 30 Dec 2017 18:15:14 +0000 (16:15 -0200)
committerSilvio Rhatto <rhatto@riseup.net>
Sat, 30 Dec 2017 18:15:14 +0000 (16:15 -0200)
kvmx
kvmx-create
share/provision/development
share/screen/screenrc [new file with mode: 0644]

diff --git a/kvmx b/kvmx
index f896815acf01a57e5a96af925e32200c50565d5e..81b62e53a7a754c965231c7905d3b6f2d0ee6bc6 100755 (executable)
--- a/kvmx
+++ b/kvmx
@@ -167,6 +167,7 @@ function __kvmx_initialize {
     XPRALOG="$LOG_DIR/xpra"
     XDMCPLOG="$LOG_DIR/xdmcp"
     MONITORFILE="$STATE_DIR/monitor"
+    CONSOLEFILE="$STATE_DIR/console"
 
     if [ -e "$STORAGE/ssh/$VM.key" ]; then
       mkdir -p "$STORAGE/ssh"
@@ -378,6 +379,7 @@ function kvmx_up {
       -device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 \
       -chardev spicevmc,id=spicechannel0,name=vdagent \
       -chardev "socket,id=monitor,path=$MONITORFILE,server,nowait" -mon chardev=monitor,mode=readline \
+      -chardev "socket,id=serial0,path=$CONSOLEFILE,server" -device isa-serial,chardev=serial0 \
       -smp $smp -soundhw ac97 -cpu host -balloon virtio \
       -net nic,model=$nic_model \
       -net user,hostfwd=tcp:127.0.0.1:$SSH-:22,hostfwd=udp:127.0.0.1:$XDMCP_PORT-:177$hostfwd $qemu_opts &> $LOGFILE < /dev/null &
@@ -391,6 +393,16 @@ function kvmx_up {
   echo $GUEST_DISPLAY > $DISPLAYFILE
   echo $XDMCP_PORT    > $XDMCPPORTFILE
 
+  # Thanks kvm-manager code for that portion
+  local screen_log_args=-L
+  if dpkg --compare-versions "$screen_version" ge 4.05; then
+    screen_log_args="-L ./servicelog"
+  fi
+
+  /usr/bin/screen -D -m $screen_log_args \
+                  -c $DIRNAME/share/screen/screenrc \
+                  -S "kvmx-$VM" -t "kvmx-$VM" socat STDIO,raw,echo=0 "UNIX:${CONSOLEFILE},retry=30" &
+
   if [ "$run_spice_client" == "1" ]; then
     sleep 1
     kvmx_spice
@@ -1373,6 +1385,7 @@ function kvmx_mv {
 }
 
 # Interface to QEMU monitor
+# Thanks kvm-manager for the idea
 function kvmx_monitor {
   if ! kvmx_running; then
     echo "$BASENAME: guest $VM is not running"
@@ -1416,6 +1429,21 @@ function kvmx_install {
   kvm -m $memory -net nic,model=virtio -net user -drive file=$image -cdrom $media
 }
 
+# Serial console
+function kvmx_console {
+  if ! kvmx_running; then
+    echo "$BASENAME: guest $VM is not running"
+    exit 1
+  fi
+
+  screen -x kvmx-$VM
+}
+
+# Alias to console
+function kvmx_serial {
+  kvmx_serial $*
+}
+
 # Dispatch
 if type kvmx_$ACTION 2> /dev/null | grep -q "kvmx_$ACTION ()"; then
   __kvmx_initialize
index 8b30e8399b6d2c77970c25b633faad50c137abaf..cdf3278e30c1e282f50b082f2ba6de8fba278b27 100755 (executable)
@@ -286,6 +286,14 @@ function kvmx_create_custom {
 
   if [ "$bootloader" == "grub" ]; then
     $APT_INSTALL grub-pc
+
+    # Serial console support
+    echo ''                                            >> $WORK/etc/default/grub
+    echo '# Custom configuration'                      >> $WORK/etc/default/grub
+    echo 'GRUB_TERMINAL=serial'                        >> $WORK/etc/default/grub
+    echo 'GRUB_SERIAL_COMMAND="serial --speed=115200"' >> $WORK/etc/default/grub
+    echo 'GRUB_CMDLINE_LINUX="console=ttyS0,115200n8"' >> $WORK/etc/default/grub
+
     kvmx_sudo_run chroot $WORK/ update-grub
     kvmx_sudo_run chroot $WORK/ grub-install $device
     # Possible alternatives:
index 2a311177440042f346385473f8fcab07f73fc48c..0f44c07e6ea55ccfe9e9c5431b452f087bf16ddf 100755 (executable)
@@ -85,12 +85,23 @@ if ! grep -q "/swapfile" /etc/fstab; then
   sudo update-initramfs -u
 fi
 
-# GRUB configuration
+# GRUB timeout
 if ! grep -q "GRUB_TIMEOUT=1" /etc/default/grub; then
   sudo sed -i -e 's|GRUB_TIMEOUT=5|GRUB_TIMEOUT=1|' /etc/default/grub
   sudo update-grub
 fi
 
+# Serial console support for VMs not created by a recent kvmx-create
+if ! grep -q "GRUB_TERMINAL=serial" /etc/default/grub; then
+  # Serial console support
+  echo ''                                            | sudo tee -a /etc/default/grub > /dev/null
+  echo '# Custom configuration'                      | sudo tee -a /etc/default/grub > /dev/null
+  echo 'GRUB_TERMINAL=serial'                        | sudo tee -a /etc/default/grub > /dev/null
+  echo 'GRUB_SERIAL_COMMAND="serial --speed=115200"' | sudo tee -a /etc/default/grub > /dev/null
+  echo 'GRUB_CMDLINE_LINUX="console=ttyS0,115200n8"' | sudo tee -a /etc/default/grub > /dev/null
+  sudo update-grub
+fi
+
 # Fstrim
 # See https://pve.proxmox.com/wiki/Shrink_Qcow2_Disk_Files
 sudo fstrim -av
diff --git a/share/screen/screenrc b/share/screen/screenrc
new file mode 100644 (file)
index 0000000..0a76b80
--- /dev/null
@@ -0,0 +1,85 @@
+# default screenrc file for kvm-manager sessions (copied from cereal's screenrc)
+
+# use C-\ as the escape character
+escape \034\034
+
+logfile ./servicelog
+logfile flush 1
+
+# unset important key bindings
+## prevent external command execution
+bind :
+## prevent opening new windows
+bind c
+bind ^c
+## prevent ability to turn off logging
+bind H
+## prevent terminal reset
+bind Z
+## unset kill commands, sincepreference is to "detach")
+## (should unset "quit" too, or is it useful to have a backup "kill"?)
+bind ^k
+bind k
+bind K
+bind \
+bind \\
+bind ^\
+## no lockscreen:
+bind ^x
+bind x
+## no monitor, prev, next:
+bind M
+bind ^@
+bind n
+bind ^N
+bind ' ' 
+bind ^H
+bind ^P
+bind p
+bind ^?
+# no number:
+bind N
+# no clear:
+bind C
+# no displays or dumptermcap:
+bind *
+bind .
+# no window switching:
+bind -
+bind 0
+bind 1
+bind 2
+bind 3
+bind 4
+bind 5
+bind 6
+bind 7
+bind 8
+bind 9
+## no window splitting:
+bind S
+# no title change
+bind A
+# no remove
+bind X
+# no windows:
+bind ^w
+bind w
+# no suspend:
+bind ^z
+# no focus:
+bind ^i
+# no windowlist:
+bind \"
+
+# set default shell to be /bin/false
+# this is a little redundant with unsetting the create new window keys
+shell /bin/false
+
+# set a nice, informative caption
+caption always "%{= bw}%f %{+b w}KVM console: %{+b y}%t%{-b w} %= %Y-%m-%d %c %= %{-b}\"C-\ d\" to detach"
+
+startup_message off
+defscrollback 1024
+
+defutf8 on