-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: sparse loop dev provisioning with systemd
- Loading branch information
Showing
11 changed files
with
205 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -651,7 +651,8 @@ def _get_parts(self) -> Iterator[str]: | |
Path('salt/metalk8s/volumes/init.sls'), | ||
Path('salt/metalk8s/volumes/prepared/init.sls'), | ||
Path('salt/metalk8s/volumes/prepared/installed.sls'), | ||
Path('salt/metalk8s/volumes/provisioned/init.sls'), | ||
Path('salt/metalk8s/volumes/prepared/files/[email protected]'), | ||
Path('salt/metalk8s/volumes/prepared/files/metalk8s-volume-clean-up'), | ||
Path('salt/metalk8s/volumes/unprepared/init.sls'), | ||
|
||
Path('salt/_auth/kubernetes_rbac.py'), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
salt/metalk8s/volumes/prepared/files/metalk8s-sparse-volume-cleanup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
#!/usr/bin/env python2 | ||
|
||
from __future__ import print_function | ||
import argparse | ||
import errno | ||
import os | ||
import stat | ||
import sys | ||
|
||
|
||
SPARSE_FILES_DIR = '/var/lib/metalk8s/storage/sparse/' | ||
LOOP_MAJOR = 7 | ||
|
||
|
||
def parse_args(args=None): | ||
parser = argparse.ArgumentParser( | ||
prog='metalk8s-sparse-volume-cleanup', | ||
help='Cleanup MetalK8s sparse loop volumes', | ||
) | ||
|
||
parser.add_argument( | ||
'-i', '--uid', | ||
required=True, | ||
help='UID of the MetalK8s Volume object', | ||
dest='volume_id', | ||
) | ||
|
||
return parser.parse_args(args=args) | ||
|
||
|
||
def check_sparse_file(volume_id): | ||
"""Check if the sparse file exists for this volume ID.""" | ||
sparse_file_path = SPARSE_FILES_DIR + volume_id | ||
if not os.path.isfile(sparse_file_path): | ||
die( | ||
"Sparse file {} does not exist for volume '{}'".format( | ||
sparse_file_path, volume_id | ||
), | ||
exit_code=errno.ENOENT | ||
) | ||
|
||
|
||
def cleanup(volume_id): | ||
device_path = find_device(volume_id) | ||
|
||
print("Detaching loop device '{}' for volume '{}'".format( | ||
device_path, volume_id | ||
)) | ||
|
||
fd = os.open(device_path, os.O_RDONLY) | ||
try: | ||
fcntl.ioctl(fd, LOOP_CLR_FD, 0) | ||
except IOError as exn: | ||
if exn.errno != errno.ENXIO: | ||
die("Unexpected error when trying to free device {}: {}".format( | ||
device_path, str(exn) | ||
)) | ||
print("Device already freed", file=sys.stderr) | ||
finally: | ||
fd.close() | ||
|
||
print("Loop device for volume '{}' was successfully detached".format( | ||
volume_id | ||
)) | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
|
||
check_sparse_file(args.volume_id) | ||
cleanup(args.volume_id) | ||
|
||
|
||
# Helpers {{{ | ||
def die(message, exit_code=1): | ||
print(message, file=sys.stderr) | ||
sys.exit(exit_code) | ||
|
||
|
||
def find_device(volume_id): | ||
"""Find the device provisioned for this volume ID. | ||
SparseLoopDevice volumes are either: | ||
- formatted, with the volume ID set in the filesystem annotations, | ||
hence discoverable under /dev/disk/by-uuid/ | ||
- raw, in which case the device is partitioned and the first partition | ||
is labeled with the volume ID, hence discoverable under | ||
/dev/disk/by-partuuid/ | ||
""" | ||
path_by_uuid = "/dev/disk/by-uuid/{}".format(volume_id) | ||
if os.path.exists(path_by_uuid): | ||
device_path = os.path.realpath(path_by_uuid) | ||
else: | ||
path_by_partuuid = "/dev/disk/by-partuuid/{}".format(volume_id) | ||
if not os.path.exists(path_by_partuuid): | ||
die( | ||
"Device for volume '{}' was not found".format(volume_id), | ||
exit_code=errno.ENOENT, | ||
) | ||
|
||
partition_name = os.path.basename(os.path.realpath(path_by_partuuid)) | ||
device_name = os.path.basename( | ||
os.path.realpath("/sys/class/block/{}/..".format(partition_name)) | ||
) | ||
device_path = '/dev/{}'.format(match.group(0)) | ||
|
||
if not is_loop_device(device_path): | ||
die("{} (found for volume '{}') is not a loop device".format( | ||
device_path, volume_id | ||
)) | ||
|
||
return device_path | ||
|
||
|
||
def is_loop_device(path): | ||
device_stat = os.stat(path) | ||
return stat.S_ISBLK(device_stat.st_mode) \ | ||
and (_major(device_stat.st_rdev) == LOOP_MAJOR) | ||
|
||
|
||
def _major(value): | ||
return (value >> 8) & 0xff | ||
|
||
|
||
# }}} | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[Unit] | ||
Description=Setup MetalK8s sparse loop volume %I | ||
RequiresMountsFor=/var/lib/metalk8s/storage/sparse | ||
AssertFileNotEmpty=/var/lib/metalk8s/storage/sparse/%i | ||
|
||
[Service] | ||
Type=oneshot | ||
ExecStart=/bin/flock --exclusive --wait 10 /var/lock/metalk8s-sparse-volume.lock \ | ||
-c "/sbin/losetup --find --partscan /var/lib/metalk8s/storage/sparse/%i" | ||
ExecStop=/usr/local/bin/libexec/metalk8s-sparse-volume-cleanup --uid "%i" | ||
RemainAfterExit=yes | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
Oops, something went wrong.