8
import VMBuilder.plugins
9
from VMBuilder.util import run_cmd
10
from VMBuilder.disk import Disk
11
from VMBuilder.hypervisor import Hypervisor
12
from VMBuilder.distro import Distro
13
from opts import create_optparser
14
from VMBuilder.plugins import load_plugins
15
from exception import VMBuilderException
17
def register_hypervisor(cls):
18
hypervisors[cls.arg] = cls
20
def register_distro(cls):
21
distros[cls.arg] = cls
23
# This is waaay to simple. All it does is to apply the sizes of root, swap and opt
24
# and put them all on a single disk (in separate partitions, of course)
26
size = options.rootsize + options.swapsize + options.optsize
27
disk = Disk(dir=workdir, size='%dM' % size)
29
disk.add_part(offset, options.rootsize, 'ext3', '/')
30
offset += options.rootsize+1
31
disk.add_part(offset, options.swapsize, 'swap', 'swap')
32
offset += options.swapsize+1
33
if options.optsize > 0:
34
disk.add_part(offset, options.optsize, 'ext3', '/opt')
39
optparser.error("This script must be run as root (e.g. via sudo)")
44
optparser.disable_interspersed_args()
45
(options, args) = optparser.parse_args()
47
optparser.error("You need to specify at least the hypervisor type and the distro")
49
if not args[0] in hypervisors.keys():
50
optparser.error("Invalid hypervisor. Valid hypervisors: %s" % " ".join(hypervisors.keys()))
52
hypervisor = hypervisors[args[0]]()
53
if getattr(hypervisor, 'extend_optparse', None):
54
optparser = hypervisor.extend_optparse(optparser)
56
if not args[1] in distros.keys():
57
optparser.error("Invalid distro. Valid distros: %s" % " ".join(distros.keys()))
59
distro = distros[args[1]]()
60
if getattr(distro, 'extend_optparse', None):
61
optparser = distro.extend_optparse(optparser)
63
optparser.set_defaults(destdir='%s-%s' % (args[0], args[1]))
64
optparser.enable_interspersed_args()
65
(options, args) = optparser.parse_args()
66
return (distro, hypervisor)
73
logging.debug('Loading plugins')
75
(distro, hypervisor) = parse_options()
76
create_directory_structure()
83
install(distro, hypervisor)
90
except VMBuilderException,e:
94
logging.critical("Oh, dear, an exception occurred")
98
logging.info("Cleaning up the mess...")
99
while len(cleanup_cb) > 0:
102
def add_clean_cb(cb):
103
cleanup_cb.insert(0, cb)
105
def add_clean_cmd(*argv, **kwargs):
106
add_clean_cb(lambda : run_cmd(*argv, **kwargs))
108
def create_partitions():
109
for disk in VMBuilder.disks:
112
def get_ordered_partitions():
114
for disk in VMBuilder.disks:
115
parts += disk.partitions
116
parts.sort(lambda x,y: len(x.mntpnt)-len(y.mntpnt))
119
def mount_partitions():
120
logging.info('Mounting target filesystem')
121
parts = get_ordered_partitions()
123
if part.type != part.TYPE_SWAP:
124
logging.debug('Mounting %s', part.mntpnt)
125
part.mntpath = '%s%s' % (rootmnt, part.mntpnt)
126
if not os.path.exists(part.mntpath):
127
os.makedirs(part.mntpath)
128
run_cmd('mount', part.mapdev, part.mntpath)
129
add_clean_cmd('umount', part.mntpath, ignore_fail=True)
131
def umount_partitions():
132
logging.info('Unmounting target filesystem')
133
parts = get_ordered_partitions()
136
if part.type != part.TYPE_SWAP:
137
logging.debug('Unounting %s', part.mntpath)
138
run_cmd('umount', part.mntpath)
142
def install(distro, hypervisor):
148
logging.info("Installing guest operating system. This might take some time...")
149
distro.install(destdir=destdir)
151
logging.info("Copying to disk images")
152
run_cmd('rsync', '-aHA', '%s/' % tmproot, rootmnt)
154
logging.info("Installing bootloader")
155
distro.install_bootloader()
158
for disk in VMBuilder.disks:
159
disk.convert('/home/soren/disk1.qcow2', 'qcow2')
162
return disks[0].partitions[0]
165
optparser = create_optparser()