debian-forge-composer/internal/osbuild2/loopback_device.go
Christian Kellner 9e5b265a58 osbuild2: lock loopback devices during sfdisk, mkfs
Since udev will probe block devices it is advisable to hold a lock
on the device when modifying its partition table or the superblock
of the filesystem (see [1]). osbuild loopback devices do support
this via the `lock` option. Set this option for all operation that
involve changing block device "metadata" that could potentionally
race with udev, such as sfdisk, mkfs, creating a luks2 container
and creating LVM2 volume groups and logical volumes.
NB: osbuild also has its own device inhibition logic to prevent
udev/lvm2 from auto activating devices and in general to limit the
interaction between the host and devices used by osbuild. See [2]
for more information.
NB: this also locks the loopback device in situation where we the
it is strickly not the right thing to do, e.g. when creating a fs
on a logical voume that is located on a loopback device, since in
this case the device we would need to lock is the logical volume.
Sadly, LVM/DM devices are exempt from block device locking. But,
due to a bug in osbuild < 50, the udev inhibitor does *not* work
for loopback devices and therefore we have to use the actual lock
to preven LVM device auto-activation via `69-dm-lvm-metad.rules`.
The change was implemented by adding a new boolean to `getDevices`
indicating if the loopback device should be locked or not. Once
we depend on osbuild 50 we can change the logic in `getDevices`
to only lock the loopback device if the number of devices is one,
i.e. we are working directly on the loopback device.

[1] https://systemd.io/BLOCK_DEVICE_LOCKING/
[2] /usr/lib/udev/rules.d/10-osbuild-inhibitor.rules
2022-02-28 17:09:30 +01:00

29 lines
702 B
Go

package osbuild2
// Expose a file (or part of it) as a device node
type LoopbackDeviceOptions struct {
// File to associate with the loopback device
Filename string `json:"filename"`
// Start of the data segment
Start uint64 `json:"start,omitempty"`
// Size limit of the data segment (in sectors)
Size uint64 `json:"size,omitempty"`
// Sector size (in bytes)
SectorSize *uint64 `json:"sector-size,omitempty"`
// Lock (bsd lock) the device after opening it
Lock bool `json:"lock,omitempty"`
}
func (LoopbackDeviceOptions) isDeviceOptions() {}
func NewLoopbackDevice(options *LoopbackDeviceOptions) *Device {
return &Device{
Type: "org.osbuild.loopback",
Options: options,
}
}