Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rouge: Add support for resizing raw images #120

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
author = 'EPAM Systems'

# The full version, including alpha/beta/rc tags
release = 'v0.26'
release = 'v0.27'

# -- General configuration ---------------------------------------------------

Expand Down
10 changes: 8 additions & 2 deletions docs/rouge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,21 @@ partition (which is described below).

type: raw_image # defines raw image block
size: 400 MiB
resize: false
image_path: "some/path/rootfs.ext4"

:code:`image_path` is mandatory. This is a file to be included into
resulting image.

:code:`size` is optional. If it is omitted, `rouge` will use size of
file. If provided :code:`size` is smaller than file size, `rouge` will
stop with an error. Thus, you can create block that is bigger than
file, but not smaller.
stop with an error. If provided :code:`size` is bigger than file size,
`rouge` will try to resize the file to match :code:`size`.

:code:`resize` is optional. If set to :code:`false`, it will prevent
`rouge` from resizing the image to the size of the block. This is
useful when you want to include a file that is smaller than the block
and leave the rest of the block empty.

Android Sparse Image Block
^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
27 changes: 26 additions & 1 deletion moulin/rouge/block_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import shutil
import logging
import itertools
import subprocess
from typing import List, Tuple, NamedTuple, cast
from tempfile import NamedTemporaryFile, TemporaryDirectory

Expand Down Expand Up @@ -129,12 +130,14 @@ def __init__(self, node: YamlValue, **kwargs):
self._node = node
self._fname = self._node["image_path"].as_str
self._size = 0
self._resize = True

def _complete_init(self):
mark = self._node["image_path"].mark
if not os.path.exists(self._fname):
raise YAMLProcessingError(f"Can't find file '{self._fname}'", mark)
fsize = os.path.getsize(self._fname)
self._resize = self._node.get("resize", True).as_bool
size_node = self._node.get("size", None)
if size_node:
self._size = _parse_size(size_node)
Expand All @@ -154,7 +157,29 @@ def size(self) -> int:
def write(self, fp, offset):
if not self._size:
self._complete_init()
ext_utils.dd(self._fname, fp, offset)

fsize = os.path.getsize(self._fname)

if self._resize and fsize < self._size:
# Not using default /tmp to prevent filling ram with huge images
with TemporaryDirectory(dir=".") as tmpd:
shutil.copy(self._fname, tmpd)
with open(os.path.join(tmpd, os.path.basename(self._fname)), "rb+") as data:
data.truncate(self._size)
try:
ext_utils.resize2fs(os.path.join(tmpd, os.path.basename(self._fname)))
except subprocess.CalledProcessError as e:
log.error(
"""Failed to resize %s partition.
Right now we support resizing for EXT{2,3,4} partitions only.
If you don't really want to resize it, please remove 'size' parameter or set 'resize' to false.
If you want to resize some other type of partitions - please create a PR or notify us at least.""",
self._fname)
raise e

ext_utils.dd(os.path.join(tmpd, os.path.basename(self._fname)), fp, offset)
else:
ext_utils.dd(self._fname, fp, offset)

def get_deps(self) -> List[str]:
"Return list of dependencies needed to build this block"
Expand Down
10 changes: 10 additions & 0 deletions moulin/rouge/ext_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,13 @@ def mmd(img: BinaryIO, folders: list):
args.extend(folders)

_run_cmd(args)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this empty line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is required by python styling guides

def resize2fs(img: str, size: Optional[int] = None):
"Resize fs image to the given size"
args = ["resize2fs", img]

if size:
args.append(str(size))

_run_cmd(args)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

SETUP_ARGS: Dict[str, Any] = dict(
name='moulin', # Required
version='0.26', # Required
version='0.27', # Required
description='Meta-build system', # Required
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
Loading