From dffadabb945f5fa16a15ed46ea7576706ed4a083 Mon Sep 17 00:00:00 2001 From: Takuya ASADA Date: Mon, 30 Jan 2023 19:20:42 +0900 Subject: [PATCH] scylla_swap_setup: run error check before allocating swap We should run error check before running dd, otherwise it will left swapfile on disk without completing swap setup. Signed-off-by: Takuya ASADA --- dist/common/scripts/scylla_swap_setup | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/dist/common/scripts/scylla_swap_setup b/dist/common/scripts/scylla_swap_setup index 852784692b..de58d5a2e0 100755 --- a/dist/common/scripts/scylla_swap_setup +++ b/dist/common/scripts/scylla_swap_setup @@ -42,6 +42,17 @@ if __name__ == '__main__': print("Cannot specify both --swap-size and --swap-size-bytes") sys.exit(1) + swapfile = Path(args.swap_directory) / 'swapfile' + if swapfile.exists(): + print('swapfile {} already exists'.format(swapfile)) + sys.exit(1) + + swapunit_bn = out('systemd-escape -p --suffix=swap {}'.format(swapfile)) + swapunit = Path('/etc/systemd/system/{}'.format(swapunit_bn)) + if not args.overwrite_unit_file and swapunit.exists(): + print('swap unit {} already exists'.format(swapunit)) + sys.exit(1) + diskfree = psutil.disk_usage(args.swap_directory).free if args.swap_size or args.swap_size_bytes: if args.swap_size: @@ -71,18 +82,9 @@ if __name__ == '__main__': swapsize = half_of_diskfree swapsize_mb = int(swapsize / 1024 / 1024) - swapfile = Path(args.swap_directory) / 'swapfile' - if swapfile.exists(): - print('swapfile {} already exists'.format(swapfile)) - sys.exit(1) run('dd if=/dev/zero of={} bs=1M count={}'.format(swapfile, swapsize_mb), shell=True, check=True) swapfile.chmod(0o600) run('mkswap -f {}'.format(swapfile), shell=True, check=True) - swapunit_bn = out('systemd-escape -p --suffix=swap {}'.format(swapfile)) - swapunit = Path('/etc/systemd/system/{}'.format(swapunit_bn)) - if swapunit.exists(): - print('swap unit {} already exists'.format(swapunit)) - sys.exit(1) unit_data = ''' [Unit] Description=swapfile