Skip to content
This repository has been archived by the owner on Jun 10, 2019. It is now read-only.

Add LVM as a disk backend #381

Merged
merged 1 commit into from
Jun 8, 2017
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
4 changes: 3 additions & 1 deletion bootstrapvz/base/fs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ def load_volume(data, bootloader):
from bootstrapvz.common.fs.virtualharddisk import VirtualHardDisk
from bootstrapvz.common.fs.virtualmachinedisk import VirtualMachineDisk
from bootstrapvz.common.fs.folder import Folder
from bootstrapvz.common.fs.logicalvolume import LogicalVolume
volume_backing = {'raw': LoopbackVolume,
's3': LoopbackVolume,
'vdi': VirtualDiskImage,
'vhd': VirtualHardDisk,
'vmdk': VirtualMachineDisk,
'ebs': EBSVolume,
'folder': Folder
'folder': Folder,
'lvm': LogicalVolume
}.get(data['backing'])

# Instantiate the partition map
Expand Down
21 changes: 20 additions & 1 deletion bootstrapvz/base/manifest-schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ properties:
^\w+$: {}
additionalProperties: false
volume:
type: object
oneOf:
- $ref: '#/definitions/standardvolume'
- $ref: '#/definitions/logicalvolume'
definitions:
standardvolume:
type: object
properties:
backing: {type: string}
Expand All @@ -141,7 +147,20 @@ properties:
- $ref: '#/definitions/partition_table'
required: [partitions]
additionalProperties: false
definitions:
logicalvolume:
type: object
properties:
backing:
enum: [lvm]
volumegroup : {type: string}
logicalvolume: {type: string}
partitions:
type: object
oneOf:
- $ref: '#/definitions/no_partitions'
- $ref: '#/definitions/partition_table'
required: [partitions]
additionalProperties: false
absolute_path:
type: string
pattern: ^/[^\0]+$
Expand Down
38 changes: 38 additions & 0 deletions bootstrapvz/common/fs/logicalvolume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from bootstrapvz.base.fs.volume import Volume
from bootstrapvz.common.tools import log_check_call
import os


class LogicalVolume(Volume):

def __init__(self, partitionmap):
super(LogicalVolume, self).__init__(partitionmap)
self.vg = ''
self.lv = ''

def create(self, volumegroup, logicalvolume):
self.vg = volumegroup
self.lv = logicalvolume
image_path = os.path.join(os.sep, 'dev', self.vg, self.lv)
self.fsm.create(image_path=image_path)

def _before_create(self, e):
self.image_path = e.image_path
lv_size = str(self.size.bytes.get_qty_in('MiB'))
log_check_call(['lvcreate', '--size', '{mib}M'.format(mib=lv_size),
'--name', self.lv, self.vg])

def _before_attach(self, e):
log_check_call(['lvchange', '--activate', 'y', self.image_path])
[self.loop_device_path] = log_check_call(['losetup', '--show', '--find', '--partscan', self.image_path])
self.device_path = self.loop_device_path

def _before_detach(self, e):
log_check_call(['losetup', '--detach', self.loop_device_path])
log_check_call(['lvchange', '--activate', 'n', self.image_path])
del self.loop_device_path
self.device_path = None

def delete(self):
log_check_call(['lvremove', '-f', self.image_path])
del self.image_path
37 changes: 37 additions & 0 deletions bootstrapvz/common/tasks/logicalvolume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import bootstrapvz.common.tasks.host as host
import bootstrapvz.common.tasks.volume as volume
from bootstrapvz.base import Task
from bootstrapvz.common import phases


class AddRequiredCommands(Task):
description = 'Adding commands required for creating and mounting logical volumes'
phase = phases.validation
successors = [host.CheckExternalCommands]

@classmethod
def run(cls, info):
from bootstrapvz.common.fs.logicalvolume import LogicalVolume
if type(info.volume) is LogicalVolume:
info.host_dependencies['lvcreate'] = 'lvm2'
info.host_dependencies['losetup'] = 'mount'


class Create(Task):
description = 'Creating a Logical volume'
phase = phases.volume_creation
successors = [volume.Attach]

@classmethod
def run(cls, info):
info.volume.create(volumegroup=info.manifest.volume['volumegroup'],
logicalvolume=info.manifest.volume['logicalvolume'])


class Delete(Task):
description = 'Deleting a Logical volume'
phase = phases.cleaning

@classmethod
def run(cls, info):
info.volume.delete()
8 changes: 8 additions & 0 deletions bootstrapvz/providers/kvm/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ virtual images for Linux Kernel-based Virtual Machines. It supports the
installation of `virtio kernel
modules <http://www.linux-kvm.org/page/Virtio>`__ (paravirtualized
drivers for IO operations).
It also supports creating an image with LVM as a disk backend.

Manifest settings
-----------------
Expand All @@ -15,6 +16,9 @@ Provider

- ``virtio``: Specifies which virtio kernel modules to install.
``optional``
- ``logicalvolume``: Specifies the logical volume where the disk image will be built.
``volumegroup``: Specifies the volume group where the logical volume will be stored.
These options should only be used if ``lvm`` was given as a disk backend.

Example:

Expand All @@ -26,3 +30,7 @@ Example:
virtio:
- virtio_blk
- virtio_net
volume:
backing: lvm
logicalvolume: lvtest
volumegroup: vgtest
15 changes: 11 additions & 4 deletions bootstrapvz/providers/kvm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from bootstrapvz.common import task_groups
import tasks.packages
from bootstrapvz.common.tasks import image, loopback, initd, ssh
from bootstrapvz.common.tasks import image, loopback, initd, ssh, logicalvolume


def validate_manifest(data, validator, error):
Expand All @@ -12,14 +12,20 @@ def resolve_tasks(taskset, manifest):
taskset.update(task_groups.get_standard_groups(manifest))

taskset.update([tasks.packages.DefaultPackages,
loopback.AddRequiredCommands,
loopback.Create,
initd.InstallInitScripts,
ssh.AddOpenSSHPackage,
ssh.ShredHostkeys,
ssh.AddSSHKeyGeneration,
image.MoveImage,
])
if manifest.volume.get('logicalvolume', []):
taskset.update([logicalvolume.AddRequiredCommands,
Copy link
Owner

Choose a reason for hiding this comment

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

Instead of only supporting this on kvm, you could modify volume_group in task_groups.py to be a function and add the commands there. The same would actually work for the loopback tasks as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which providers do you have in mind ?

Copy link
Owner

Choose a reason for hiding this comment

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

Come to think of it, there might not be any. This is pretty kvm specific, since the volume is kind of tied to the host machine, correct?
I'm ok with merging this if you squash your commits :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The commits have been squashed.

logicalvolume.Create,
])
else:
taskset.update([loopback.AddRequiredCommands,
loopback.Create,
image.MoveImage,
])

if manifest.provider.get('virtio', []):
from tasks import virtio
Expand All @@ -28,3 +34,4 @@ def resolve_tasks(taskset, manifest):

def resolve_rollback_tasks(taskset, manifest, completed, counter_task):
taskset.update(task_groups.get_standard_rollback_tasks(completed))
counter_task(taskset, logicalvolume.Create, logicalvolume.Delete)
6 changes: 5 additions & 1 deletion bootstrapvz/providers/kvm/manifest-schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ properties:
properties:
backing:
type: string
enum: [raw]
enum:
- raw
- lvm
logicalvolume: {type: string}
volumegroup: {type: string}
partitions:
type: object
properties:
Expand Down
26 changes: 26 additions & 0 deletions manifests/examples/kvm/jessie-lvm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
name: debian-lvm-example
provider:
name: kvm
bootstrapper:
workspace: /target
system:
release: jessie
architecture: amd64
bootloader: grub
charmap: UTF-8
locale: en_US
timezone: UTC
volume:
backing: lvm
logicalvolume: lvtest
volumegroup: vgtest
partitions:
type: gpt
root:
filesystem: ext4
size: 1GB
packages: {}
plugins:
root_password:
password: test