libvirt-chardev

Serial/Parallel/Console

Serial Port vs Parallel Port

Serial port is used to connect a serial device to the computer and capable of transmitting one bit at a time.

serial port used for

  • Mouse - One of the most commonly used devices for serial ports, usually used with computers with no PS/2 or USB ports and specialty mice.

Parallel port is used to connect a parallel device to the computer and capable of transmitting 8 bits at a time.
parallel port used for?

Today, the parallel port has widely been replaced by the USB port. However, below is a listing of various hardware components that were used with the parallel port

  • Printer - The most common use for the parallel port.
  • Scanner - Another commonly used parallel device is a parallel port scanner. Parallel port scanners are a popular alternative to SCSI scanners because of how easy they are to install.

The main difference between a serial port and a parallel port is that a serial port transmits data one bit after another, while a parallel port transmits all 8 bits of a byte in parallel. Thus a parallel port transmits data much faster than a serial port.

The ports COM1 and COM2 on your computer are serial ports and the LPT1 port is a parallel port.

Common serial port(builtin motherboard/isa serial) names are /dev/ttyS0, /dev/ttyS1, etc. Then around the year 2000 came the USB bus with names like /dev/ttyUSB0 and /dev/ttyACM1 (for the ACM modem on the USB bus). Multiport serial card used somewhat differnt names (depending on the brand) such as /dev/ttyE5.

1
2
3
4
5
6
7
8
9
10
# check how many serial ports that a PC has
$ ls /dev/ttyS*
/dev/ttyS0 /dev/ttyS1 /dev/ttyS2 /dev/ttyS3

# if it's usb serial port(converted usb port to serial port)
$ ls /dev/USB*
/dev/ttyUSB0 /dev/USB1 /dev/USB2 /dev/USB3

# check perallel ports
$ ls /dev/lp*

Console

The physical interface of console port is RJ45(but not ethernet port which is also RJ45 like management port), the other end is always PC serial port. you can’t telnet/SSH into a console port. Console port is an “up close and personal” port: You need to have physical access to the appliance in order to use the console port.

PARAMETER CONSOLE PORT MANAGEMENT PORT
IP address Assignment Can’t give IP address to console Port IP address can be given to a management port
Communication Type Asynchronous Synchronous
Remote access via Telnet/SSH No Yes
Access required Physical access to device required IP reachability and TCP port 23 (for  telnet) or  TCP port 443 (for SSH) required or HTTP (80)
Segregation type Physically separate connection Generally a VRF based traffic segregation
Maximum Speed 0.1 Mbps (115200 bps) 1 Gbps
Connectivity Type Serial , DB9 , Rj45 RJ45
Management type Out of Band Management Out of Band Management
Boot Sequence Shows Boot sequence Does not show boot sequence
SNMP, Logging on interface No SNMP, syslog configurable on console interface SNMP, syslog configurable on management interface
Application required HyperTerminal Telnet/SSH Web GUI

Console (Serial) Port: Connect to a PC with a serial adapter cable, so that we can get output from console to PC which connected with serial port.

console vs tty vs pty(pts)

  • tty = text input/output environment(tty is an environment)
  • console = physical terminal

TTY

In unix terminology, a tty is a particular kind of device file which implements a number of additional commands (ioctls) beyond read and write. In its most common meaning, terminal is synonymous with tty. Some ttys are provided by the kernel on behalf of a hardware device, for example with the input coming from the keyboard and the output going to a text mode screen, or with the input and output transmitted over a serial line. Other ttys, sometimes called pseudo-ttys(pts), are provided (through a thin kernel layer) by programs called terminal emulators, such as Ssh (which connects a terminal on one machine with programs on another machine), Expect (for scripting terminal interactions), etc.

The console appears to the operating system as a (kernel-implemented) tty. On some systems, such as Linux and FreeBSD, the console appears as several ttys (special key combinations switch between these ttys); just to confuse matters, the name given to each particular tty can be “console”, ”virtual console”, ”virtual terminal”, and other variations.

PTY
A pty is a pseudo-terminal - it’s a software implementation that appears to the attached program like a terminal, but instead of communicating directly with a “real” terminal, it transfers the input and output to another program. For example, when you ssh in to a machine and run ls, the ls command is sending its output to a pseudo-terminal, the other side of which is attached to the SSH daemon. A pts is the slave part of a pty. A ptmx is the master part of a pty

/dev/ptmx The idea of ptmx is that your application creates a virtual console for communication with other applications or with the operating system. By opening ptmx, an application gets a filed descriptor (basically a number) which gives your application the possibility to communicate over a virtual terminal with other applications. These other applications can open your terminal by opening /dev/pts/12345 for example.

The only function of /dev/ptmx is to provide your application with a file descriptor of a newly created /dev/pts/ device which can be used by echo.

PTS

  • Stands for pseudo terminal slave.
  • A pts is the slave part of a pty.
  • A pty (pseudo terminal device) is a terminal device which is emulated by an other program (example: xterm, screen, or ssh are such programs).
  • /dev/pts contains entries corresponding to devices. /dev/pts is a special directory that is created dynamically by the Linux kernel. The contents of the directory vary with time and reflect the state of the running system.
  • The entries in /dev/pts correspond to pseudo-terminals (or pseudo-TTYs, or PTYs).
  • In laymen terms the primary difference between TTY and PTS is the type of connection to the computer. TTY ports are direct connections to the computer such as a keyboard/mouse or a serial connection to the device. PTS connections are SSH connections or telnet connections. All of these connections can connect to a shell which will allow you to issue commands to the computer.

CharDev

A character device provides a way to interact with the virtual machine. Paravirtualized consoles, serial ports, parallel ports and channels are all classed as character devices and so represented using the same syntax.

To specify the consols, channel and other devices configuration settings, use a management tool to make the following changes to the domain XML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<devices>
<!--serial device -->
<serial type='file'>
<source path='/tmp/serial.log'/>
<target type='isa-serial' port='0'> <!--/dev/ttyS0-->
<model name='isa-serial'/>
</target>
<alias name='serial0'/>
</serial>
<!--console for early boot message-->
<console type='file'>
<source path='/tmp/serial.log'/>
<target type='serial' port='0'/> <!--/dev/ttyS0-->
<alias name='serial0'/>
</console>

<!--virtio console-->
<!--/dev/hvc0, after system boot and loaded virtio driver-->
<console type='pty'>
<source path='/dev/pts/5'/>
<target type='virtio' port='1'/>
<alias name='console1'/>
</console>
<!--virtio console-->

<channel type='unix'>
<source mode='bind' path='/tmp/agent'/>
<target type='virtio' name='virtio.serial.port0' state='disconnected'/>
<alias name='channel1'/>
<address type='virtio-serial' controller='0' bus='0' port='2'/>
</channel>
<channel type='pty'>
<source path='/dev/pts/3'/>
<target type='virtio' name='virtio.serial.port1' state='disconnected'/>
<alias name='channel2'/>
<address type='virtio-serial' controller='0' bus='0' port='3'/>
</channel>
</devices>

In each of these directives, the top-level element name (parallel, serial, console, channel) describes how the device is presented to the guest virtual machine. The guest virtual machine interface is configured by the target element.

Console(which is a serial as well)
libvirt supports two console types: serial and virtio, you can use one or both at same time, but dynamic adding is not supported for serial console, virsh attach is not allowed for it, Using the serial type for the console, you get the usual /dev/ttyS0 console. Using virtio, you get /dev/hvc0 as the console device

  • serial console(NOTE: the backend can be pts or file)
    1
    2
    3
    4
    <console type='pty'> <!--on host auto select one-->
    <target type='serial' port='0'/> <!--insde vm /dev/ttyS0-->
    <alias name='console0'/>
    </console>
  • virtio console(NOTE: the backend can be pts or file)
    1
    2
    3
    4
    5
    <console type='pty'>
    <source path='/dev/pts/5'/><!--specify the pts, not auto selecting-->
    <target type='virtio' port='1'/><!--inside vm /dev/hvc0-->
    <alias name='console1'/>
    </console>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
####################check console#################################
# on the guest
$ echo hello > /dev/console

# on the host
$ tail -f /tmp/serial.log
hello

# on host(quit screen ctrl + a, then press \)
$ screen /dev/pts/5
# on guest
$ echo hello >/dev/hvc0


####################check serial################################
# on the guest
$ echo hello >/dev/ttyS0
# on the host
$ tail -f /tmp/serial.log
hello
####################check channel################################

# check virtio-ports with name set by user(actuall it's link to /dev/vport0p2)
$ ls -al /dev/virtio-ports/
total 0
drwxr-xr-x. 2 root root 100 Aug 8 09:10 .
drwxr-xr-x. 19 root root 3220 Aug 8 09:10 ..
lrwxrwxrwx. 1 root root 11 Aug 8 09:10 virtio.serial.port0 -> ../vport0p2
lrwxrwxrwx. 1 root root 11 Aug 8 09:10 virtio.serial.port1 -> ../vport0p3

$ ls /sys/class/virtio-ports/vport0p3/
dev device name power subsystem uevent
$ cat /sys/class/virtio-ports/vport0p3/name
virtio.serial.port1

# on host
$ screen /dev/pts/3

hello

# inside guest
$ echo hello >/dev/vport0p3

# on host
$ socat - UNIX-CONNECT:/tmp/agent
hello

# inside guest
$ echo hello >/dev/vport0p2