lib: make the loop device size can be increased
PATCH v5 1/1 lib: make the loop device size can be increased
For the purpose of satisfying specific requirements, here rename function tst_acqurire_device() to tst_acqurie_device_(), then add parameter '@size' to tst_acquire_device_() to make the test device can be increased according to real need.
This change gives a flexibility to ltp test device, you can customize its size by passing 'tst_test->device_min_size' to your cases. But it's limited that at least 150MB for the device generation.
Also, add tst_device.c tests, updating test-writing-guidelines.txt documents.
--- doc/test-writing-guidelines.txt | 12 ++++++---- include/old/old_device.h | 2 ++ include/tst_test.h | 2 ++ lib/newlib_tests/tst_device.c | 49 +++++++++++++++++++++++++++++++++++++++++ lib/tst_device.c | 30 ++++++++++++++++++++----- lib/tst_test.c | 5 ++++- testcases/lib/test.sh | 8 +++++-- 7 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 lib/newlib_tests/tst_device.c
doc/test-writing-guidelines.txt +8 -4
@@ -1323,12 +1323,16 @@ Following functions similar to the LTP C interface are available. * tst_rmdir() * tst_fs_has_free() * tst_mkfs() -* tst_acquire_device() * tst_release_device() -There is one more function called 'tst_check_cmds()' that gets unspecified -number of parameters and asserts that each parameter is a name of an -executable in '$PATH' and exits the test with 'TCONF' on first missing. +One more function called 'tst_check_cmds()' that gets unspecified number +of parameters and asserts that each parameter is a name of an executable +in '$PATH' and exits the test with 'TCONF' on first missing. + +Another one is 'tst_acquire_device()' that looks for LTP_DEV env variable +first (which may be passed by the test driver or by a user) and returns while +it's value greater than 150MB. It also does clear first sectors of $LTP_DEV +and makes the block device size can be increased according to real need. tst_sleep +++++++++
include/old/old_device.h +2 -0
@@ -44,6 +44,8 @@ const char *tst_dev_fs_type(void);*/ const char *tst_acquire_device (void (cleanup_fn )(void )); +const char *tst_acquire_device_ (void (cleanup_fn )(void ),unsigned int size); +/* * @dev: device path returned by the tst_acquire_device() */
include/tst_test.h +2 -0
@@ -106,6 +106,8 @@ struct tst_test { int needs_device:1; int needs_checkpoints:1; +unsigned int device_min_size; +/* override default timeout per test run */ unsigned int timeout;
lib/newlib_tests/tst_device.c new file +49 -0
49 lines not on this page · raw message
lib/tst_device.c +25 -5
@@ -184,8 +184,18 @@ static void detach_device(const char *dev)const char *tst_acquire_device (void (cleanup_fn )(void )) { +return tst_acquire_device_ (cleanup_fn,0 ); +} + +const char *tst_acquire_device_ (void (cleanup_fn )(void ),unsigned int size) +{ +int fd;char *dev;struct stat st; +unsigned int acq_dev_size; +unsigned int ltp_dev_size; + + acq_dev_size = size >150 ? size :150 ;if (device_acquired)tst_brkm (TBROK , cleanup_fn,"Device allready acquired" );
@@ -207,15 +217,25 @@ const char *tst_acquire_device(void (cleanup_fn)(void)) "%s is not a block device", dev); } -if (tst_fill_file (dev,0 ,1024 ,512 )) { -tst_brkm (TBROK |TERRNO , cleanup_fn, -"Failed to clear the first 512k of %s" , dev); + fd =SAFE_OPEN (cleanup_fn, dev,O_RDONLY ); +SAFE_IOCTL (cleanup_fn, fd,BLKGETSIZE64 , <p_dev_size); +SAFE_CLOSE (cleanup_fn, fd); + ltp_dev_size = ltp_dev_size/1024 /1024 ; + +if (acq_dev_size <= ltp_dev_size) { +if (tst_fill_file (dev,0 ,1024 ,512 )) { +tst_brkm (TBROK |TERRNO , cleanup_fn, +"Failed to clear the first 512k of %s" , dev); + } + +return dev; } -return dev; +tst_resm (TINFO ,"Skipping $LTP_DEV size %luMB, requested size %luMB" , + ltp_dev_size, acq_dev_size); } -if (tst_fill_file (DEV_FILE ,0 ,1024 ,153600 )) { +if (tst_fill_file (DEV_FILE ,0 ,1024 ,1024 * acq_dev_size)) {tst_brkm (TBROK |TERRNO , cleanup_fn,"Failed to create " DEV_FILE );
lib/tst_test.c +4 -1
@@ -596,7 +596,10 @@ static void do_setup(int argc, char *argv[]) }if (tst_test->needs_device) { - tdev.dev =tst_acquire_device (NULL ); +if (!tst_test->device_min_size) + tst_test->device_min_size =150 ; + + tdev.dev =tst_acquire_device_ (NULL , tst_test->device_min_size); tdev.fs_type =tst_dev_fs_type ();if (!tdev.dev)
testcases/lib/test.sh +6 -2
@@ -283,11 +283,15 @@ EXPECT_FAIL() tst_acquire_device() { + local acq_dev_size=${1:-150} + if [ -z ${TST_TMPDIR} ]; then tst_brkm "Use 'tst_tmpdir' before 'tst_acquire_device'" fi - if [ -n "${LTP_DEV}" ]; then + ltp_dev_size=$((`blockdev --getsize64 $LTP_DEV`/1024/1024)) + + if [ -n "${LTP_DEV}" ] && [ ${acq_dev_size} -le ${ltp_dev_size} ]; then tst_resm TINFO "Using test device LTP_DEV='${LTP_DEV}'" if [ ! -b ${LTP_DEV} ]; then tst_brkm TBROK "${LTP_DEV} is not a block device"
@@ -300,7 +304,7 @@ tst_acquire_device() return fi - ROD_SILENT dd if=/dev/zero of=test_dev.img bs=1024 count=153600 + ROD_SILENT dd if=/dev/zero of=test_dev.img bs=1024 count=$((1024*$acq_dev_size)) TST_DEVICE=$(losetup -f) if [ $? -ne 0 ]; then
Signature · 1 line
1.8.3.1
PATCH v5 1/1 lib: make the loop device size can be increased
Hi!
lib/tst_device.c static void detach_device(const char *dev) · line 188
Quoted 10 lines
lib/tst_device.c +2
-0@@ -184,8 +184,18 @@ static void detach_device(const char *dev)const char *tst_acquire_device (void (cleanup_fn )(void )) { +return tst_acquire_device_ (cleanup_fn,0 ); +}
This can be implemented as static inline function inside the header file.
Quoted 48 lines
lib/tst_device.c +22
-5+const char *tst_acquire_device_ (void (cleanup_fn )(void ),unsigned int size) +{ +int fd;char *dev;struct stat st; +unsigned int acq_dev_size; +unsigned int ltp_dev_size; + + acq_dev_size = size >150 ? size :150 ;if (device_acquired)tst_brkm (TBROK , cleanup_fn,"Device allready acquired" );@@ -207,15 +217,25 @@ const char *tst_acquire_device(void (cleanup_fn)(void)) "%s is not a block device", dev); } -if (tst_fill_file (dev,0 ,1024 ,512 )) { -tst_brkm (TBROK |TERRNO , cleanup_fn, -"Failed to clear the first 512k of %s" , dev); + fd =SAFE_OPEN (cleanup_fn, dev,O_RDONLY ); +SAFE_IOCTL (cleanup_fn, fd,BLKGETSIZE64 , <p_dev_size); +SAFE_CLOSE (cleanup_fn, fd); + ltp_dev_size = ltp_dev_size/1024 /1024 ; + +if (acq_dev_size <= ltp_dev_size) { +if (tst_fill_file (dev,0 ,1024 ,512 )) { +tst_brkm (TBROK |TERRNO , cleanup_fn, +"Failed to clear the first 512k of %s" , dev); + } + +return dev; } -return dev; +tst_resm (TINFO ,"Skipping $LTP_DEV size %luMB, requested size %luMB" , + ltp_dev_size, acq_dev_size); } -if (tst_fill_file (DEV_FILE ,0 ,1024 ,153600 )) { +if (tst_fill_file (DEV_FILE ,0 ,1024 ,1024 * acq_dev_size)) {tst_brkm (TBROK |TERRNO , cleanup_fn,"Failed to create " DEV_FILE );lib/tst_test.c +0
-0
lib/tst_test.c static void do_setup(int argc, char *argv[]) · line 600
Quoted 6 lines
lib/tst_test.c +2
-1@@ -596,7 +596,10 @@ static void do_setup(int argc, char *argv[]) }if (tst_test->needs_device) { - tdev.dev =tst_acquire_device (NULL ); +if (!tst_test->device_min_size) + tst_test->device_min_size =150 ;
The tst_acquire_device_ will make sure that the size is at least 150MB anyway. So this part is useless, isn't it?
Otherwise it looks good.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v5 1/1 lib: make the loop device size can be increased
Quoted 7 lines · Cyril Hrubis
Hi!
Quoted 6 lines
lib/tst_device.c +0
-0@@ -184,8 +184,18 @@ static void detach_device(const char *dev)
Quoted 7 lines
lib/tst_device.c static void detach_device(const char *dev) · line 188
Quoted 4 lines
lib/tst_device.c +2
-0const char *tst_acquire_device (void (cleanup_fn )(void )) { +return tst_acquire_device_ (cleanup_fn,0 ); +}This can be implemented as static inline function inside the header file.
Great! no problem.
Quoted 12 lines
Quoted 12 lines
lib/tst_device.c +1
-1-if (tst_fill_file (DEV_FILE ,0 ,1024 ,153600 )) { +if (tst_fill_file (DEV_FILE ,0 ,1024 ,1024 * acq_dev_size)) {tst_brkm (TBROK |TERRNO , cleanup_fn,"Failed to create " DEV_FILE );lib/tst_test.c +0
-0@@ -596,7 +596,10 @@ static void do_setup(int argc, char *argv[]) }
Quoted 7 lines
lib/tst_test.c static void do_setup(int argc, char *argv[]) · line 600
Quoted 4 lines
lib/tst_test.c +2
-1if (tst_test->needs_device) { - tdev.dev =tst_acquire_device (NULL ); +if (!tst_test->device_min_size) + tst_test->device_min_size =150 ;The
tst_acquire_device_will make sure that the size is at least 150MB anyway. So this part is useless, isn't it?
Yes, it's useless! From what I test on removing them, tst_device.c also works fine.
Quoted 2 lines
Otherwise it looks good.
Thanks for reviewing. Patch v6 is coming...
Li Wang