2013-07-10 10:49:45 +02:00
|
|
|
from base import Task
|
|
|
|
from common import phases
|
|
|
|
from common.exceptions import TaskError
|
|
|
|
from common.tools import log_check_call
|
|
|
|
from bootstrap import Bootstrap
|
2013-07-10 14:42:03 +02:00
|
|
|
import os
|
2013-07-10 10:49:45 +02:00
|
|
|
|
|
|
|
class FormatVolume(Task):
|
|
|
|
description = 'Formatting the volume'
|
|
|
|
phase = phases.volume_preparation
|
|
|
|
|
|
|
|
def run(self, info):
|
2013-07-10 14:42:03 +02:00
|
|
|
mkmount = ['/usr/bin/qemu-img', 'create', '-f', 'raw', info.manifest.bootstrapper['image_file'], str(info.manifest.volume['size'])+'M']
|
|
|
|
log_check_call(mkmount)
|
|
|
|
ddcmd = ['/bin/dd', 'if=/dev/zero', 'bs=1024', 'conv=notrunc', 'count='+str(info.manifest.volume['size']), 'of='+info.manifest.bootstrapper['image_file']]
|
|
|
|
log_check_call(ddcmd)
|
|
|
|
loopcmd = ['/sbin/losetup', '/dev/loop0', info.manifest.bootstrapper['image_file']]
|
|
|
|
log_check_call(loopcmd)
|
|
|
|
mkfs = [ '/sbin/mkfs.{fs}'.format(fs=info.manifest.volume['filesystem']), '-m', '1', '-v', '/dev/loop0']
|
|
|
|
log_check_call(mkfs)
|
|
|
|
|
2013-07-10 10:49:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TuneVolumeFS(Task):
|
|
|
|
description = 'Tuning the bootstrap volume filesystem'
|
|
|
|
phase = phases.volume_preparation
|
|
|
|
after = [FormatVolume]
|
|
|
|
|
|
|
|
def run(self, info):
|
2013-07-10 14:42:03 +02:00
|
|
|
#dev_path = info.bootstrap_device['path']
|
|
|
|
dev_path = info.manifest.bootstrapper['image_file']
|
2013-07-10 10:49:45 +02:00
|
|
|
# Disable the time based filesystem check
|
|
|
|
log_check_call(['/sbin/tune2fs', '-i', '0', dev_path])
|
|
|
|
|
|
|
|
|
|
|
|
class AddXFSProgs(Task):
|
|
|
|
description = 'Adding `xfsprogs\' to the image packages'
|
|
|
|
phase = phases.preparation
|
|
|
|
|
|
|
|
def run(self, info):
|
|
|
|
include, exclude = info.img_packages
|
|
|
|
include.add('xfsprogs')
|
|
|
|
|
|
|
|
|
|
|
|
class CreateMountDir(Task):
|
|
|
|
description = 'Creating mountpoint for the bootstrap volume'
|
|
|
|
phase = phases.volume_mounting
|
|
|
|
|
|
|
|
def run(self, info):
|
|
|
|
import os
|
|
|
|
mount_dir = info.manifest.bootstrapper['mount_dir']
|
2013-07-10 14:42:03 +02:00
|
|
|
info.root = mount_dir
|
2013-07-10 10:49:45 +02:00
|
|
|
# Works recursively, fails if last part exists, which is exaclty what we want.
|
2013-07-10 14:42:03 +02:00
|
|
|
os.makedirs(mount_dir)
|
2013-07-10 10:49:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MountVolume(Task):
|
|
|
|
description = 'Mounting the bootstrap volume'
|
|
|
|
phase = phases.volume_mounting
|
|
|
|
after = [CreateMountDir]
|
|
|
|
|
|
|
|
def run(self, info):
|
|
|
|
with open('/proc/mounts') as mounts:
|
|
|
|
for mount in mounts:
|
|
|
|
if info.root in mount:
|
|
|
|
msg = 'Something is already mounted at {root}'.format(root=info.root)
|
|
|
|
raise TaskError(msg)
|
|
|
|
|
2013-07-10 14:42:03 +02:00
|
|
|
log_check_call(['/bin/mount', '-t', info.manifest.volume['filesystem'], '/dev/loop0', info.root])
|
2013-07-10 10:49:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MountSpecials(Task):
|
|
|
|
description = 'Mounting special block devices'
|
|
|
|
phase = phases.os_installation
|
|
|
|
after = [Bootstrap]
|
|
|
|
|
|
|
|
def run(self, info):
|
|
|
|
log_check_call(['/bin/mount', '--bind', '/dev', '{root}/dev'.format(root=info.root)])
|
|
|
|
log_check_call(['/usr/sbin/chroot', info.root, '/bin/mount', '-t', 'proc', 'none', '/proc'])
|
|
|
|
log_check_call(['/usr/sbin/chroot', info.root, '/bin/mount', '-t', 'sysfs', 'none', '/sys'])
|
|
|
|
log_check_call(['/usr/sbin/chroot', info.root, '/bin/mount', '-t', 'devpts', 'none', '/dev/pts'])
|
|
|
|
|
|
|
|
|
|
|
|
class UnmountSpecials(Task):
|
|
|
|
description = 'Unmunting special block devices'
|
|
|
|
phase = phases.volume_unmounting
|
|
|
|
|
|
|
|
def run(self, info):
|
|
|
|
log_check_call(['/usr/sbin/chroot', info.root, '/bin/umount', '/dev/pts'])
|
|
|
|
log_check_call(['/usr/sbin/chroot', info.root, '/bin/umount', '/sys'])
|
|
|
|
log_check_call(['/usr/sbin/chroot', info.root, '/bin/umount', '/proc'])
|
|
|
|
log_check_call(['/bin/umount', '{root}/dev'.format(root=info.root)])
|
|
|
|
|
|
|
|
|
|
|
|
class UnmountVolume(Task):
|
|
|
|
description = 'Unmounting the bootstrap volume'
|
|
|
|
phase = phases.volume_unmounting
|
|
|
|
after = [UnmountSpecials]
|
|
|
|
|
|
|
|
def run(self, info):
|
|
|
|
log_check_call(['/bin/umount', info.root])
|
2013-07-10 14:42:03 +02:00
|
|
|
log_check_call(['/sbin/losetup', '-d', '/dev/loop0'])
|
|
|
|
|
2013-07-10 10:49:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteMountDir(Task):
|
|
|
|
description = 'Deleting mountpoint for the bootstrap volume'
|
|
|
|
phase = phases.volume_unmounting
|
|
|
|
after = [UnmountVolume]
|
|
|
|
|
|
|
|
def run(self, info):
|
|
|
|
import os
|
|
|
|
os.rmdir(info.root)
|
|
|
|
del info.root
|
|
|
|
|
|
|
|
|
|
|
|
class ModifyFstab(Task):
|
|
|
|
description = 'Adding root volume to the fstab'
|
|
|
|
phase = phases.system_modification
|
|
|
|
|
|
|
|
def run(self, info):
|
|
|
|
import os.path
|
|
|
|
mount_opts = ['defaults']
|
|
|
|
if info.manifest.volume['filesystem'].lower() in ['ext2', 'ext3', 'ext4']:
|
|
|
|
mount_opts.append('barrier=0')
|
|
|
|
if info.manifest.volume['filesystem'].lower() == 'xfs':
|
|
|
|
mount_opts.append('nobarrier')
|
|
|
|
fstab_path = os.path.join(info.root, 'etc/fstab')
|
|
|
|
with open(fstab_path, 'a') as fstab:
|
|
|
|
fstab.write(('/dev/xvda1 / {filesystem} {mount_opts} 1 1\n'
|
|
|
|
.format(filesystem=info.manifest.volume['filesystem'].lower(),
|
|
|
|
mount_opts=','.join(mount_opts))))
|