我們有一個伺服器(管理程式)託管許多由 libvirt 控制的虛擬機器。 libvirt 為虛擬機器和虛擬機器管理程式之間的通訊提供了虛擬網路。此虛擬網路由運行時建立libvirtd.service
並稱為sys-devices-virtual-net-virbr1.device
。
在虛擬機器管理程式上執行的一些服務應該綁定到該虛擬網絡,例如 IMAP、資料庫等。
我們面臨的問題是,在虛擬機器管理程式上執行的服務是在虛擬網路設定之前由 systemd 啟動的,因此服務啟動失敗。目前的啟動順序如下圖所示:
對於這樣的情況,有 systemd 指令,例如
After=
Required=
Wants=
等等。但它們對我的情況沒有幫助。據我了解,原因是它是在運行時動態sys-devices-virtual-net-virbr1.device
創建的libvirtd.service
,並且在系統啟動時 systemd 不知道。因此,由於sys-devices-virtual-net-virbr1.device
網路介面未知,systemd 無法在啟動時解決依賴關係。
systemd 是否提供了解決此類問題的技術?
任何提示表示讚賞。謝謝。
答案1
您可以將服務單元配置為依賴裝置單元,即使該裝置單元未在另一個單元中明確定義(例如,它來自 udev)。
[Unit]
BindsTo=sys-devices-virtual-net-virbr1.device
After=sys-devices-virtual-net-virbr1.device
正如你在 man systemd.unit 中看到的
綁定到=
Configures requirement dependencies, very similar in style to Requires=. However, this dependency type is stronger: in addition to the effect of Requires= it declares that if the unit bound to is stopped, this unit will be stopped too.
When used in conjunction with After= on the same unit the behaviour of BindsTo= is even stronger. In this case, the unit bound to strictly has to be in active state for this unit to also be in active state