summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <lars.wirzenius@codethink.co.uk>2012-01-11 17:00:33 +0000
committerLars Wirzenius <lars.wirzenius@codethink.co.uk>2012-01-11 17:00:33 +0000
commitaaee88bf4323dc4f29af63dd1b2a99c87c23ffa1 (patch)
tree8d219be1caf25a8a2b021b2d59c8a1474af2c4d5
parenta41042cd9fd7ee21b2f47406764d650dbb9cea8e (diff)
downloadmorph-aaee88bf4323dc4f29af63dd1b2a99c87c23ffa1.tar.gz
stop morph from using fakeroot and sudo
Instead, assume the whole build will be run, by the user, under fakeroot or sudo (the latter for system image builds). This allows us to run all non-system-image-build tests without root access at all. We now always create the cache directory, if missing, even if we're running as root. We no longer run ldconfig if ld.so.conf is missing. It is missing during our tests, but is (now) created by fhs-dirs for real builds.
-rwxr-xr-xcheck3
-rwxr-xr-xmorph4
-rw-r--r--morphlib/bins.py10
-rw-r--r--morphlib/bins_tests.py6
-rw-r--r--morphlib/builder.py61
-rw-r--r--morphlib/execute.py27
-rw-r--r--morphlib/execute_tests.py6
7 files changed, 39 insertions, 78 deletions
diff --git a/check b/check
index 794d035c..25a3d655 100755
--- a/check
+++ b/check
@@ -20,4 +20,5 @@
set -e
python setup.py clean check
-cmdtest -c ./morph tests
+fakeroot env PATH="$PATH:/sbin:/usr/sbin:/usr/local/sbin" \
+ cmdtest -c ./morph tests
diff --git a/morph b/morph
index 742879c5..f557baa9 100755
--- a/morph
+++ b/morph
@@ -79,7 +79,7 @@ class Morph(cliapp.Application):
tempdir = morphlib.tempdir.Tempdir()
builder = morphlib.builder.Builder(tempdir, self)
- if not os.path.exists(self.settings['cachedir']) and os.getuid() != 0:
+ if not os.path.exists(self.settings['cachedir']):
os.mkdir(self.settings['cachedir'])
ret = []
@@ -91,7 +91,7 @@ class Morph(cliapp.Application):
# we may not have permission to tempdir.remove()
ex = morphlib.execute.Execute('.', lambda msg: None)
- ex.runv(["rm", "-rf", tempdir.dirname], as_root=True)
+ ex.runv(["rm", "-rf", tempdir.dirname])
if args:
raise cliapp.AppException('Extra args on command line: %s' % args)
diff --git a/morphlib/bins.py b/morphlib/bins.py
index 90d96d82..cd77a037 100644
--- a/morphlib/bins.py
+++ b/morphlib/bins.py
@@ -101,20 +101,16 @@ def create_stratum(rootdir, stratum_filename, ex):
'''Create a stratum from the contents of a directory.'''
logging.debug('Creating stratum file %s from %s' %
(stratum_filename, rootdir))
- ex.runv(['tar', '-C', rootdir, '-caf', stratum_filename, '.'],
- as_fakeroot=True)
+ ex.runv(['tar', '-C', rootdir, '-caf', stratum_filename, '.'])
-def unpack_binary(filename, dirname, ex, as_fakeroot=False, as_root=False):
+def unpack_binary(filename, dirname, ex):
'''Unpack a binary into a directory.
The directory must exist already.
- If the binary will be packed up again by tar with the same Execute
- object then as_fakeroot will suffice
- If it will be creating a system image as_root will be needed
'''
logging.debug('Unpacking %s into %s' % (filename, dirname))
- ex.runv(['tar', '-C', dirname, '-xvf', filename], as_fakeroot=as_fakeroot, as_root=as_root)
+ ex.runv(['tar', '-C', dirname, '-xvf', filename])
diff --git a/morphlib/bins_tests.py b/morphlib/bins_tests.py
index 6fe48aa7..8a54033b 100644
--- a/morphlib/bins_tests.py
+++ b/morphlib/bins_tests.py
@@ -92,8 +92,7 @@ class ChunkTests(unittest.TestCase):
morphlib.bins.create_chunk(self.instdir, self.chunk_file, ['.'],
self.ex)
os.mkdir(self.unpacked)
- morphlib.bins.unpack_binary(self.chunk_file, self.unpacked, self.ex,
- as_fakeroot=True)
+ morphlib.bins.unpack_binary(self.chunk_file, self.unpacked, self.ex)
self.assertEqual(orig_files, recursive_lstat(self.unpacked))
def test_uses_only_matching_names(self):
@@ -127,8 +126,7 @@ class StratumTests(unittest.TestCase):
self.populate_instdir()
morphlib.bins.create_stratum(self.instdir, self.stratum_file, self.ex)
os.mkdir(self.unpacked)
- morphlib.bins.unpack_binary(self.stratum_file, self.unpacked, self.ex,
- as_fakeroot=True)
+ morphlib.bins.unpack_binary(self.stratum_file, self.unpacked, self.ex)
self.assertEqual(recursive_lstat(self.instdir),
recursive_lstat(self.unpacked))
diff --git a/morphlib/builder.py b/morphlib/builder.py
index c3e3b727..1ac18e0b 100644
--- a/morphlib/builder.py
+++ b/morphlib/builder.py
@@ -43,11 +43,13 @@ def ldconfig(ex, rootdir):
'''
- logging.debug('Running ldconfig for %s' % rootdir)
conf = os.path.join(rootdir, 'etc', 'ld.so.conf')
- cache = os.path.join(rootdir, 'etc', 'ld.so.cache')
- ex.runv(['ldconfig', '-f', conf, '-C', cache, '-r', rootdir])
-
+ if os.path.exists(conf):
+ logging.debug('Running ldconfig for %s' % rootdir)
+ cache = os.path.join(rootdir, 'etc', 'ld.so.cache')
+ ex.runv(['ldconfig', '-f', conf, '-C', cache, '-r', rootdir])
+ else:
+ logging.debug('No %s, not running ldconfig' % conf)
class BinaryBlob(object):
@@ -251,16 +253,14 @@ class Chunk(BinaryBlob):
self.run_sequentially('configure', bs['configure-commands'])
self.run_in_parallel('build', bs['build-commands'])
self.run_sequentially('test', bs['test-commands'])
- self.run_sequentially('install', bs['install-commands'],
- as_fakeroot=True)
+ self.run_sequentially('install', bs['install-commands'])
def build_using_commands(self):
self.msg('Building using explicit commands')
self.run_sequentially('configure', self.morph.configure_commands)
self.run_in_parallel('build', self.morph.build_commands)
self.run_sequentially('test', self.morph.test_commands)
- self.run_sequentially('install', self.morph.install_commands,
- as_fakeroot=True)
+ self.run_sequentially('install', self.morph.install_commands)
def run_in_parallel(self, what, commands):
self.msg('commands: %s' % what)
@@ -268,13 +268,13 @@ class Chunk(BinaryBlob):
self.ex.run(commands)
self.build_watch.stop(what)
- def run_sequentially(self, what, commands, as_fakeroot=False, as_root=False):
+ def run_sequentially(self, what, commands):
self.msg ('commands: %s' % what)
self.build_watch.start(what)
flags = self.ex.env['MAKEFLAGS']
self.ex.env['MAKEFLAGS'] = '-j1'
logging.debug('Setting MAKEFLAGS=%s' % self.ex.env['MAKEFLAGS'])
- self.ex.run(commands, as_fakeroot=as_fakeroot, as_root=as_root)
+ self.ex.run(commands)
self.ex.env['MAKEFLAGS'] = flags
logging.debug('Restore MAKEFLAGS=%s' % self.ex.env['MAKEFLAGS'])
self.build_watch.stop(what)
@@ -321,8 +321,7 @@ class Stratum(BinaryBlob):
self.build_watch.start('unpack-chunks')
for chunk_name, filename in self.built:
self.msg('Unpacking chunk %s' % chunk_name)
- morphlib.bins.unpack_binary(filename, self.destdir, ex,
- as_fakeroot=True)
+ morphlib.bins.unpack_binary(filename, self.destdir, ex)
self.build_watch.stop('unpack-chunks')
self.prepare_binary_metadata(self.morph.name)
self.build_watch.start('create-binary')
@@ -355,22 +354,20 @@ class System(BinaryBlob):
# Partition it.
self.build_watch.start('partition-image')
- self.ex.runv(['parted', '-s', image_name, 'mklabel', 'msdos'],
- as_root=True)
+ self.ex.runv(['parted', '-s', image_name, 'mklabel', 'msdos'])
self.ex.runv(['parted', '-s', image_name, 'mkpart', 'primary',
- '0%', '100%'], as_root=True)
- self.ex.runv(['parted', '-s', image_name, 'set', '1', 'boot', 'on'],
- as_root=True)
+ '0%', '100%'])
+ self.ex.runv(['parted', '-s', image_name, 'set', '1', 'boot', 'on'])
self.build_watch.stop('partition-image')
# Install first stage boot loader into MBR.
self.build_watch.start('install-mbr')
- self.ex.runv(['install-mbr', image_name], as_root=True)
+ self.ex.runv(['install-mbr', image_name])
self.build_watch.stop('install-mbr')
# Setup device mapper to access the partition.
self.build_watch.start('setup-device-mapper')
- out = self.ex.runv(['kpartx', '-av', image_name], as_root=True)
+ out = self.ex.runv(['kpartx', '-av', image_name])
devices = [line.split()[2]
for line in out.splitlines()
if line.startswith('add map ')]
@@ -381,14 +378,14 @@ class System(BinaryBlob):
try:
# Create filesystem.
self.build_watch.start('create-filesystem')
- self.ex.runv(['mkfs', '-t', 'ext3', partition], as_root=True)
+ self.ex.runv(['mkfs', '-t', 'ext3', partition])
self.build_watch.stop('create-filesystem')
# Mount it.
self.build_watch.start('mount-filesystem')
mount_point = self.tempdir.join('mnt')
os.mkdir(mount_point)
- self.ex.runv(['mount', partition, mount_point], as_root=True)
+ self.ex.runv(['mount', partition, mount_point])
self.build_watch.stop('mount-filesystem')
# Unpack all strata into filesystem.
@@ -396,8 +393,7 @@ class System(BinaryBlob):
self.build_watch.start('unpack-strata')
for name, filename in self.built:
self.msg('unpack %s from %s' % (name, filename))
- self.ex.runv(['tar', '-C', mount_point, '-xf', filename],
- as_root=True)
+ self.ex.runv(['tar', '-C', mount_point, '-xf', filename])
ldconfig(ex, mount_point)
self.build_watch.stop('unpack-strata')
@@ -409,7 +405,7 @@ class System(BinaryBlob):
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
/dev/sda1 / ext4 errors=remount-ro 0 1
-''', as_root=True, stdout=open(os.devnull,'w'))
+''', stdout=open(os.devnull,'w'))
self.build_watch.stop('create-fstab')
# Install extlinux bootloader.
@@ -423,9 +419,9 @@ timeout 1
label linux
kernel /vmlinuz
append root=/dev/sda1 init=/sbin/init quiet rw
-''', as_root=True, stdout=open(os.devnull, 'w'))
+''', stdout=open(os.devnull, 'w'))
- self.ex.runv(['extlinux', '--install', mount_point], as_root=True)
+ self.ex.runv(['extlinux', '--install', mount_point])
# Weird hack that makes extlinux work. There is a bug somewhere.
self.ex.runv(['sync'])
@@ -434,26 +430,26 @@ append root=/dev/sda1 init=/sbin/init quiet rw
# Unmount.
self.build_watch.start('unmount-filesystem')
- self.ex.runv(['umount', mount_point], as_root=True)
+ self.ex.runv(['umount', mount_point])
self.build_watch.stop('unmount-filesystem')
except BaseException, e:
# Unmount.
if mount_point is not None:
try:
- self.ex.runv(['umount', mount_point], as_root=True)
+ self.ex.runv(['umount', mount_point])
except Exception:
pass
# Undo device mapping.
try:
- self.ex.runv(['kpartx', '-d', image_name], as_root=True)
+ self.ex.runv(['kpartx', '-d', image_name])
except Exception:
pass
raise
# Undo device mapping.
self.build_watch.start('undo-device-mapper')
- self.ex.runv(['kpartx', '-d', image_name], as_root=True)
+ self.ex.runv(['kpartx', '-d', image_name])
self.build_watch.stop('undo-device-mapper')
# Move image file to cache.
@@ -574,13 +570,12 @@ class Builder(object):
if self.settings['bootstrap']:
self.msg('Unpacking chunk %s onto system' % chunk_name)
ex = morphlib.execute.Execute('/', self.msg)
- morphlib.bins.unpack_binary(chunk_filename, '/', ex, as_root=True)
+ morphlib.bins.unpack_binary(chunk_filename, '/', ex)
ldconfig(ex, '/')
else:
self.msg('Unpacking chunk %s into staging' % chunk_name)
ex = morphlib.execute.Execute(staging_dir, self.msg)
- morphlib.bins.unpack_binary(chunk_filename, staging_dir, ex,
- as_root=True)
+ morphlib.bins.unpack_binary(chunk_filename, staging_dir, ex)
ldconfig(ex, staging_dir)
def get_morph_from_git(self, repo, ref, filename):
diff --git a/morphlib/execute.py b/morphlib/execute.py
index 7b6cf585..038a5cfd 100644
--- a/morphlib/execute.py
+++ b/morphlib/execute.py
@@ -39,31 +39,11 @@ class Execute(object):
self._setup_env()
self.dirname = dirname
self.msg = msg
- self._fakeroot_session = None
-
- def __del__(self): # pragma: no cover
- if self._fakeroot_session:
- os.remove(self._fakeroot_session)
def _setup_env(self):
self.env = dict(os.environ)
- def _prefix(self, argv, as_root, as_fakeroot):
- if as_root: # pragma: no cover
- if os.getuid() == 0:
- prefix = ['env']
- else:
- prefix = ['sudo']
- envs = ["%s=%s" % x for x in self.env.iteritems()]
- argv = prefix + envs + argv
- elif as_fakeroot and os.getuid() != 0:
- if not self._fakeroot_session:
- self._fakeroot_session = tempfile.mkstemp()[1]
- argv = ['fakeroot', '-i', self._fakeroot_session, '-s',
- self._fakeroot_session, '--'] + argv
- return argv
-
- def run(self, commands, as_root=False, as_fakeroot=False, _log=True):
+ def run(self, commands, _log=True):
'''Execute a list of commands.
If a command fails (returns non-zero exit code), the rest are
@@ -75,7 +55,6 @@ class Execute(object):
for command in commands:
self.msg('# %s' % command)
argv = ['sh', '-c', command]
- argv = self._prefix(argv, as_root, as_fakeroot)
logging.debug('run: argv=%s' % repr(argv))
logging.debug('run: env=%s' % repr(self.env))
logging.debug('run: cwd=%s' % repr(self.dirname))
@@ -94,8 +73,7 @@ class Execute(object):
stdouts.append(out)
return stdouts
- def runv(self, argv, feed_stdin=None, as_root=False, as_fakeroot=False,
- _log=True, **kwargs):
+ def runv(self, argv, feed_stdin=None, _log=True, **kwargs):
'''Run a command given as a list of argv elements.
Return standard output. Raise ``CommandFailure`` if the command
@@ -113,7 +91,6 @@ class Execute(object):
if 'env' not in kwargs:
kwargs['env'] = self.env
- argv = self._prefix(argv, as_root, as_fakeroot)
logging.debug('runv: argv=%s' % repr(argv))
logging.debug('runv: env=%s' % repr(self.env))
logging.debug('runv: cwd=%s' % repr(self.dirname))
diff --git a/morphlib/execute_tests.py b/morphlib/execute_tests.py
index 86db9c25..da6f5d49 100644
--- a/morphlib/execute_tests.py
+++ b/morphlib/execute_tests.py
@@ -52,9 +52,3 @@ class ExecuteTests(unittest.TestCase):
def test_runv_sets_working_directory(self):
self.assertEqual(self.e.runv(['pwd']), '/\n')
- def test_runs_as_fakeroot_when_requested(self):
- self.assertEqual(self.e.run(['id -u'], as_fakeroot=True), ['0\n'])
-
- def test_runvs_as_fakeroot_when_requested(self):
- self.assertEqual(self.e.runv(['id', '-u'], as_fakeroot=True), '0\n')
-