Skip to content

Commit 1a2bfaf

Browse files
committed
Adding a path split based on snapper version
Signed-off-by: David Cassany <[email protected]>
1 parent 15259bb commit 1a2bfaf

File tree

2 files changed

+70
-31
lines changed

2 files changed

+70
-31
lines changed

kiwi/volume_manager/btrfs.py

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from kiwi.mount_manager import MountManager
3232
from kiwi.storage.mapped_device import MappedDevice
3333
from kiwi.filesystem import FileSystem
34+
from kiwi.utils.command_capabilities import CommandCapabilities
3435
from kiwi.utils.sync import DataSync
3536
from kiwi.utils.block import BlockID
3637
from kiwi.utils.sysconfig import SysConfig
@@ -541,40 +542,46 @@ def _set_snapper_sysconfig_file(root_path):
541542
)
542543

543544
def _create_first_snapper_snapshot_as_default(self):
544-
snapshot_volume = self.mountpoint + \
545-
f'/{self.root_volume_name}/.snapshots'
546-
Command.run(
547-
['btrfs', 'subvolume', 'create', snapshot_volume]
548-
)
549-
os.chmod(snapshot_volume, 0o700)
550-
Path.create(snapshot_volume + '/1')
551-
snapshot = self.mountpoint + \
552-
f'/{self.root_volume_name}/.snapshots/1/snapshot'
553-
Command.run(
554-
['btrfs', 'subvolume', 'create', snapshot]
555-
)
556-
self._set_default_volume(
557-
f'{self.root_volume_name}/.snapshots/1/snapshot'
558-
)
545+
if not CommandCapabilities.check_version(
546+
'snapper', (0,12,1), root=self.root_dir
547+
):
548+
snapshot_volume = self.mountpoint + \
549+
f'/{self.root_volume_name}/.snapshots'
550+
Command.run(
551+
['btrfs', 'subvolume', 'create', snapshot_volume]
552+
)
553+
os.chmod(snapshot_volume, 0o700)
554+
Path.create(snapshot_volume + '/1')
555+
snapshot = self.mountpoint + \
556+
f'/{self.root_volume_name}/.snapshots/1/snapshot'
557+
Command.run(
558+
['btrfs', 'subvolume', 'create', snapshot]
559+
)
560+
self._set_default_volume(
561+
f'{self.root_volume_name}/.snapshots/1/snapshot'
562+
)
559563

560564
def _create_snapshot_info(self, path):
561-
date_info = datetime.datetime.now()
562-
snapshot = ElementTree.Element('snapshot')
565+
if not CommandCapabilities.check_version(
566+
'snapper', (0,12,1), root=self.mountpoint
567+
):
568+
date_info = datetime.datetime.now()
569+
snapshot = ElementTree.Element('snapshot')
563570

564-
snapshot_type = ElementTree.SubElement(snapshot, 'type')
565-
snapshot_type.text = 'single'
571+
snapshot_type = ElementTree.SubElement(snapshot, 'type')
572+
snapshot_type.text = 'single'
566573

567-
snapshot_number = ElementTree.SubElement(snapshot, 'num')
568-
snapshot_number.text = '1'
574+
snapshot_number = ElementTree.SubElement(snapshot, 'num')
575+
snapshot_number.text = '1'
569576

570-
snapshot_description = ElementTree.SubElement(snapshot, 'description')
571-
snapshot_description.text = 'first root filesystem'
577+
snapshot_description = ElementTree.SubElement(snapshot, 'description')
578+
snapshot_description.text = 'first root filesystem'
572579

573-
snapshot_date = ElementTree.SubElement(snapshot, 'date')
574-
snapshot_date.text = date_info.strftime("%Y-%m-%d %H:%M:%S")
580+
snapshot_date = ElementTree.SubElement(snapshot, 'date')
581+
snapshot_date.text = date_info.strftime("%Y-%m-%d %H:%M:%S")
575582

576-
with open(os.path.join(path, 'info.xml'), 'w') as snapshot_info_file:
577-
snapshot_info_file.write(self._xml_pretty(snapshot))
583+
with open(os.path.join(path, 'info.xml'), 'w') as snapshot_info_file:
584+
snapshot_info_file.write(self._xml_pretty(snapshot))
578585

579586
def __exit__(self, exc_type, exc_value, traceback):
580587
if self.toplevel_mount:

test/unit/volume_manager/btrfs_test.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,25 @@ def test_setup_with_snapshot(
132132
self, mock_Temporary, mock_mount, mock_mapped_device, mock_fs,
133133
mock_command, mock_os_exists, mock_os_chmod
134134
):
135+
def return_snapper_version(cmd, *args):
136+
mock = Mock()
137+
snapperCmd = ['chroot', 'snapper', '--version']
138+
subCmd = [element for element in cmd if element in snapperCmd]
139+
if snapperCmd == subCmd:
140+
mock = Mock()
141+
mock.output = 'snapper 0.12.0'
142+
else:
143+
mock.output = \
144+
'ID 258 gen 26 top level 257 path @/.snapshots/1/snapshot'
145+
return mock
146+
147+
mock_command.side_effect = return_snapper_version
148+
135149
mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir'
136150
toplevel_mount = Mock()
137151
mock_mount.return_value = toplevel_mount
138-
command_call = Mock()
139-
command_call.output = \
140-
'ID 258 gen 26 top level 257 path @/.snapshots/1/snapshot'
141152
mock_mapped_device.return_value = 'mapped_device'
142153
mock_os_exists.return_value = False
143-
mock_command.return_value = command_call
144154
self.volume_manager.custom_args['root_is_snapper_snapshot'] = True
145155
self.volume_manager.custom_args['quota_groups'] = True
146156

@@ -161,6 +171,7 @@ def test_setup_with_snapshot(
161171
assert mock_command.call_args_list == [
162172
call(['btrfs', 'quota', 'enable', 'tmpdir']),
163173
call(['btrfs', 'subvolume', 'create', 'tmpdir/@']),
174+
call(['chroot', 'root_dir', 'snapper', '--version']),
164175
call(['btrfs', 'subvolume', 'create', 'tmpdir/@/.snapshots']),
165176
call(['btrfs', 'subvolume', 'create', 'tmpdir/@/.snapshots/1/snapshot']),
166177
call(['btrfs', 'subvolume', 'list', 'tmpdir']),
@@ -436,6 +447,16 @@ def exists(name):
436447
return False
437448
return True
438449

450+
def return_snapper_version(cmd, *args):
451+
snapperCmd = ['chroot', 'snapper', '--version']
452+
subCmd = [element for element in cmd if element in snapperCmd]
453+
if snapperCmd == subCmd:
454+
mock = Mock()
455+
mock.output = 'snapper 0.12.0'
456+
return mock
457+
458+
mock_command.side_effect = return_snapper_version
459+
439460
self.volume_manager.custom_args['quota_groups'] = True
440461
mock_exists.side_effect = exists
441462

@@ -479,6 +500,7 @@ def exists(name):
479500
call(minidom.parseString(xml_info).toprettyxml(indent=" "))
480501
]
481502
assert mock_command.call_args_list == [
503+
call(['chroot', 'tmpdir', 'snapper', '--version']),
482504
call(['btrfs', 'qgroup', 'create', '1/0', 'tmpdir']),
483505
call([
484506
'chroot', 'tmpdir/@/.snapshots/1/snapshot',
@@ -502,6 +524,16 @@ def getitem(key):
502524
def contains(key):
503525
return key in item
504526

527+
def return_snapper_version(cmd, *args):
528+
snapperCmd = ['chroot', 'snapper', '--version']
529+
subCmd = [element for element in cmd if element in snapperCmd]
530+
if snapperCmd == subCmd:
531+
mock = Mock()
532+
mock.output = 'snapper 0.12.0'
533+
return mock
534+
535+
mock_command.side_effect = return_snapper_version
536+
505537
sysconf = Mock()
506538
sysconf.__contains__ = Mock(side_effect=contains)
507539
sysconf.__getitem__ = Mock(side_effect=getitem)

0 commit comments

Comments
 (0)