test_fs: Allow running unprivileged
There is no need to mount the filesystem on the host side.
All filesystem tools offer some way to fill the fs without mounting.
So, create the content on the host side, create and fill the fs
without mounting.
No more sudo or guestmount needed.
This new approach works because the tests don't care about user IDs
and no device files are needed.
If user IDs start to matter it's still possible to use wrapper
tools like fakeroot in future while filling the fs.
Signed-off-by: Richard Weinberger <richard@nod.at>
Signed-off-by: Simon Glass <sjg@chromium.org>
Tested-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>
diff --git a/test/py/tests/fs_helper.py b/test/py/tests/fs_helper.py
index 380f4c4..ccfc020 100644
--- a/test/py/tests/fs_helper.py
+++ b/test/py/tests/fs_helper.py
@@ -9,7 +9,7 @@
import os
from subprocess import call, check_call, check_output, CalledProcessError
-def mk_fs(config, fs_type, size, prefix, size_gran = 0x100000):
+def mk_fs(config, fs_type, size, prefix, src_dir=None, size_gran = 0x100000):
"""Create a file system volume
Args:
@@ -17,6 +17,7 @@
fs_type (str): File system type, e.g. 'ext4'
size (int): Size of file system in bytes
prefix (str): Prefix string of volume's file name
+ src_dir (str): Root directory to use, or None for none
size_gran (int): Size granularity of file system image in bytes
Raises:
@@ -39,6 +40,12 @@
else:
fs_lnxtype = fs_type
+ if src_dir:
+ if fs_lnxtype == 'ext4':
+ mkfs_opt = mkfs_opt + ' -d ' + src_dir
+ elif fs_lnxtype != 'vfat':
+ raise ValueError(f'src_dir not implemented for fs {fs_lnxtype}')
+
count = (size + size_gran - 1) // size_gran
# Some distributions do not add /sbin to the default PATH, where mkfs lives
@@ -55,6 +62,8 @@
shell=True).decode()
if 'metadata_csum' in sb_content:
check_call(f'tune2fs -O ^metadata_csum {fs_img}', shell=True)
+ elif fs_lnxtype == 'vfat' and src_dir:
+ check_call(f'mcopy -i {fs_img} -vsmpQ {src_dir}/* ::/', shell=True)
return fs_img
except CalledProcessError:
call(f'rm -f {fs_img}', shell=True)
diff --git a/test/py/tests/test_fs/conftest.py b/test/py/tests/test_fs/conftest.py
index fca5448..59342a6 100644
--- a/test/py/tests/test_fs/conftest.py
+++ b/test/py/tests/test_fs/conftest.py
@@ -156,64 +156,6 @@
return True
return False
-fuse_mounted = False
-
-def mount_fs(fs_type, device, mount_point):
- """Mount a volume.
-
- Args:
- fs_type: File system type.
- device: Volume's file name.
- mount_point: Mount point.
-
- Return:
- Nothing.
- """
- global fuse_mounted
-
- try:
- check_call('guestmount --pid-file guestmount.pid -a %s -m /dev/sda %s'
- % (device, mount_point), shell=True)
- fuse_mounted = True
- return
- except CalledProcessError:
- fuse_mounted = False
-
- mount_opt = 'loop,rw'
- if re.match('fat', fs_type):
- mount_opt += ',umask=0000'
-
- check_call('sudo mount -o %s %s %s'
- % (mount_opt, device, mount_point), shell=True)
-
- # may not be effective for some file systems
- check_call('sudo chmod a+rw %s' % mount_point, shell=True)
-
-def umount_fs(mount_point):
- """Unmount a volume.
-
- Args:
- mount_point: Mount point.
-
- Return:
- Nothing.
- """
- if fuse_mounted:
- call('sync')
- call('guestunmount %s' % mount_point, shell=True)
-
- try:
- with open("guestmount.pid", "r") as pidfile:
- pid = int(pidfile.read())
- util.waitpid(pid, kill=True)
- os.remove("guestmount.pid")
-
- except FileNotFoundError:
- pass
-
- else:
- call('sudo umount %s' % mount_point, shell=True)
-
#
# Fixture for basic fs test
# derived from test/fs/fs-test.sh
@@ -242,14 +184,6 @@
big_file = mount_dir + '/' + BIG_FILE
try:
-
- # 3GiB volume
- fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0xc0000000, '3GB')
- except CalledProcessError as err:
- pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
- return
-
- try:
check_call('mkdir -p %s' % mount_dir, shell=True)
except CalledProcessError as err:
pytest.skip('Preparing mount folder failed for filesystem: ' + fs_type + '. {}'.format(err))
@@ -257,15 +191,6 @@
return
try:
- # Mount the image so we can populate it.
- mount_fs(fs_type, fs_img, mount_dir)
- except CalledProcessError as err:
- pytest.skip('Mounting to folder failed for filesystem: ' + fs_type + '. {}'.format(err))
- call('rmdir %s' % mount_dir, shell=True)
- call('rm -f %s' % fs_img, shell=True)
- return
-
- try:
# Create a subdirectory.
check_call('mkdir %s/SUBDIR' % mount_dir, shell=True)
@@ -326,15 +251,20 @@
% big_file, shell=True).decode()
md5val.append(out.split()[0])
+ try:
+ # 3GiB volume
+ fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0xc0000000, '3GB', mount_dir)
+ except CalledProcessError as err:
+ pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
+ return
+
except CalledProcessError as err:
pytest.skip('Setup failed for filesystem: ' + fs_type + '. {}'.format(err))
- umount_fs(mount_dir)
return
else:
- umount_fs(mount_dir)
yield [fs_ubtype, fs_img, md5val]
finally:
- call('rmdir %s' % mount_dir, shell=True)
+ call('rm -rf %s' % mount_dir, shell=True)
call('rm -f %s' % fs_img, shell=True)
#
@@ -364,14 +294,6 @@
tmp_file = mount_dir + '/tmpfile'
try:
-
- # 128MiB volume
- fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
- except CalledProcessError as err:
- pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
- return
-
- try:
check_call('mkdir -p %s' % mount_dir, shell=True)
except CalledProcessError as err:
pytest.skip('Preparing mount folder failed for filesystem: ' + fs_type + '. {}'.format(err))
@@ -379,15 +301,6 @@
return
try:
- # Mount the image so we can populate it.
- mount_fs(fs_type, fs_img, mount_dir)
- except CalledProcessError as err:
- pytest.skip('Mounting to folder failed for filesystem: ' + fs_type + '. {}'.format(err))
- call('rmdir %s' % mount_dir, shell=True)
- call('rm -f %s' % fs_img, shell=True)
- return
-
- try:
# Create a test directory
check_call('mkdir %s/dir1' % mount_dir, shell=True)
@@ -427,15 +340,21 @@
md5val.append(out.split()[0])
check_call('rm %s' % tmp_file, shell=True)
+
+ try:
+ # 128MiB volume
+ fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x8000000, '128MB', mount_dir)
+ except CalledProcessError as err:
+ pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
+ return
+
except CalledProcessError:
pytest.skip('Setup failed for filesystem: ' + fs_type)
- umount_fs(mount_dir)
return
else:
- umount_fs(mount_dir)
yield [fs_ubtype, fs_img, md5val]
finally:
- call('rmdir %s' % mount_dir, shell=True)
+ call('rm -rf %s' % mount_dir, shell=True)
call('rm -f %s' % fs_img, shell=True)
#
@@ -461,7 +380,7 @@
try:
# 128MiB volume
- fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
+ fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x8000000, '128MB', None)
except:
pytest.skip('Setup failed for filesystem: ' + fs_type)
return
@@ -493,14 +412,6 @@
mount_dir = u_boot_config.persistent_data_dir + '/mnt'
try:
-
- # 128MiB volume
- fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
- except CalledProcessError as err:
- pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
- return
-
- try:
check_call('mkdir -p %s' % mount_dir, shell=True)
except CalledProcessError as err:
pytest.skip('Preparing mount folder failed for filesystem: ' + fs_type + '. {}'.format(err))
@@ -508,15 +419,6 @@
return
try:
- # Mount the image so we can populate it.
- mount_fs(fs_type, fs_img, mount_dir)
- except CalledProcessError as err:
- pytest.skip('Mounting to folder failed for filesystem: ' + fs_type + '. {}'.format(err))
- call('rmdir %s' % mount_dir, shell=True)
- call('rm -f %s' % fs_img, shell=True)
- return
-
- try:
# Test Case 1 & 3
check_call('mkdir %s/dir1' % mount_dir, shell=True)
check_call('dd if=/dev/urandom of=%s/dir1/file1 bs=1K count=1'
@@ -538,15 +440,20 @@
check_call('dd if=/dev/urandom of=%s/dir5/file1 bs=1K count=1'
% mount_dir, shell=True)
+ try:
+ # 128MiB volume
+ fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x8000000, '128MB', mount_dir)
+ except CalledProcessError as err:
+ pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
+ return
+
except CalledProcessError:
pytest.skip('Setup failed for filesystem: ' + fs_type)
- umount_fs(mount_dir)
return
else:
- umount_fs(mount_dir)
yield [fs_ubtype, fs_img]
finally:
- call('rmdir %s' % mount_dir, shell=True)
+ call('rm -rf %s' % mount_dir, shell=True)
call('rm -f %s' % fs_img, shell=True)
#
@@ -576,14 +483,6 @@
medium_file = mount_dir + '/' + MEDIUM_FILE
try:
-
- # 1GiB volume
- fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x40000000, '1GB')
- except CalledProcessError as err:
- pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
- return
-
- try:
check_call('mkdir -p %s' % mount_dir, shell=True)
except CalledProcessError as err:
pytest.skip('Preparing mount folder failed for filesystem: ' + fs_type + '. {}'.format(err))
@@ -591,15 +490,6 @@
return
try:
- # Mount the image so we can populate it.
- mount_fs(fs_type, fs_img, mount_dir)
- except CalledProcessError as err:
- pytest.skip('Mounting to folder failed for filesystem: ' + fs_type + '. {}'.format(err))
- call('rmdir %s' % mount_dir, shell=True)
- call('rm -f %s' % fs_img, shell=True)
- return
-
- try:
# Create a subdirectory.
check_call('mkdir %s/SUBDIR' % mount_dir, shell=True)
@@ -621,15 +511,20 @@
% medium_file, shell=True).decode()
md5val.extend([out.split()[0]])
+ try:
+ # 1GiB volume
+ fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x40000000, '1GB', mount_dir)
+ except CalledProcessError as err:
+ pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
+ return
+
except CalledProcessError:
pytest.skip('Setup failed for filesystem: ' + fs_type)
- umount_fs(mount_dir)
return
else:
- umount_fs(mount_dir)
yield [fs_ubtype, fs_img, md5val]
finally:
- call('rmdir %s' % mount_dir, shell=True)
+ call('rm -rf %s' % mount_dir, shell=True)
call('rm -f %s' % fs_img, shell=True)
#
@@ -665,7 +560,7 @@
try:
# the volume size depends on the filesystem
- fs_img = fs_helper.mk_fs(u_boot_config, fs_type, fs_size, f'{fs_size}', 1024)
+ fs_img = fs_helper.mk_fs(u_boot_config, fs_type, fs_size, f'{fs_size}', None, 1024)
except:
pytest.skip('Setup failed for filesystem: ' + fs_type)
return
diff --git a/test/py/tests/test_ut.py b/test/py/tests/test_ut.py
index 7a0bde4..940373c 100644
--- a/test/py/tests/test_ut.py
+++ b/test/py/tests/test_ut.py
@@ -589,8 +589,8 @@
u_boot_utils.run_and_log(
u_boot_console, f'sfdisk {fn}', stdin=b'type=83')
- fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB')
- fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB')
+ fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB', None)
+ fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB', None)
mmc_dev = 6
fn = os.path.join(u_boot_console.config.source_dir, f'mmc{mmc_dev}.img')