Как я могу запустить usb_modeswitch перед настройкой сети?

1100
Christian Benke

Я использую Huawei E3531 для подключения Raspberry Zero, работающего на vanilla Raspbian 8.0 (Джесси), к Интернету. Так как это для удаленного автономного приложения, оно должно быть в состоянии автоматически вернуться в рабочее состояние после отключения питания.

Я настроил usb_modeswitch для переключения USB-режима на cdc_ether, который надежно выводит eth0 после переключения режимов. К сожалению, usb_modeswitch запускается после того, как сетевые устройства настроены, поэтому сетевой канал не запускается при холодной загрузке (он прекрасно работает при перезагрузке, где режим уже установлен правильно).

По https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/ должно быть возможно добавить network-pre.target -directives к службе, чтобы заставить его работать до настройки сети:

network-pre.target - это цель, которую можно использовать для заказа услуг до того, как будет настроен любой сетевой интерфейс. Его основная цель - использование со службами брандмауэра, которые хотят установить брандмауэр до того, как какой-либо сетевой интерфейс активен.

Это пассивный модуль: вы не можете запустить его напрямую, и он не втягивается службой управления сетью, а службой, которая хочет работать до него. [..] Службы, которые необходимо запустить до настройки сети, должны поместить Before = network-pre.target, а также установить Wants = network-pre.target, чтобы подключить его. Таким образом, если только на самом деле нет службы, которая нуждается в быть заказанным до того, как сеть заработает, цель не будет задействована, что позволит избежать ненужных точек синхронизации.

Я изменил /var/lib/systemd/system/usb_modeswitch@.service и соответственно добавил директивы Before- / Wants -directs:

[Unit] Description=USB_ModeSwitch Before=network-pre.target Wants=network-pre.target  [Service] Type=oneshot ExecStart=/usr/sbin/usb_modeswitch_dispatcher --switch-systemd %I Environment="TMPDIR=/run" 

Что теперь приводит к ошибке «Цикл заказа» при загрузке:

[..] [ OK ] Started Trigger Flushing of Journal to Persistent Storage. [ SKIP ] Ordering cycle found, skipping LSB: Raise network interfaces. [ SKIP ] Ordering cycle found, skipping Network (Pre) [ OK ] Created slice system-usb_modeswitch.slice. [..] 

Вот вывод systemctl show .. :

root@raspberrypi:/lib/systemd/system# systemctl show -p Requires,Wants,Requisite,BindsTo,PartOf,Before,After usb_modeswitch@. Requires=basic.target Requisite= Wants=network-pre.target system-usb_modeswitch.slice BindsTo= PartOf= Before=network-pre.target shutdown.target After=systemd-journald.socket basic.target system-usb_modeswitch.slice  root@raspberrypi:/lib/systemd/system# systemctl show -p Requires,Wants,Requisite,BindsTo,PartOf,Before,After usb_modeswitch@.service Failed to get properties: Unit name usb_modeswitch@.service is not valid. root@raspberrypi:/lib/systemd/system# 

Мне также интересно, почему systemctl show работает с usb_modeswitch @. но не с помощью usb_modeswitch @ .service

Удаление двух строк в сервис-файле восстанавливает старое поведение без SKIP-ошибок.

Есть ли другой способ вызвать сетевые интерфейсы после usb_modeswitch? Нужно ли мне что-то адаптировать в systemd-конфигурации, чтобы это работало?

1

1 ответ на вопрос

1
Christian Benke

I've been able to fix the issue using the following configuration:

[Unit] Description=USB_ModeSwitch DefaultDependencies=no After=local-fs.target systemd-sysctl.service Before=network-pre.target shutdown.target Wants=network-pre.target Conflicts=shutdown.target [Service] Type=oneshot ExecStart=/usr/sbin/usb_modeswitch_dispatcher --switch-systemd %I Environment="TMPDIR=/run" 

I found the solution via this comment on an Ubuntu-bugreport, which refers to a similar problem with shorewall instead of usb_modeswitch.

DefaultDependencies is defined as:

will implicitly complement all configured dependencies of type Wants= or Requires= with dependencies of type After=

I have not tested if this setting is a key part of the configuration or if it can be left out.

--

Here's the full explanation from the aforementioned bug-report (For shorewall instead of usb_modeswitch):

Shorewall does not come with a systemd native service unit description. Such description is being generated at boot by /lib/systemd/system-generators/systemd-sysv-generator based on /etc/init.d/shorewall. I have noticed, however, that the LSB header of /etc/init.d/shorewall wants the service to be started from /etc/rcS.d, which is pretty early, and at the same time it has Required-Start: $network $remote_fs, which is a pretty strong requirement. In fact, this is the only script in /etc/rcS.d that requires $network (well, except shorewall6, which exhibits exactly the same problem). Looking into the auto-generated unit in /run/systemd/generator.late/shorewall.service shows:

DefaultDependencies=no Before=sysinit.target shutdown.target After=network-online.target remote-fs.target Wants=network-online.target Conflicts=shutdown.target 

This looks problematic: sysinit.target is a very early target, most higher level services are started after it, and on many systems (including mine) various dependencies will make network-online.target available only after sysinit.target.

Whatever the exact cause was, this configuration works for me and doesn't seem to have any undesired effects.