AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1142417
Accepted
Ali Razmdideh
Ali Razmdideh
Asked: 2019-05-12 07:38:04 +0800 CST2019-05-12 07:38:04 +0800 CST 2019-05-12 07:38:04 +0800 CST

如何在启动时使用服务设置循环设备?

  • 772

我正在使用以下命令为 VM 原始映像备份文件设置循环设备:

sudo losetup /dev/loop0 "/home/asus/VirtualBox VMs/Windows RAW/Windows 10.img" 

它工作正常。

$ losetup -l
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                                            DIO LOG-SEC
/dev/loop0         0      0         0  0 /home/asus/VirtualBox VMs/Windows RAW/Windows 10.img   0     512

但我想让这个更改永久化,所以我创建了这个.sevice在启动时运行:

$ cat /etc/systemd/system/loops-setup.service
[Unit]
Description=Setup loop devices
DefaultDependencies=no
Conflicts=umount.target
Before=local-fs.target
After=systemd-udev-settle.service
Required=systemd-udev-settle.service

[Service]
Type=oneshot
ExecStart=/sbin/losetup /dev/loop0 "/home/asus/VirtualBox VMs/Windows RAW/Windows 10.img"
ExecStop=/sbin/losetup -d /dev/loop0
TimeoutSec=60
RemainAfterExit=yes

[Install]
WantedBy=local-fs.target
Also=systemd-udev-settle.service

此服务也可以手动启动:

sudo systemctl start loops-setup

但它在启动时无法启动,并显示此错误:

May 11 19:57:17 ubuntu losetup[308]: losetup: /dev/loop0: failed to set up loop device: No such file or directory

我猜是因为服务的层次结构,我应该改变Before和After选择,但我不知道该怎么做。

提前致谢。

mount services systemd loop-device
  • 1 1 个回答
  • 5017 Views

1 个回答

  • Voted
  1. Best Answer
    Ali Razmdideh
    2019-05-12T09:58:00+08:002019-05-12T09:58:00+08:00

    这个问题是由两个原因引起的:

    1.正如@CharlesGreen提到的systemd-udev-settle.service无法激活:

    $ sudo systemctl enable systemd-udev-settle.service
    The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
    Alias= settings in the [Install] section, and DefaultInstance= for template
    units). This means they are not meant to be enabled using systemctl.
    
    Possible reasons for having this kind of units are:
    • A unit may be statically enabled by being symlinked from another unit's
      .wants/ or .requires/ directory.
    • A unit's purpose may be to act as a helper for some other unit which has
      a requirement dependency on it.
    • A unit may be started when needed via activation (socket, path, timer,
      D-Bus, udev, scripted systemctl call, ...).
    • In case of template units, the unit is meant to be enabled with some
      instance name specified.
    

    因此,此服务保持非活动状态:

    $ sudo systemctl status systemd-udev-settle.service
    ● systemd-udev-settle.service - udev Wait for Complete Device Initialization
       Loaded: loaded (/usr/lib/systemd/system/systemd-udev-settle.service; static; vendor preset: disabled)
       Active: inactive (dead)
         Docs: man:udev(7)
               man:systemd-udevd.service(8)
    

    2.第二个问题是losetup命令需要访问home分区才能设置Windows 10.img。并且这个分区在启动时没有挂载。


    因此以这种方式更改服务,解决了问题:

    [Unit]
    Description=Setup loop devices
    DefaultDependencies=no
    Conflicts=umount.target
    Before=local-fs.target
    After=systemd-udevd.service home.mount
    Required=systemd-udevd.service
    
    [Service]
    Type=oneshot
    ExecStart=/sbin/losetup /dev/loop0 "/home/asus/VirtualBox VMs/Windows RAW/Windows 10.img"
    ExecStop=/sbin/losetup -d /dev/loop0
    TimeoutSec=60
    RemainAfterExit=yes
    
    [Install]
    WantedBy=local-fs.target
    Also=systemd-udevd.service
    
    • 2

相关问题

  • 是否有更好的方式为 /home/userX 构建分区/挂载

  • 当另一个文件系统安装在路径上时是否可以删除文件?

  • 延长笔记本电脑和笔记本电脑电池寿命的技巧

  • 命令列出启动时启动的服务?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve