Skip to content

Commit 468ee1b

Browse files
committed
image-customize: Add --fresh option
By default, multiple image-customize calls are additive. This is useful, and API by now. But our various `make vm` rules explicitly remove the overlays, and hardcoding/duplicating overlay names in projects is ugly. Let's give this a proper option.
1 parent 39b9807 commit 468ee1b

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

image-customize

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,28 @@ opt_quick = False
2929
opt_verbose = False
3030

3131

32-
def prepare_install_image(base_image, install_image, resize):
32+
def prepare_install_image(base_image, install_image, resize, fresh):
3333
'''Create the necessary layered image for the build/install'''
3434

3535
if "/" not in base_image:
3636
base_image = os.path.join(testvm.IMAGES_DIR, base_image)
3737
if "/" not in install_image:
3838
install_image = os.path.join(os.path.join(TEST_DIR, "images"), os.path.basename(install_image))
3939

40-
# In vm-customize we don't force recreate images
40+
qcow2_image = f"{install_image}.qcow2"
41+
42+
# Remove existing overlay if --fresh was requested
43+
if fresh:
44+
for f in [install_image, qcow2_image]:
45+
try:
46+
os.unlink(f)
47+
except FileNotFoundError:
48+
pass
49+
4150
if not os.path.exists(install_image):
4251
install_image_dir = os.path.dirname(install_image)
4352
os.makedirs(install_image_dir, exist_ok=True)
4453
base_image = os.path.realpath(base_image)
45-
qcow2_image = "{0}.qcow2".format(install_image)
4654
subprocess.check_call(["qemu-img", "create", "-q", "-f", "qcow2",
4755
"-o", "backing_file={0},backing_fmt=qcow2".format(base_image), qcow2_image])
4856
if os.path.lexists(install_image):
@@ -232,6 +240,8 @@ def main():
232240
# options
233241
parser.add_argument('--base-image',
234242
help='Base image name, if "image" does not match a standard Cockpit VM image name')
243+
parser.add_argument('--fresh', action='store_true',
244+
help="Start fresh from the base image; by default, multiple image-customize calls are additive")
235245
parser.add_argument('--resize', help="Resize the image. Size in bytes with using K, M, or G suffix.")
236246
parser.add_argument('-n', '--no-network', action='store_true', help='Do not connect the machine to the Internet')
237247
parser.add_argument('-v', '--verbose', action='store_true',
@@ -259,7 +269,7 @@ def main():
259269
machine = testvm.VirtMachine(maintain=True,
260270
verbose=args.verbose,
261271
networking=network.host(restrict=args.no_network),
262-
image=prepare_install_image(args.base_image, args.image, args.resize))
272+
image=prepare_install_image(args.base_image, args.image, args.resize, args.fresh))
263273
machine.start()
264274
machine.wait_boot()
265275
try:

0 commit comments

Comments
 (0)