Skip to content

Commit

Permalink
Evaluate the @root volume name also for btrfs
Browse files Browse the repository at this point in the history
In a volume setup the special volume declaration
<volume name="@root=identifier"/> was only evaluated for the
LVM volume manager. In case of btrfs a hardcoded root volume
name '@' was used. This commit allows to specify a custom
name for the root volume for btrfs as well and also allows
to specify that there should be no such root volume.
Example:

    <volume name="@root=@"/>

Name the root volume '@'. If not specified this stays as
the default to stay compatible

    <volume name="@root=/"/>

Indicate no root volume is wanted. All subvolumes resides
below root (/)

    <volume name="@root=foo"/>

Name the root volume 'foo'

This is related to Issue #2316 and a first patch to
address the requested changes
  • Loading branch information
schaefi committed Jul 12, 2023
1 parent 82688c5 commit 5823f33
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
41 changes: 27 additions & 14 deletions kiwi/volume_manager/btrfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ def setup(self, name=None):
:param string name: unused
"""
root_volume_name = '@'
canonical_volume_list = self.get_canonical_volume_list()
for volume in canonical_volume_list.volumes:
if volume.is_root_volume and volume.name:
root_volume_name = volume.name

self.setup_mountpoint()

filesystem = FileSystem.new(
Expand All @@ -112,31 +118,38 @@ def setup(self, name=None):
Command.run(
['btrfs', 'quota', 'enable', self.mountpoint]
)
root_volume = self.mountpoint + '/@'
Command.run(
['btrfs', 'subvolume', 'create', root_volume]
)
if root_volume_name != '/':
root_volume = self.mountpoint + f'/{root_volume_name}'
Command.run(
['btrfs', 'subvolume', 'create', root_volume]
)
if self.custom_args['root_is_snapshot']:
snapshot_volume = self.mountpoint + '/@/.snapshots'
snapshot_volume = self.mountpoint + \
f'/{root_volume_name}/.snapshots'.replace('//', '/')
Command.run(
['btrfs', 'subvolume', 'create', snapshot_volume]
)
os.chmod(snapshot_volume, 0o700)
Path.create(snapshot_volume + '/1')
snapshot = self.mountpoint + '/@/.snapshots/1/snapshot'
snapshot = self.mountpoint + \
f'/{root_volume_name}/.snapshots/1/snapshot'.replace('//', '/')
Command.run(
['btrfs', 'subvolume', 'create', snapshot]
)
self._set_default_volume('@/.snapshots/1/snapshot')
snapshot = self.mountpoint + '/@/.snapshots/1/snapshot'
# Mount /@/.snapshots as /.snapshots inside the root
snapshots_mount = MountManager(
device=self.device,
mountpoint=snapshot + '/.snapshots'
self._set_default_volume(
f'{root_volume_name}/.snapshots/1/snapshot'.replace('//', '/')
)
self.subvol_mount_list.append(snapshots_mount)
if root_volume_name != '/':
snapshot = self.mountpoint + \
f'/{root_volume_name}/.snapshots/1/snapshot'
# Mount /{root_volume_name}/.snapshots as /.snapshots inside root
snapshots_mount = MountManager(
device=self.device,
mountpoint=snapshot + '/.snapshots'
)
self.subvol_mount_list.append(snapshots_mount)
else:
self._set_default_volume('@')
self._set_default_volume(root_volume_name)

def create_volumes(self, filesystem_name):
"""
Expand Down
5 changes: 4 additions & 1 deletion kiwi/xml_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,9 @@ def get_volumes(self) -> List[volume_type]:
if not have_root_volume_setup:
# There must always be a root volume setup. It will be the
# full size volume if no other volume has this setup
volume_management = self.get_volume_management()
root_volume_name = \
defaults.ROOT_VOLUME_NAME if volume_management == 'lvm' else ''
if have_full_size_volume:
size = 'freespace:' + format(
Defaults.get_min_volume_mbytes()
Expand All @@ -1690,7 +1693,7 @@ def get_volumes(self) -> List[volume_type]:
fullsize = True
volume_type_list.append(
volume_type(
name=defaults.ROOT_VOLUME_NAME,
name=root_volume_name,
size=size,
fullsize=fullsize,
mountpoint=None,
Expand Down
10 changes: 5 additions & 5 deletions test/unit/volume_manager/btrfs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ def inject_fixtures(self, caplog):
def setup(self, mock_path):
self.volumes = [
volume_type(
name='LVRoot', size='freespace:100', realpath='/',
name='@', size='freespace:100', realpath='/',
mountpoint=None, fullsize=False, label=None,
attributes=[], is_root_volume=True
),
volume_type(
name='LVetc', size='freespace:200', realpath='/etc',
name='etc', size='freespace:200', realpath='/etc',
mountpoint='/etc', fullsize=False, label=None,
attributes=[], is_root_volume=False
),
Expand All @@ -42,7 +42,7 @@ def setup(self, mock_path):
attributes=[], is_root_volume=False
),
volume_type(
name='LVhome', size=None, realpath='/home',
name='home', size=None, realpath='/home',
mountpoint='/home', fullsize=True, label=None,
attributes=[], is_root_volume=False
)
Expand Down Expand Up @@ -186,15 +186,15 @@ def test_create_volumes(
),
call(
'tmpdir/@/', volume_type(
name='LVetc', size='freespace:200', realpath='/etc',
name='etc', size='freespace:200', realpath='/etc',
mountpoint='/etc', fullsize=False, label=None,
attributes=[],
is_root_volume=False
)
),
call(
'tmpdir/@/', volume_type(
name='LVhome', size=None, realpath='/home',
name='home', size=None, realpath='/home',
mountpoint='/home', fullsize=True, label=None,
attributes=[],
is_root_volume=False
Expand Down

0 comments on commit 5823f33

Please sign in to comment.