summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2023-05-17 13:27:55 +0200
committerKarel Zak <kzak@redhat.com>2023-05-17 13:27:55 +0200
commit372e026e09382dae5385718567ecd97ad0637ece (patch)
tree9d7edf56431ecf62b2e3a6fc19cd3ccbe9ba8386
parent976157c821d81080b707315dfa17bbff87e9f5ca (diff)
parent84ec6f99f7927e5dc05a20f133b21f5870923ff1 (diff)
downloadutil-linux-372e026e09382dae5385718567ecd97ad0637ece.tar.gz
Merge branch 'mkswap/offset' of https://github.com/t-8ch/util-linux
* 'mkswap/offset' of https://github.com/t-8ch/util-linux: mkswap: implement --offset mkswap: (tests) don't overwrite logfiles mkswap: (tests) validate existence of truncate command
-rw-r--r--bash-completion/mkswap6
-rw-r--r--disk-utils/mkswap.8.adoc3
-rw-r--r--disk-utils/mkswap.c18
-rwxr-xr-xtests/ts/mkswap/mkswap31
4 files changed, 52 insertions, 6 deletions
diff --git a/bash-completion/mkswap b/bash-completion/mkswap
index 61157ef13..33b0a70ce 100644
--- a/bash-completion/mkswap
+++ b/bash-completion/mkswap
@@ -21,13 +21,17 @@ _mkswap_module()
COMPREPLY=( $(compgen -W "$(uuidgen -r)" -- $cur) )
return 0
;;
+ '-o'|'--offset')
+ COMPREPLY=( $(compgen -W "bytes" -- $cur) )
+ return 0
+ ;;
'-h'|'--help'|'-V'|'--version')
return 0
;;
esac
case $cur in
-*)
- OPTS="--check --force --pagesize --lock --label --swapversion --uuid --verbose --version --help"
+ OPTS="--check --force --pagesize --lock --label --swapversion --uuid --offset --verbose --version --help"
COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
return 0
;;
diff --git a/disk-utils/mkswap.8.adoc b/disk-utils/mkswap.8.adoc
index 904467d77..ad48c99e0 100644
--- a/disk-utils/mkswap.8.adoc
+++ b/disk-utils/mkswap.8.adoc
@@ -73,6 +73,9 @@ generate a new time-based UUID
*-e*, *--endianness* _ENDIANNESS_::
Specify the _ENDIANNESS_ to use, valid arguments are *native*, *little* or *big*. The default is *native*.
+*-o*, *--offset* _offset_::
+Specify the _offset_ to write the swap area to.
+
*-v*, *--swapversion 1*::
Specify the swap-space version. (This option is currently pointless, as the old *-v 0* option has become obsolete and now only *-v 1* is supported. The kernel has not supported v0 swap-space format since 2.5.22 (June 2002). The new version v1 is supported since 2.1.117 (August 1998).)
diff --git a/disk-utils/mkswap.c b/disk-utils/mkswap.c
index bd0230177..b6deadf38 100644
--- a/disk-utils/mkswap.c
+++ b/disk-utils/mkswap.c
@@ -76,6 +76,7 @@ struct mkswap_control {
int user_pagesize; /* --pagesize */
int pagesize; /* final pagesize used for the header */
+ off_t offset; /* offset of the header in the target */
char *opt_label; /* LABEL as specified on command line */
unsigned char *uuid; /* UUID parsed by libbuuid */
@@ -194,6 +195,7 @@ static void __attribute__((__noreturn__)) usage(void)
fprintf(out,
_(" -e, --endianness=<value> specify the endianness to use "
"(%s, %s or %s)\n"), "native", "little", "big");
+ fputs(_(" -o, --offset OFFSET specify the offset in the device\n"), out);
fputs(_(" --verbose verbose output\n"), out);
fprintf(out,
@@ -347,6 +349,9 @@ static unsigned long long get_size(const struct mkswap_control *ctl)
err(EXIT_FAILURE, _("cannot open %s"), ctl->devname);
if (blkdev_get_size(fd, &size) < 0)
err(EXIT_FAILURE, _("cannot determine size of %s"), ctl->devname);
+ if ((unsigned long long) ctl->offset > size)
+ errx(EXIT_FAILURE, _("offset larger than file size"));
+ size -= ctl->offset;
size /= ctl->pagesize;
close(fd);
@@ -465,11 +470,15 @@ static void wipe_device(struct mkswap_control *ctl)
static void write_header_to_device(struct mkswap_control *ctl)
{
+ off_t offset;
+
assert(ctl);
assert(ctl->fd > -1);
assert(ctl->signature_page);
- if (lseek(ctl->fd, SIGNATURE_OFFSET, SEEK_SET) != SIGNATURE_OFFSET)
+ offset = SIGNATURE_OFFSET + ctl->offset;
+
+ if (lseek(ctl->fd, offset, SEEK_SET) != offset)
errx(EXIT_FAILURE, _("unable to rewind swap-device"));
if (write_all(ctl->fd, (char *) ctl->signature_page + SIGNATURE_OFFSET,
@@ -503,6 +512,7 @@ int main(int argc, char **argv)
{ "swapversion", required_argument, NULL, 'v' },
{ "uuid", required_argument, NULL, 'U' },
{ "endianness", required_argument, NULL, 'e' },
+ { "offset", required_argument, NULL, 'o' },
{ "version", no_argument, NULL, 'V' },
{ "help", no_argument, NULL, 'h' },
{ "lock", optional_argument, NULL, OPT_LOCK },
@@ -521,7 +531,7 @@ int main(int argc, char **argv)
textdomain(PACKAGE);
close_stdout_atexit();
- while((c = getopt_long(argc, argv, "cfp:qL:v:U:e:Vh", longopts, NULL)) != -1) {
+ while((c = getopt_long(argc, argv, "cfp:qL:v:U:e:o:Vh", longopts, NULL)) != -1) {
err_exclusive_options(c, longopts, excl, excl_st);
@@ -567,6 +577,10 @@ int main(int argc, char **argv)
_("invalid endianness %s is not supported"), optarg);
}
break;
+ case 'o':
+ ctl.offset = str2unum_or_err(optarg,
+ 10, _("Invalid offset"), SINT_MAX(off_t));
+ break;
case 'V':
print_version(EXIT_SUCCESS);
break;
diff --git a/tests/ts/mkswap/mkswap b/tests/ts/mkswap/mkswap
index fa4856b71..c4fdce4f0 100755
--- a/tests/ts/mkswap/mkswap
+++ b/tests/ts/mkswap/mkswap
@@ -27,6 +27,7 @@ ts_check_test_command "$TS_CMD_MKSWAP"
ts_check_test_command "$TS_HELPER_SYSINFO"
ts_check_prog "xz"
ts_check_prog "cmp"
+ts_check_prog "truncate"
UUID=4c08e1cd-3c82-46bf-a55b-0c3270d6dfeb
@@ -49,17 +50,41 @@ for PAGESIZE in 4096 8192; do
ts_init_subtest $name
rm -f "$outimg"
- truncate -s $(( PAGESIZE * 10 )) "$outimg" > $TS_ERRLOG 2>&1
+ truncate -s $(( PAGESIZE * 10 )) "$outimg" >> $TS_ERRLOG 2>&1
"$TS_CMD_MKSWAP" -q -L label -U "$UUID" -e "$ENDIANNESS" -p "$PAGESIZE" "$outimg" \
- > "$TS_OUTPUT" 2>/dev/null \
+ >> "$TS_OUTPUT" 2>/dev/null \
|| ts_log "mkswap failed"
xz -dc "$TS_SELF/${BYTE_ORDER}-${PAGESIZE}.img.xz" > "$origimg"
- cmp "$origimg" "$outimg" > "$TS_ERRLOG" 2>&1
+ cmp "$origimg" "$outimg" >> "$TS_ERRLOG" 2>&1
ts_finalize_subtest
done
done
+ts_init_subtest offset
+
+offset=10000
+outimg="$TS_OUTDIR/offset.img"
+
+rm -f "$outimg"
+truncate -s $(( 4096 * 10 )) "$outimg" > $TS_ERRLOG 2>&1
+
+rm -f "$outimg.offset"
+truncate -s $(( 4096 * 10 + $offset )) "$outimg.offset" > $TS_ERRLOG 2>&1
+
+"$TS_CMD_MKSWAP" -q -U "$UUID" -p 4096 "$outimg" \
+ >> "$TS_OUTPUT" 2>/dev/null \
+ || ts_log "mkswap failed"
+
+"$TS_CMD_MKSWAP" -q -U "$UUID" -p 4096 -o "$offset" "$outimg.offset" \
+ >> "$TS_OUTPUT" 2>/dev/null \
+ || ts_log "mkswap -o failed"
+
+cmp -n "$offset" "$outimg.offset" /dev/zero >> "$TS_ERRLOG" 2>&1
+cmp "$outimg" "$outimg.offset" 0 "$offset" >> "$TS_ERRLOG" 2>&1
+
+ts_finalize_subtest
+
ts_finalize