办公场景双主机一套键鼠的问题Office Scenario and KVM: One Keyboard and Mouse Set for Two Hosts
At the office I have a laptop (Linux Mint 22.3, Lenovo V14 G5 IRL) and a desktop workstation (Windows), each with its own monitor and a set of keyboard/mouse. My work requires both machines — basic tasks on Linux, while document formatting and Comsol’s GUI need the Windows workstation’s performance. Running between two desks was annoying, so I tried networking them so some operations could be done from one host, but I still had to switch desks a lot.
在公司,我有一个笔记本(Linux Mint 22.3, Lenovo V14 G5 IRL)和一个台式工作站(Windows),每个都配备一个显示屏和一套键盘鼠标。我的工作需要同时用到这两个电脑,比如一些基本的工作可以在Linux上完成,而修改文档格式、使用Comsol图形化界面则需要用到Windows工作站的性能。我在两个工位之间来回跑很麻烦,我试图接网线让一些操作能在一个主机上完成,但很多时候还是需要换工位完成。
The ideal scenario is using just one monitor and switching the displayed content on it, sharing one set of keyboard and mouse. I first thought of an HDMI switch, but that doesn’t solve the shared keyboard/mouse problem. Then the internet recommended KVM to me, and I immediately felt it was what I needed. I bought a SGEYR KVM (from Xianyu, 40 CNY). It connects two hosts via HDMI cables, then connects to the monitor with another HDMI cable — same as an HDMI switch. Additionally, it connects to both hosts via USB and applies the keyboard/mouse plugged into it to whichever host is active.
最好的情况是我只用一个显示屏,可以在上面切换显示内容,共用一套键盘鼠标。我一开始想到了HDMI切换器,但是它无法解决共用一套键鼠的问题。后来网络给我推送了KVM,我直觉地感到这就是我要的东西,我购买了一个SGEYR的KVM(闲鱼上买的,40¥),它能通过HDMI线连接两个主机,再用一根HDMI线连接到显示屏,这部分和HDMI切换器是一样的,进一步地,它还能分别通过USB连接到两个主机,把插在自身上的键盘鼠标应用到上面。
The desktop workstation’s original monitor stays connected. Both hosts connect to the original laptop monitor through the KVM switch. Problem: since the desktop monitor remains, switching to the workstation is fine, but then Linux goes to sleep. Switching back, keyboard and mouse are also dead — I have to open the laptop lid to wake it up. This is because my old-style KVM doesn’t have the feature of simulating a monitor that’s never unplugged. I didn’t want to tinker with hardware anymore; this must be solvable with software. My goal was to keep Linux running when the KVM switches away. So I had AI research GitHub repos, found the twshield/Fedora-EDID-KVM-config project, and finally solved the problem.
台式工作站原本的屏幕仍然保留,两个主机都通过KVM切换器连接到原先笔记本的那个显示屏上。结果出现了问题,因为台式机工作站屏幕保留了,每次切换到工作站都没事,但是这时候Linux却陷入了休眠,再切回去键盘和鼠标也都瘫痪了,还得再打开笔记本合盖唤醒。这是因为我用的是老式KVM,不具备模拟显示屏没有拔出的功能。我不想再折腾硬件了,这个肯定可以通过软件解决,我的目的就是能让KVM切换回Linux时其仍在保持运行。所以我让AI调研了Github仓库,找到了twshield/Fedora-EDID-KVM-config这个项目,并最终解决了这个问题。
The core dependencies are initramfs-tools + update-grub (Debian/Ubuntu GRUB wrapper) + systemd-logind drop-in. Any distro that has all three can basically “rename the connector and go”; ones that don’t need the corresponding component commands replaced.
核心依赖是 initramfs-tools + update-grub(Debian/Ubuntu 系的 GRUB 封装)+ systemd-logind drop-in。凡是同时具备这三件套的发行版,基本可以"改个连接器名字直接用";不具备的,需要替换对应的组件命令。
不同Linux发行版适配Adapting to Different Linux Distributions
Besides Linux Mint, the following distros use initramfs-tools and update-grub by default, and the logind HandleLidSwitch* drop-in mechanism is identical:
除了 Linux Mint,以下发行版默认使用 initramfs-tools 和 update-grub,logind 的 HandleLidSwitch* drop-in 机制也完全一致:
-
Debian (default initramfs generator is
initramfs-tools,update-grubis Debian’s GRUB wrapper) -
Ubuntu (before 25.10,
initramfs-tools+update-grubis the standard combo) -
Pop!_OS (based on Ubuntu, explicitly depends on
initramfs-tools) -
Zorin OS (based on Ubuntu, uses
initramfs-toolsandupdate-initramfs -u) -
elementary OS (based on Ubuntu, uses
initramfs-tools) -
KDE neon, Deepin, and other Ubuntu/Debian derivatives — as long as they haven’t swapped the initramfs generator.
-
Debian(默认 initramfs 生成器就是
initramfs-tools,update-grub是 Debian 的 GRUB 封装命令) -
Ubuntu(25.10 之前,
initramfs-tools+update-grub是标准组合) -
Pop!_OS(基于 Ubuntu,明确依赖
initramfs-tools) -
Zorin OS(基于 Ubuntu,使用
initramfs-tools和update-initramfs -u) -
elementary OS(基于 Ubuntu,使用
initramfs-tools) -
KDE neon、Deepin 等 Ubuntu/Debian 衍生版,只要它们没有擅自替换 initramfs 生成器,基本同理。
Only watch out for the connector name (HDMI-A-1 is common on Intel/AMD drivers, but check the actual name under /sys/class/drm/) and whether the desktop environment overrides logind. If Cinnamon/GNOME/KDE’s own power manager has its own lid-close policy, the logind.conf.d drop-in might be overridden — also check in desktop settings.
需要注意的只是 连接器名(HDMI-A-1 在 Intel/AMD 驱动下常见,但具体要按 /sys/class/drm/ 里的实际名字改)和 桌面环境是否覆盖 logind。如果 Cinnamon/GNOME/KDE 自己的电源管理器另有合盖策略,logind.conf.d 的 drop-in 可能被上层覆盖,需要同时在桌面设置里确认。
The following distros also use systemd-logind and the /etc/default/grub + grub*-mkconfig kernel parameter injection logic is the same, but the initramfs generator is NOT initramfs-tools — your /etc/initramfs-tools/hooks/edid won’t work, and update-initramfs -u doesn’t exist:
以下发行版也用 systemd-logind,/etc/default/grub + grub*-mkconfig 的内核参数注入逻辑也通用,但 initramfs 生成器不是 initramfs-tools,你的 /etc/initramfs-tools/hooks/edid 不会生效,update-initramfs -u 也不存在:
-
Arch Linux / EndeavourOS / Manjaro / CachyOS / Garuda: default initramfs generator is
mkinitcpio. To put EDID into initramfs, add/lib/firmware/edid/lecoo-m2721u.bintoFILES=(...)in/etc/mkinitcpio.conf, then runsudo mkinitcpio -P. For GRUB, Arch family typically usesgrub-mkconfig -o /boot/grub/grub.cfg;update-grubmay not exist. -
Fedora / RHEL family: use dracut. Write
install_items+=" /lib/firmware/edid/lecoo-m2721u.bin "in/etc/dracut.conf.d/, thendracut -f. GRUB usesgrub2-mkconfig -o /boot/grub2/grub.cfg, notupdate-grub. -
openSUSE: modern versions use dracut underneath (
mkinitrdis actually a dracut wrapper). GRUB usesgrub2-mkconfig -o /boot/grub2/grub.cfg. TheHandleLidSwitch*logind settings work the same. Just replace the initramfs step with dracut/mkinitrd syntax. -
Arch Linux / EndeavourOS / Manjaro / CachyOS / Garuda:默认 initramfs 生成器是
mkinitcpio。要把 EDID 放进 initramfs,在/etc/mkinitcpio.conf的FILES=(...)里加入/lib/firmware/edid/lecoo-m2721u.bin,然后运行sudo mkinitcpio -P。GRUB 用grub-mkconfig -o /boot/grub/grub.cfg。 -
Fedora / RHEL 系:用
/etc/dracut.conf.d/写install_items+=" /lib/firmware/edid/lecoo-m2721u.bin ",然后dracut -f。GRUB 用grub2-mkconfig -o /boot/grub2/grub.cfg。 -
openSUSE:底层使用 dracut,GRUB 用
grub2-mkconfig -o /boot/grub2/grub.cfg。
Starting from Ubuntu 25.10 (Questing Quokka), Canonical uses dracut instead of initramfs-tools by default. Your initramfs-tools hook and update-initramfs -u will no longer work — migrate to dracut’s install_items+ syntax. Mint based on Ubuntu 25.10+ will also be affected.
从 Ubuntu 25.10(Questing Quokka) 开始,Canonical 默认使用 dracut 替代 initramfs-tools。你的 initramfs-tools hook 和 update-initramfs -u 将不再适用,需要迁移到 dracut 的 install_items+ 写法。Mint 如果未来基于 Ubuntu 25.10+,也会受影响。
KVM切换的机制How KVM Switching Works
DP (DisplayPort) and HDMI connect the host electrically via differential signals. The host reads the HPD (Hot Plug Detect) signal line to determine if a monitor is connected, and reads the DDC channel (Display Data Channel) for the monitor’s supported resolution, timing, and color info (EDID — Extended Display Identification Data). DP uses I²C over AUX. The kernel interprets this display connection state, which then affects power decisions (is there an external monitor, or should it sleep?).
先来说说为什么会导致休眠。硬件上,DP(Display Port)和HDMI(High-Definition Multimedia Interface)通过差分信号电气连接主机,主机通过读取HPD(Hot Plug Detect)这根信号线来确定是否有显示屏接入,通过DDC通道(Display Data Channel)读取这个显示屏支持什么分辨率、时序和色彩等(EDID, Extended Display Identification Data),其中DP线是走AUX上的I²C。这个信号需要内核来理解显示连接状态,然后影响到了电源决策(到底是有外接显示器还是休眠)。
What a KVM does is independently control each host’s HPD pull-up path. Old-style KVMs, when switching hosts, simply disconnect HPD and don’t maintain EDID — the host still concludes the monitor is unplugged. Newer KVMs have an internal EDID store that continues to respond to EDID reads, making the host think the monitor is still present (just not refreshing the display — HPD is disconnected). Once HPD goes low and EDID can’t be read, the kernel tells userspace “external monitor disappeared,” and the lid-close policy triggers sleep.
而KVM做的是,独立控制每个主机HPD的上拉通路,老式KVM切换主机时,直接断开HPD,也不维持EDID,仍然会导致主机判定显示器已经断开,而新的KVM则内部有独立的EDID存储器,会继续应答EDID的读取,使主机认为显示器仍然在场,只是画面不刷新了(HPD断开)。一旦HPD被拉低,而EDID又读取不到,kernel就告诉用户空间"外接显示屏消失了",合盖策略随即变成休眠。
One fix is to change the lid-close policy directly (HandleLidSwitchDocked=ignore), preventing userspace from changing behavior just because the external screen disappeared. But after EDID is gone the display environment changes, and the desktop compositor rearranges windows anyway.
有一种解决方法就是直接改变合盖策略(HandleLidSwitchDocked=ignore),让用户空间不因为外界屏幕没了就改变合盖策略。问题是EDID没了之后显示环境变了,桌面合成器也跟着重新排列窗口了。
USB 2.0 keyboard/mouse use D+/D- differential signal lines. The host identifies devices through level changes and differential handshakes on D+/D-, reads descriptors, configures them as HID devices, and grants wake permission. Then during system suspend, it can send resume signals via D+/D-, report to the host controller, and the kernel restores the system.
再来说说为什么休眠之后不能再用KVM连接的键盘鼠标来唤醒。USB 2.0的键盘鼠标走的是D+/D-差分信号线,主机通过D+/D-上的电平变化和差分握手识别设备,读取描述符,并将其配置为一个HID设备,然后给它唤醒权限,之后系统suspend时即可通过D+/D-发出resume状态,上报到主机控制器再由内核恢复系统。
Newer KVMs have USB virtualization that maintains virtual keyboard/mouse on the host port continuously, so the host still has a wake-capable HID device during sleep. But old-style KVMs physically disconnect the keyboard’s D+/D- from the switched-away host when switching — for the laptop that was switched away, the USB device is unplugged, the HID device is gone, and the wake permission is lost. Result: keyboard/mouse are no longer on the USB bus and the system sleeps. Even though physically connected, the host thinks there are no present keyboard/mouse and no usable external wake source.
新的KVM里有USB虚拟化,会持续在主机端口上维持虚拟键鼠,让主机在休眠时仍有可唤醒的HID设备。但是老式KVM切换主机时,把键盘的D+/D-从被切走的主机上物理断开,去接通另一台主机了,这时候对于切走的笔记本,USB设备被拔除了,HID设备也没有了,唤醒权限也丢失了。结果是键盘鼠标已经不再USB总线上就陷入休眠了。即便物理上连接着,但是主机觉得已经没有在场的鼠标键盘和可用的外部唤醒源了。
Fedora-EDID-KVM-config的解决方案The Fedora-EDID-KVM-config Solution
I want the KVM switch-away machine to keep running in the background. Solving the sleep-on-switch problem naturally solves the can’t-wake problem too. The twshield/Fedora-EDID-KVM-config project feeds a pre-prepared EDID to the DRM subsystem (Direct Rendering Manager) via kernel parameter (drm.edid_firmware=) on Fedora, making the kernel always think the monitor is present.
我希望切走KVM后后台仍然运行,因此应该解决切走导致休眠,切走无法唤醒的问题也就自然消解了。twshield/Fedora-EDID-KVM-config这个项目就是在Fedora系统上,通过内核参数(drm.edid_firmware=)把一份预先准备好的EDID"喂"给DRM子系统(Direct Rendering Manager),始终认为显示器在场。
The boot chain is: power on → firmware (BIOS/UEFI) → GRUB → Linux kernel → initramfs → root filesystem → userspace (systemd, logind, desktop).
启动链条是这样的:上电 → 固件(BIOS/UEFI)→ GRUB → Linux 内核 → initramfs → 根文件系统 → 用户态(systemd、logind、桌面)。
So the main task is to modify the file’s parameters that GRUB passes to the Linux kernel. This file is generated from a template, so ultimately we modify the GRUB template and regenerate the config file — after reboot, the kernel re-reads it.
所以主要做的是,修改GRUB传给Linux内核的文件内的参数,而这个是通过读取文件完成的,这个文件又是通过模板生成的,最终需要修改GRUB中的模板,重新生成配置文件,这样重启后内核就会重新读取了。
Step-by-Step on Linux Mint
I’m using Linux Mint, which is the same as Fedora in this part. First, read the EDID and save it to /tmp/edid-work/monitor.bin:
我使用的是Linux Mint,在这部分和Fedora是一样的,因此只需要读取EDID并保存在/tmp/edid-work/monitor.bin:
sudo -n true 2>&1; echo "sudo_exit=$?"; ls -l /sys/class/drm/card1-HDMI-A-1/edid; mkdir -p /tmp/edid-work && cat /sys/class/drm/card1-HDMI-A-1/edid > /tmp/edid-work/monitor.bin && wc -c /tmp/edid-work/monitor.bin && xxd /tmp/edid-work/monitor.bin | head -5
Verify and read the EDID — the monitor is LECOO M2721U, optimal resolution 3840x2160:
验证和读取EDID,显示屏是LECOO M2721U,最佳分辨率是3840x2160:
python3 - <<'PY'
import struct
edid=open('/tmp/edid-work/monitor.bin','rb').read()
print('header_ok', edid[:8]==b'\x00\xff\xff\xff\xff\xff\xff\x00')
mfg=struct.unpack('>H', edid[8:10])[0]
c1=chr(((mfg>>10)&0x1f)+64); c2=chr(((mfg>>5)&0x1f)+64); c3=chr((mfg&0x1f)+64)
print('manufacturer', c1+c2+c3)
print('product', hex(struct.unpack('<H', edid[10:12])[0]))
print('serial', hex(struct.unpack('<I', edid[12:16])[0]))
for i in range(4):
off=54+i*18
blk=edid[off:off+18]
if blk[0]==0 and blk[1]==0 and blk[3]==0xfc:
print('monitor_name', blk[5:18].split(b'\n')[0].decode('ascii','replace').strip())
if blk[0]==0 and blk[1]==0 and blk[3]==0xff:
print('serial_str', blk[5:18].split(b'\n')[0].decode('ascii','replace').strip())
print('checksum_ok', sum(edid[:128])%256==0, 'ext_blocks', edid[126])
if len(edid)>=256:
print('ext_checksum_ok', sum(edid[128:256])%256==0)
dtd=edid[54:72]
if dtd[0] or dtd[1]:
hactive=dtd[2]+((dtd[4]&0xf0)<<4)
vactive=dtd[5]+((dtd[7]&0xf0)<<4)
print('preferred_mode', f'{hactive}x{vactive}')
PY
# header_ok True
# manufacturer LEC
# product 0x2757
# serial 0x1010101
# monitor_name LECOO M2721U
# serial_str GE1DR108
# checksum_ok True ext_blocks 1
# ext_checksum_ok True
# preferred_mode 3840x2160
The monitor is LECOO M2721U (3840x2160). Now write the install script: EDID firmware + kernel parameters + disable lid-close sleep.
显示器是 LECOO M2721U(3840x2160)。接下来写安装脚本:EDID 固件 + 内核参数 + 禁止合盖休眠。
#!/bin/bash
# EDID + KVM keep-alive setup for Linux Mint (adapted from Fedora-EDID-KVM-config)
set -euo pipefail
EDID_SRC="/tmp/edid-work/monitor.bin"
EDID_NAME="lecoo-m2721u.bin"
EDID_DIR="/lib/firmware/edid"
EDID_PATH="${EDID_DIR}/${EDID_NAME}"
CONNECTOR="HDMI-A-1"
VIDEO_MODE="3840x2160@60e"
echo "==> 1. Install EDID firmware"
mkdir -p "$EDID_DIR"
cp "$EDID_SRC" "$EDID_PATH"
chmod 644 "$EDID_PATH"
echo " installed: $EDID_PATH ($(wc -c < "$EDID_PATH") bytes)"
echo "==> 2. Initramfs hook (Ubuntu/Mint uses initramfs-tools, not dracut)"
cat > /etc/initramfs-tools/hooks/edid <<'HOOK'
#!/bin/sh
PREREQ=""
prereqs() { echo "$PREREQ"; }
case "$1" in
prereqs) prereqs; exit 0 ;;
esac
. /usr/share/initramfs-tools/hook-functions
if [ -d /lib/firmware/edid ]; then
mkdir -p "${DESTDIR}/lib/firmware/edid"
cp -a /lib/firmware/edid/. "${DESTDIR}/lib/firmware/edid/"
fi
exit 0
HOOK
chmod 755 /etc/initramfs-tools/hooks/edid
echo " hook: /etc/initramfs-tools/hooks/edid"
echo "==> 3. GRUB kernel parameters"
if [ ! -f /etc/default/grub.bak-edid-kvm ]; then
cp /etc/default/grub /etc/default/grub.bak-edid-kvm
echo " backup: /etc/default/grub.bak-edid-kvm"
fi
python3 - <<'PY'
from pathlib import Path
p = Path("/etc/default/grub")
text = p.read_text()
add = "video=HDMI-A-1:3840x2160@60e drm.edid_firmware=HDMI-A-1:edid/lecoo-m2721u.bin"
out = []
done = False
for line in text.splitlines():
if line.startswith("GRUB_CMDLINE_LINUX_DEFAULT="):
if "drm.edid_firmware" in line:
done = True
out.append(line)
continue
inner = line.split("=", 1)[1].strip().strip('"')
new = f'{inner} {add}'.strip()
out.append(f'GRUB_CMDLINE_LINUX_DEFAULT="{new}"')
done = True
else:
out.append(line)
if not done:
out.append(f'GRUB_CMDLINE_LINUX_DEFAULT="quiet splash {add}"')
p.write_text("\n".join(out) + "\n")
print(" updated GRUB_CMDLINE_LINUX_DEFAULT")
PY
echo "==> 4. logind: never suspend on lid close (KVM undock while lid closed)"
mkdir -p /etc/systemd/logind.conf.d
cat > /etc/systemd/logind.conf.d/kvm-lid.conf <<'LOGIND'
[Login]
HandleLidSwitch=ignore
HandleLidSwitchExternalPower=ignore
HandleLidSwitchDocked=ignore
LOGIND
echo " drop-in: /etc/systemd/logind.conf.d/kvm-lid.conf"
echo "==> 5. Rebuild initramfs + GRUB"
update-initramfs -u -k "$(uname -r)"
update-grub
echo
echo "Done. Please REBOOT for kernel parameters + EDID to take effect."
echo "After reboot verify with:"
echo " cat /proc/cmdline"
echo " dmesg | grep -i edid"
echo " for d in /sys/class/drm/card1-*/; do echo \$(basename \$d) \$(cat \$d/status); done"