Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/firecracker
Path: blob/main/tests/host_tools/drive.py
1956 views
1
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
# SPDX-License-Identifier: Apache-2.0
3
"""Utilities for creating filesystems on the host."""
4
5
import os
6
7
import framework.utils as utils
8
9
10
class FilesystemFile:
11
"""Facility for creating and working with filesystems."""
12
13
KNOWN_FILEFS_FORMATS = {'ext4'}
14
path = None
15
16
def __init__(self, path: str, size: int = 256, fs_format: str = 'ext4'):
17
"""Create a new file system in a file.
18
19
Raises if the file system format is not supported, if the file already
20
exists, or if it ends in '/'.
21
"""
22
if fs_format not in self.KNOWN_FILEFS_FORMATS:
23
raise ValueError(
24
'Format not in: + ' + str(self.KNOWN_FILEFS_FORMATS)
25
)
26
# Here we append the format as a
27
path = os.path.join(path + '.' + fs_format)
28
29
if os.path.isfile(path):
30
raise FileExistsError("File already exists: " + path)
31
32
utils.run_cmd(
33
'dd status=none if=/dev/zero'
34
' of=' + path +
35
' bs=1M count=' + str(size))
36
utils.run_cmd('mkfs.ext4 -qF ' + path)
37
self.path = path
38
39
def resize(self, new_size):
40
"""Resize the filesystem."""
41
utils.run_cmd('truncate --size ' + str(new_size) + 'M ' + self.path)
42
utils.run_cmd('resize2fs ' + self.path)
43
44
def size(self):
45
"""Return the size of the filesystem."""
46
return os.stat(self.path).st_size
47
48
def __del__(self):
49
"""Destructor cleaning up filesystem from where it was created."""
50
if self.path:
51
try:
52
os.remove(self.path)
53
except OSError:
54
pass
55
56