summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTiago Gomes <tiago.gomes@codethink.co.uk>2013-05-10 08:20:32 +0000
committerTiago Gomes <tiago.gomes@codethink.co.uk>2013-05-10 08:20:32 +0000
commit6cbc4e0a701ac6f2a14b48a0dd13ecc716822734 (patch)
tree8e12d386f01bb0b22b5a5ec939acac594dabed48
parentf6b904bbbc98cbf430beabc2359af9d6607c369d (diff)
parentcf06b44be82ae862b89fe789d018264b9d99ef3e (diff)
downloadmorph-6cbc4e0a701ac6f2a14b48a0dd13ecc716822734.tar.gz
Merge git://git.baserock.org/baserock/baserock/morph into baserock/tiagogomes/drop-config
-rwxr-xr-xmorphlib/exts/kvm.write19
-rwxr-xr-xmorphlib/exts/virtualbox-ssh.write9
-rwxr-xr-xmorphlib/writeexts.py84
3 files changed, 76 insertions, 36 deletions
diff --git a/morphlib/exts/kvm.write b/morphlib/exts/kvm.write
index 630f6ae7..e2f7435c 100755
--- a/morphlib/exts/kvm.write
+++ b/morphlib/exts/kvm.write
@@ -56,6 +56,7 @@ class KvmPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
temp_root, location = args
ssh_host, vm_name, vm_path = self.parse_location(location)
+ autostart = self.parse_autostart()
fd, raw_disk = tempfile.mkstemp()
os.close(fd)
@@ -63,7 +64,7 @@ class KvmPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
try:
self.transfer(raw_disk, ssh_host, vm_path)
- self.create_libvirt_guest(ssh_host, vm_name, vm_path)
+ self.create_libvirt_guest(ssh_host, vm_name, vm_path, autostart)
except BaseException:
sys.stderr.write('Error deploying to libvirt')
os.remove(raw_disk)
@@ -95,7 +96,7 @@ class KvmPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
with open(raw_disk, 'rb') as f:
cliapp.runcmd(['rsync', '-zS', raw_disk, target])
- def create_libvirt_guest(self, ssh_host, vm_name, vm_path):
+ def create_libvirt_guest(self, ssh_host, vm_name, vm_path, autostart):
'''Create the libvirt virtual machine.'''
self.status(msg='Creating libvirt/kvm virtual machine')
@@ -107,13 +108,13 @@ class KvmPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
ram_mebibytes = str(self.get_ram_size() / (1024**2))
- cliapp.runcmd(
- ['ssh', ssh_host,
- 'virt-install', '--connect qemu:///system', '--import',
- '--name', vm_name, '--vnc', '--noreboot',
- '--ram=%s' % ram_mebibytes,
- '--disk path=%s,bus=ide' % vm_path] +
- attach_opts)
+ cmdline = ['ssh', ssh_host,
+ 'virt-install', '--connect qemu:///system', '--import',
+ '--name', vm_name, '--vnc', '--ram=%s' % ram_mebibytes,
+ '--disk path=%s,bus=ide' % vm_path] + attach_opts
+ if not autostart:
+ cmdline += '--noreboot'
+ cliapp.runcmd(cmdline)
KvmPlusSshWriteExtension().run()
diff --git a/morphlib/exts/virtualbox-ssh.write b/morphlib/exts/virtualbox-ssh.write
index 37f56524..cb17b69b 100755
--- a/morphlib/exts/virtualbox-ssh.write
+++ b/morphlib/exts/virtualbox-ssh.write
@@ -62,6 +62,7 @@ class VirtualBoxPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
temp_root, location = args
ssh_host, vm_name, vdi_path = self.parse_location(location)
+ autostart = self.parse_autostart()
fd, raw_disk = tempfile.mkstemp()
os.close(fd)
@@ -70,7 +71,8 @@ class VirtualBoxPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
try:
self.transfer_and_convert_to_vdi(
raw_disk, ssh_host, vdi_path)
- self.create_virtualbox_guest(ssh_host, vm_name, vdi_path)
+ self.create_virtualbox_guest(ssh_host, vm_name, vdi_path,
+ autostart)
except BaseException:
sys.stderr.write('Error deploying to VirtualBox')
os.remove(raw_disk)
@@ -105,7 +107,7 @@ class VirtualBoxPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
str(os.path.getsize(raw_disk))],
stdin=f)
- def create_virtualbox_guest(self, ssh_host, vm_name, vdi_path):
+ def create_virtualbox_guest(self, ssh_host, vm_name, vdi_path, autostart):
'''Create the VirtualBox virtual machine.'''
self.status(msg='Create VirtualBox virtual machine')
@@ -134,6 +136,9 @@ class VirtualBoxPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
'--medium', disk]
commands.append(cmd)
+ if autostart:
+ commands.append(['startvm', vm_name])
+
for command in commands:
argv = ['ssh', ssh_host, 'VBoxManage'] + command
cliapp.runcmd(argv)
diff --git a/morphlib/writeexts.py b/morphlib/writeexts.py
index 48847d56..c74a4b76 100755
--- a/morphlib/writeexts.py
+++ b/morphlib/writeexts.py
@@ -62,10 +62,15 @@ class WriteExtension(cliapp.Application):
os.remove(raw_disk)
raise
try:
- self.create_factory(mp, temp_root)
- self.create_fstab(mp)
- self.create_factory_run(mp)
+ version_label = 'version1'
+ version_root = os.path.join(mp, 'systems', version_label)
+ os.makedirs(version_root)
+ self.create_state(mp)
+ self.create_orig(version_root, temp_root)
+ self.create_fstab(version_root)
+ self.create_run(version_root)
if self.bootloader_is_wanted():
+ self.install_kernel(version_root, temp_root)
self.install_extlinux(mp)
except BaseException, e:
sys.stderr.write('Error creating disk image')
@@ -113,6 +118,16 @@ class WriteExtension(cliapp.Application):
'''Parse RAM size from environment.'''
return self._parse_size_from_environment('RAM_SIZE', '1G')
+ def create_state(self, real_root):
+ '''Create the state subvolumes that are shared between versions'''
+
+ self.status(msg='Creating state subvolumes')
+ os.mkdir(os.path.join(real_root, 'state'))
+ statedirs = ['home', 'opt', 'srv']
+ for statedir in statedirs:
+ dirpath = os.path.join(real_root, 'state', statedir)
+ cliapp.runcmd(['btrfs', 'subvolume', 'create', dirpath])
+
def create_raw_disk_image(self, filename, size):
'''Create a raw disk image.'''
@@ -152,36 +167,30 @@ class WriteExtension(cliapp.Application):
cliapp.runcmd(['umount', mount_point])
os.rmdir(mount_point)
- def create_factory(self, real_root, temp_root):
+ def create_orig(self, version_root, temp_root):
'''Create the default "factory" system.'''
- factory = os.path.join(real_root, 'factory')
+ orig = os.path.join(version_root, 'orig')
- self.status(msg='Creating factory subvolume')
- cliapp.runcmd(['btrfs', 'subvolume', 'create', factory])
- self.status(msg='Copying files to factory subvolume')
- cliapp.runcmd(['cp', '-a', temp_root + '/.', factory + '/.'])
+ self.status(msg='Creating orig subvolume')
+ cliapp.runcmd(['btrfs', 'subvolume', 'create', orig])
+ self.status(msg='Copying files to orig subvolume')
+ cliapp.runcmd(['cp', '-a', temp_root + '/.', orig + '/.'])
- # The kernel needs to be on the root volume.
- self.status(msg='Copying boot directory to root subvolume')
- factory_boot = os.path.join(factory, 'boot')
- root_boot = os.path.join(real_root, 'boot')
- cliapp.runcmd(['cp', '-a', factory_boot, root_boot])
-
- def create_factory_run(self, real_root):
- '''Create the 'factory-run' snapshot.'''
+ def create_run(self, version_root):
+ '''Create the 'run' snapshot.'''
- self.status(msg='Creating factory-run subvolume')
- factory = os.path.join(real_root, 'factory')
- factory_run = factory + '-run'
+ self.status(msg='Creating run subvolume')
+ orig = os.path.join(version_root, 'orig')
+ run = os.path.join(version_root, 'run')
cliapp.runcmd(
- ['btrfs', 'subvolume', 'snapshot', factory, factory_run])
+ ['btrfs', 'subvolume', 'snapshot', orig, run])
- def create_fstab(self, real_root):
+ def create_fstab(self, version_root):
'''Create an fstab.'''
self.status(msg='Creating fstab')
- fstab = os.path.join(real_root, 'factory', 'etc', 'fstab')
+ fstab = os.path.join(version_root, 'orig', 'etc', 'fstab')
if os.path.exists(fstab):
with open(fstab, 'r') as f:
@@ -201,6 +210,18 @@ class WriteExtension(cliapp.Application):
with open(fstab, 'w') as f:
f.write(contents)
+ def install_kernel(self, version_root, temp_root):
+ '''Install the kernel outside of 'orig' or 'run' subvolumes'''
+
+ self.status(msg='Installing kernel')
+ image_names = ['vmlinuz', 'zImage', 'uImage']
+ kernel_dest = os.path.join(version_root, 'linux')
+ for name in image_names:
+ try_path = os.path.join(temp_root, 'boot', name)
+ if os.path.exists(try_path):
+ cliapp.runcmd(['cp', '-a', try_path, kernel_dest])
+ break
+
def install_extlinux(self, real_root):
'''Install extlinux on the newly created disk image.'''
@@ -210,8 +231,9 @@ class WriteExtension(cliapp.Application):
f.write('default linux\n')
f.write('timeout 1\n')
f.write('label linux\n')
- f.write('kernel /boot/vmlinuz\n')
- f.write('append root=/dev/sda rootflags=subvol=factory-run '
+ f.write('kernel /systems/version1/linux\n')
+ f.write('append root=/dev/sda '
+ 'rootflags=subvol=systems/version1/run '
'init=/sbin/init rw\n')
self.status(msg='Installing extlinux')
@@ -251,3 +273,15 @@ class WriteExtension(cliapp.Application):
value = 'no'
return value == 'yes'
+
+ def parse_autostart(self):
+ '''Parse $AUTOSTART to determine if VMs should be started.'''
+
+ autostart = os.environ.get('AUTOSTART', 'no')
+ if autostart == 'no':
+ return False
+ elif autostart == 'yes':
+ return True
+ else:
+ raise cliapp.AppException('Unexpected value for AUTOSTART: %s' %
+ autostart)