Skip to content

Commit

Permalink
adding support for creating a snapshot during vm build
Browse files Browse the repository at this point in the history
  • Loading branch information
chilcote committed Oct 27, 2017
1 parent b1d8eac commit 48e3110
Showing 1 changed file with 44 additions and 17 deletions.
61 changes: 44 additions & 17 deletions pkgroot/usr/local/vfuse/vfuse
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def create_vmdk(output_dir, output_name, disk_id, disk_type,
except err:
print colored('Error: %s' % err.strip(), 'red')

return vmpath
return vmpath, fusion_tools_path


def set_perms(path):
Expand Down Expand Up @@ -536,15 +536,13 @@ def main():
parser.add_argument('--recovery',
help='Boot into Recovery HD',
action='store_true')
parser.add_argument('--version',
help='Print version of vfuse and exit',
parser.add_argument('--snapshot',
help='Create initial snapshot',
action='store_true')
parser.add_argument('--snapshot-name',
help='Custom name for the initial snapshot')
args = parser.parse_args()

if args.version:
print __version__
sys.exit(0)

if (os.getuid() != 0
and not args.stop
and not args.reset
Expand Down Expand Up @@ -579,6 +577,8 @@ def main():
netboot_hdsize = '40'
netboot_osvers = get_osvers('/')
recovery = False
snapshot = False
snapshot_name = 'baseline'

monitor_vm = False
job_id = None
Expand Down Expand Up @@ -680,6 +680,12 @@ def main():
if args.recovery:
recovery = True

if args.snapshot:
snapshot = True

if args.snapshot_name:
snapshot_name = args.snapshot_name

if args.template:
# Use template-defined keys, otherwise use defaults defined above.
d = import_template(args.template)
Expand Down Expand Up @@ -742,6 +748,10 @@ def main():
qemu_path = d['qemu_path']
if d.get('recovery'):
recovery = d['recovery']
if d.get('snapshot'):
snapshot = d['snapshot']
if d.get('snapshot_name'):
snapshot_name = d['snapshot_name']

mount_point, disk_id, volume_kind = mount_dmg(source_dmg)

Expand Down Expand Up @@ -781,16 +791,16 @@ def main():
if volume_kind == 'apfs':
unmount_dmg(mount_point)

vmpath = create_vmdk(
output_dir,
output_name,
disk_id,
disk_type,
fusion_path,
use_qemu,
qemu_path,
source_dmg
)
vmpath, fusion_tools_path = create_vmdk(
output_dir,
output_name,
disk_id,
disk_type,
fusion_path,
use_qemu,
qemu_path,
source_dmg
)

vmx = create_vmx(
vmpath,
Expand All @@ -812,6 +822,23 @@ def main():
)
unmount_dmg(disk_id)

if snapshot:
print colored('Creating a "%s" snapshot' % snapshot_name, 'green')
cmd = [os.path.join(fusion_tools_path, 'vmrun'),
'-T',
'fusion',
'snapshot',
vmx,
snapshot_name
]
task = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = task.communicate()
if err:
print colored('Error: %s' % err.strip(), 'yellow')
print colored('There is a problem creating a snapshot',
'yellow')

if os.getuid() == 0:
set_perms(vmpath)

Expand Down

0 comments on commit 48e3110

Please sign in to comment.