|
|
488707 |
From c0d49d739d39573b59c827c89f56386d162d9381 Mon Sep 17 00:00:00 2001
|
|
|
488707 |
From: Vitaly Kuznetsov <vkuznets@redhat.com>
|
|
|
488707 |
Date: Wed, 13 Mar 2019 18:44:24 +0000
|
|
|
488707 |
Subject: [PATCH] Add fixes for handling swap file and other nit fixes (#1485)
|
|
|
488707 |
|
|
|
488707 |
RH-Author: Vitaly Kuznetsov <vkuznets@redhat.com>
|
|
|
488707 |
Message-id: <20190313184424.29299-1-vkuznets@redhat.com>
|
|
|
488707 |
Patchwork-id: 84860
|
|
|
488707 |
O-Subject: [RHEL8 WALinuxAgent PATCH] Add fixes for handling swap file and other nit fixes (#1485)
|
|
|
488707 |
Bugzilla: 1688276
|
|
|
488707 |
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
|
|
|
488707 |
RH-Acked-by: Mohammed Gamal <mgamal@redhat.com>
|
|
|
488707 |
|
|
|
488707 |
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1684181
|
|
|
488707 |
Brew: https://brewweb.engineering.redhat.com/brew/taskinfo?taskID=20581233
|
|
|
488707 |
Tested: by me
|
|
|
488707 |
|
|
|
488707 |
This is to fix CVE-2019-0804: swapfile is created with weak permission.
|
|
|
488707 |
|
|
|
488707 |
commit 8b2fa7d6051d0ee9952be4b42185c24d2a2eacff
|
|
|
488707 |
Author: Varad Meru <vrdmr@users.noreply.github.com>
|
|
|
488707 |
Date: Tue Mar 12 12:54:08 2019 -0700
|
|
|
488707 |
|
|
|
488707 |
Add fixes for handling swap file and other nit fixes (#1485)
|
|
|
488707 |
|
|
|
488707 |
* Add fixes for handling swap file and other nit fixes
|
|
|
488707 |
|
|
|
488707 |
* Fixing bytearray and other nits
|
|
|
488707 |
|
|
|
488707 |
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
|
|
|
488707 |
|
|
|
488707 |
Conflicts:
|
|
|
488707 |
azurelinuxagent/daemon/resourcedisk/freebsd.py
|
|
|
488707 |
(requires additional commits, irrelevant to RHEL)
|
|
|
488707 |
|
|
|
488707 |
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
|
|
|
488707 |
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
|
|
|
488707 |
---
|
|
|
488707 |
azurelinuxagent/daemon/resourcedisk/default.py | 74 +++++++++++++++++++-------
|
|
|
488707 |
azurelinuxagent/daemon/resourcedisk/freebsd.py | 53 ++++++++++++------
|
|
|
488707 |
tests/distro/test_resourceDisk.py | 47 ++++++++++++++--
|
|
|
488707 |
3 files changed, 133 insertions(+), 41 deletions(-)
|
|
|
488707 |
|
|
|
488707 |
diff --git a/azurelinuxagent/daemon/resourcedisk/default.py b/azurelinuxagent/daemon/resourcedisk/default.py
|
|
|
488707 |
index 0f0925d..cfb76d2 100644
|
|
|
488707 |
--- a/azurelinuxagent/daemon/resourcedisk/default.py
|
|
|
488707 |
+++ b/azurelinuxagent/daemon/resourcedisk/default.py
|
|
|
488707 |
@@ -17,6 +17,7 @@
|
|
|
488707 |
|
|
|
488707 |
import os
|
|
|
488707 |
import re
|
|
|
488707 |
+import stat
|
|
|
488707 |
import sys
|
|
|
488707 |
import threading
|
|
|
488707 |
from time import sleep
|
|
|
488707 |
@@ -124,12 +125,13 @@ class ResourceDiskHandler(object):
|
|
|
488707 |
force_option = 'F'
|
|
|
488707 |
if self.fs == 'xfs':
|
|
|
488707 |
force_option = 'f'
|
|
|
488707 |
- mkfs_string = "mkfs.{0} -{2} {1}".format(self.fs, partition, force_option)
|
|
|
488707 |
+ mkfs_string = "mkfs.{0} -{2} {1}".format(
|
|
|
488707 |
+ self.fs, partition, force_option)
|
|
|
488707 |
|
|
|
488707 |
if "gpt" in ret[1]:
|
|
|
488707 |
logger.info("GPT detected, finding partitions")
|
|
|
488707 |
parts = [x for x in ret[1].split("\n") if
|
|
|
488707 |
- re.match("^\s*[0-9]+", x)]
|
|
|
488707 |
+ re.match(r"^\s*[0-9]+", x)]
|
|
|
488707 |
logger.info("Found {0} GPT partition(s).", len(parts))
|
|
|
488707 |
if len(parts) > 1:
|
|
|
488707 |
logger.info("Removing old GPT partitions")
|
|
|
488707 |
@@ -138,18 +140,23 @@ class ResourceDiskHandler(object):
|
|
|
488707 |
shellutil.run("parted {0} rm {1}".format(device, i))
|
|
|
488707 |
|
|
|
488707 |
logger.info("Creating new GPT partition")
|
|
|
488707 |
- shellutil.run("parted {0} mkpart primary 0% 100%".format(device))
|
|
|
488707 |
+ shellutil.run(
|
|
|
488707 |
+ "parted {0} mkpart primary 0% 100%".format(device))
|
|
|
488707 |
|
|
|
488707 |
logger.info("Format partition [{0}]", mkfs_string)
|
|
|
488707 |
shellutil.run(mkfs_string)
|
|
|
488707 |
else:
|
|
|
488707 |
logger.info("GPT not detected, determining filesystem")
|
|
|
488707 |
- ret = self.change_partition_type(suppress_message=True, option_str="{0} 1 -n".format(device))
|
|
|
488707 |
+ ret = self.change_partition_type(
|
|
|
488707 |
+ suppress_message=True,
|
|
|
488707 |
+ option_str="{0} 1 -n".format(device))
|
|
|
488707 |
ptype = ret[1].strip()
|
|
|
488707 |
if ptype == "7" and self.fs != "ntfs":
|
|
|
488707 |
logger.info("The partition is formatted with ntfs, updating "
|
|
|
488707 |
"partition type to 83")
|
|
|
488707 |
- self.change_partition_type(suppress_message=False, option_str="{0} 1 83".format(device))
|
|
|
488707 |
+ self.change_partition_type(
|
|
|
488707 |
+ suppress_message=False,
|
|
|
488707 |
+ option_str="{0} 1 83".format(device))
|
|
|
488707 |
self.reread_partition_table(device)
|
|
|
488707 |
logger.info("Format partition [{0}]", mkfs_string)
|
|
|
488707 |
shellutil.run(mkfs_string)
|
|
|
488707 |
@@ -169,7 +176,8 @@ class ResourceDiskHandler(object):
|
|
|
488707 |
attempts -= 1
|
|
|
488707 |
|
|
|
488707 |
if not os.path.exists(partition):
|
|
|
488707 |
- raise ResourceDiskError("Partition was not created [{0}]".format(partition))
|
|
|
488707 |
+ raise ResourceDiskError(
|
|
|
488707 |
+ "Partition was not created [{0}]".format(partition))
|
|
|
488707 |
|
|
|
488707 |
logger.info("Mount resource disk [{0}]", mount_string)
|
|
|
488707 |
ret, output = shellutil.run_get_output(mount_string, chk_err=False)
|
|
|
488707 |
@@ -215,14 +223,19 @@ class ResourceDiskHandler(object):
|
|
|
488707 |
"""
|
|
|
488707 |
|
|
|
488707 |
command_to_use = '--part-type'
|
|
|
488707 |
- input = "sfdisk {0} {1} {2}".format(command_to_use, '-f' if suppress_message else '', option_str)
|
|
|
488707 |
- err_code, output = shellutil.run_get_output(input, chk_err=False, log_cmd=True)
|
|
|
488707 |
+ input = "sfdisk {0} {1} {2}".format(
|
|
|
488707 |
+ command_to_use, '-f' if suppress_message else '', option_str)
|
|
|
488707 |
+ err_code, output = shellutil.run_get_output(
|
|
|
488707 |
+ input, chk_err=False, log_cmd=True)
|
|
|
488707 |
|
|
|
488707 |
# fall back to -c
|
|
|
488707 |
if err_code != 0:
|
|
|
488707 |
- logger.info("sfdisk with --part-type failed [{0}], retrying with -c", err_code)
|
|
|
488707 |
+ logger.info(
|
|
|
488707 |
+ "sfdisk with --part-type failed [{0}], retrying with -c",
|
|
|
488707 |
+ err_code)
|
|
|
488707 |
command_to_use = '-c'
|
|
|
488707 |
- input = "sfdisk {0} {1} {2}".format(command_to_use, '-f' if suppress_message else '', option_str)
|
|
|
488707 |
+ input = "sfdisk {0} {1} {2}".format(
|
|
|
488707 |
+ command_to_use, '-f' if suppress_message else '', option_str)
|
|
|
488707 |
err_code, output = shellutil.run_get_output(input, log_cmd=True)
|
|
|
488707 |
|
|
|
488707 |
if err_code == 0:
|
|
|
488707 |
@@ -245,16 +258,30 @@ class ResourceDiskHandler(object):
|
|
|
488707 |
else:
|
|
|
488707 |
return 'mount {0} {1}'.format(partition, mount_point)
|
|
|
488707 |
|
|
|
488707 |
+ @staticmethod
|
|
|
488707 |
+ def check_existing_swap_file(swapfile, swaplist, size):
|
|
|
488707 |
+ if swapfile in swaplist and os.path.isfile(
|
|
|
488707 |
+ swapfile) and os.path.getsize(swapfile) == size:
|
|
|
488707 |
+ logger.info("Swap already enabled")
|
|
|
488707 |
+ # restrict access to owner (remove all access from group, others)
|
|
|
488707 |
+ swapfile_mode = os.stat(swapfile).st_mode
|
|
|
488707 |
+ if swapfile_mode & (stat.S_IRWXG | stat.S_IRWXO):
|
|
|
488707 |
+ swapfile_mode = swapfile_mode & ~(stat.S_IRWXG | stat.S_IRWXO)
|
|
|
488707 |
+ logger.info(
|
|
|
488707 |
+ "Changing mode of {0} to {1:o}".format(
|
|
|
488707 |
+ swapfile, swapfile_mode))
|
|
|
488707 |
+ os.chmod(swapfile, swapfile_mode)
|
|
|
488707 |
+ return True
|
|
|
488707 |
+
|
|
|
488707 |
+ return False
|
|
|
488707 |
+
|
|
|
488707 |
def create_swap_space(self, mount_point, size_mb):
|
|
|
488707 |
size_kb = size_mb * 1024
|
|
|
488707 |
size = size_kb * 1024
|
|
|
488707 |
swapfile = os.path.join(mount_point, 'swapfile')
|
|
|
488707 |
swaplist = shellutil.run_get_output("swapon -s")[1]
|
|
|
488707 |
|
|
|
488707 |
- if swapfile in swaplist \
|
|
|
488707 |
- and os.path.isfile(swapfile) \
|
|
|
488707 |
- and os.path.getsize(swapfile) == size:
|
|
|
488707 |
- logger.info("Swap already enabled")
|
|
|
488707 |
+ if self.check_existing_swap_file(swapfile, swaplist, size):
|
|
|
488707 |
return
|
|
|
488707 |
|
|
|
488707 |
if os.path.isfile(swapfile) and os.path.getsize(swapfile) != size:
|
|
|
488707 |
@@ -296,7 +323,8 @@ class ResourceDiskHandler(object):
|
|
|
488707 |
os.remove(filename)
|
|
|
488707 |
|
|
|
488707 |
# If file system is xfs, use dd right away as we have been reported that
|
|
|
488707 |
- # swap enabling fails in xfs fs when disk space is allocated with fallocate
|
|
|
488707 |
+ # swap enabling fails in xfs fs when disk space is allocated with
|
|
|
488707 |
+ # fallocate
|
|
|
488707 |
ret = 0
|
|
|
488707 |
fn_sh = shellutil.quote((filename,))
|
|
|
488707 |
if self.fs != 'xfs':
|
|
|
488707 |
@@ -305,13 +333,21 @@ class ResourceDiskHandler(object):
|
|
|
488707 |
# Probable errors:
|
|
|
488707 |
# - OSError: Seen on Cygwin, libc notimpl?
|
|
|
488707 |
# - AttributeError: What if someone runs this under...
|
|
|
488707 |
+ fd = None
|
|
|
488707 |
+
|
|
|
488707 |
try:
|
|
|
488707 |
- with open(filename, 'w') as f:
|
|
|
488707 |
- os.posix_fallocate(f.fileno(), 0, nbytes)
|
|
|
488707 |
- return 0
|
|
|
488707 |
- except:
|
|
|
488707 |
+ fd = os.open(
|
|
|
488707 |
+ filename,
|
|
|
488707 |
+ os.O_CREAT | os.O_WRONLY | os.O_EXCL,
|
|
|
488707 |
+ stat.S_IRUSR | stat.S_IWUSR)
|
|
|
488707 |
+ os.posix_fallocate(fd, 0, nbytes)
|
|
|
488707 |
+ return 0
|
|
|
488707 |
+ except BaseException:
|
|
|
488707 |
# Not confident with this thing, just keep trying...
|
|
|
488707 |
pass
|
|
|
488707 |
+ finally:
|
|
|
488707 |
+ if fd is not None:
|
|
|
488707 |
+ os.close(fd)
|
|
|
488707 |
|
|
|
488707 |
# fallocate command
|
|
|
488707 |
ret = shellutil.run(
|
|
|
488707 |
diff --git a/azurelinuxagent/daemon/resourcedisk/freebsd.py b/azurelinuxagent/daemon/resourcedisk/freebsd.py
|
|
|
488707 |
index a65d7f8..a29df3a 100644
|
|
|
488707 |
--- a/azurelinuxagent/daemon/resourcedisk/freebsd.py
|
|
|
488707 |
+++ b/azurelinuxagent/daemon/resourcedisk/freebsd.py
|
|
|
488707 |
@@ -22,6 +22,7 @@ import azurelinuxagent.common.utils.shellutil as shellutil
|
|
|
488707 |
from azurelinuxagent.common.exception import ResourceDiskError
|
|
|
488707 |
from azurelinuxagent.daemon.resourcedisk.default import ResourceDiskHandler
|
|
|
488707 |
|
|
|
488707 |
+
|
|
|
488707 |
class FreeBSDResourceDiskHandler(ResourceDiskHandler):
|
|
|
488707 |
"""
|
|
|
488707 |
This class handles resource disk mounting for FreeBSD.
|
|
|
488707 |
@@ -34,6 +35,7 @@ class FreeBSDResourceDiskHandler(ResourceDiskHandler):
|
|
|
488707 |
1. MBR: The resource disk partition is /dev/da1s1
|
|
|
488707 |
2. GPT: The resource disk partition is /dev/da1p2, /dev/da1p1 is for reserved usage.
|
|
|
488707 |
"""
|
|
|
488707 |
+
|
|
|
488707 |
def __init__(self):
|
|
|
488707 |
super(FreeBSDResourceDiskHandler, self).__init__()
|
|
|
488707 |
|
|
|
488707 |
@@ -50,25 +52,30 @@ class FreeBSDResourceDiskHandler(ResourceDiskHandler):
|
|
|
488707 |
def mount_resource_disk(self, mount_point):
|
|
|
488707 |
fs = self.fs
|
|
|
488707 |
if fs != 'ufs':
|
|
|
488707 |
- raise ResourceDiskError("Unsupported filesystem type:{0}, only ufs is supported.".format(fs))
|
|
|
488707 |
+ raise ResourceDiskError(
|
|
|
488707 |
+ "Unsupported filesystem type:{0}, only ufs is supported.".format(fs))
|
|
|
488707 |
|
|
|
488707 |
# 1. Detect device
|
|
|
488707 |
err, output = shellutil.run_get_output('gpart list')
|
|
|
488707 |
if err:
|
|
|
488707 |
- raise ResourceDiskError("Unable to detect resource disk device:{0}".format(output))
|
|
|
488707 |
+ raise ResourceDiskError(
|
|
|
488707 |
+ "Unable to detect resource disk device:{0}".format(output))
|
|
|
488707 |
disks = self.parse_gpart_list(output)
|
|
|
488707 |
|
|
|
488707 |
device = self.osutil.device_for_ide_port(1)
|
|
|
488707 |
- if device is None or not device in disks:
|
|
|
488707 |
- # fallback logic to find device
|
|
|
488707 |
- err, output = shellutil.run_get_output('camcontrol periphlist 2:1:0')
|
|
|
488707 |
+ if device is None or device not in disks:
|
|
|
488707 |
+ # fallback logic to find device
|
|
|
488707 |
+ err, output = shellutil.run_get_output(
|
|
|
488707 |
+ 'camcontrol periphlist 2:1:0')
|
|
|
488707 |
if err:
|
|
|
488707 |
# try again on "3:1:0"
|
|
|
488707 |
- err, output = shellutil.run_get_output('camcontrol periphlist 3:1:0')
|
|
|
488707 |
+ err, output = shellutil.run_get_output(
|
|
|
488707 |
+ 'camcontrol periphlist 3:1:0')
|
|
|
488707 |
if err:
|
|
|
488707 |
- raise ResourceDiskError("Unable to detect resource disk device:{0}".format(output))
|
|
|
488707 |
+ raise ResourceDiskError(
|
|
|
488707 |
+ "Unable to detect resource disk device:{0}".format(output))
|
|
|
488707 |
|
|
|
488707 |
- # 'da1: generation: 4 index: 1 status: MORE\npass2: generation: 4 index: 2 status: LAST\n'
|
|
|
488707 |
+ # 'da1: generation: 4 index: 1 status: MORE\npass2: generation: 4 index: 2 status: LAST\n'
|
|
|
488707 |
for line in output.split('\n'):
|
|
|
488707 |
index = line.find(':')
|
|
|
488707 |
if index > 0:
|
|
|
488707 |
@@ -89,9 +96,11 @@ class FreeBSDResourceDiskHandler(ResourceDiskHandler):
|
|
|
488707 |
elif partition_table_type == 'GPT':
|
|
|
488707 |
provider_name = device + 'p2'
|
|
|
488707 |
else:
|
|
|
488707 |
- raise ResourceDiskError("Unsupported partition table type:{0}".format(output))
|
|
|
488707 |
+ raise ResourceDiskError(
|
|
|
488707 |
+ "Unsupported partition table type:{0}".format(output))
|
|
|
488707 |
|
|
|
488707 |
- err, output = shellutil.run_get_output('gpart show -p {0}'.format(device))
|
|
|
488707 |
+ err, output = shellutil.run_get_output(
|
|
|
488707 |
+ 'gpart show -p {0}'.format(device))
|
|
|
488707 |
if err or output.find(provider_name) == -1:
|
|
|
488707 |
raise ResourceDiskError("Resource disk partition not found.")
|
|
|
488707 |
|
|
|
488707 |
@@ -110,14 +119,24 @@ class FreeBSDResourceDiskHandler(ResourceDiskHandler):
|
|
|
488707 |
mount_cmd = 'mount -t {0} {1} {2}'.format(fs, partition, mount_point)
|
|
|
488707 |
err = shellutil.run(mount_cmd, chk_err=False)
|
|
|
488707 |
if err:
|
|
|
488707 |
- logger.info('Creating {0} filesystem on partition {1}'.format(fs, partition))
|
|
|
488707 |
- err, output = shellutil.run_get_output('newfs -U {0}'.format(partition))
|
|
|
488707 |
+ logger.info(
|
|
|
488707 |
+ 'Creating {0} filesystem on partition {1}'.format(
|
|
|
488707 |
+ fs, partition))
|
|
|
488707 |
+ err, output = shellutil.run_get_output(
|
|
|
488707 |
+ 'newfs -U {0}'.format(partition))
|
|
|
488707 |
if err:
|
|
|
488707 |
- raise ResourceDiskError("Failed to create new filesystem on partition {0}, error:{1}"
|
|
|
488707 |
- .format(partition, output))
|
|
|
488707 |
+ raise ResourceDiskError(
|
|
|
488707 |
+ "Failed to create new filesystem on partition {0}, error:{1}" .format(
|
|
|
488707 |
+ partition, output))
|
|
|
488707 |
err, output = shellutil.run_get_output(mount_cmd, chk_err=False)
|
|
|
488707 |
if err:
|
|
|
488707 |
- raise ResourceDiskError("Failed to mount partition {0}, error {1}".format(partition, output))
|
|
|
488707 |
-
|
|
|
488707 |
- logger.info("Resource disk partition {0} is mounted at {1} with fstype {2}", partition, mount_point, fs)
|
|
|
488707 |
+ raise ResourceDiskError(
|
|
|
488707 |
+ "Failed to mount partition {0}, error {1}".format(
|
|
|
488707 |
+ partition, output))
|
|
|
488707 |
+
|
|
|
488707 |
+ logger.info(
|
|
|
488707 |
+ "Resource disk partition {0} is mounted at {1} with fstype {2}",
|
|
|
488707 |
+ partition,
|
|
|
488707 |
+ mount_point,
|
|
|
488707 |
+ fs)
|
|
|
488707 |
return mount_point
|
|
|
488707 |
diff --git a/tests/distro/test_resourceDisk.py b/tests/distro/test_resourceDisk.py
|
|
|
488707 |
index d2ce6e1..5f9db0a 100644
|
|
|
488707 |
--- a/tests/distro/test_resourceDisk.py
|
|
|
488707 |
+++ b/tests/distro/test_resourceDisk.py
|
|
|
488707 |
@@ -18,6 +18,8 @@
|
|
|
488707 |
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
|
|
488707 |
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
|
|
488707 |
|
|
|
488707 |
+import os
|
|
|
488707 |
+import stat
|
|
|
488707 |
import sys
|
|
|
488707 |
from azurelinuxagent.common.utils import shellutil
|
|
|
488707 |
from azurelinuxagent.daemon.resourcedisk import get_resourcedisk_handler
|
|
|
488707 |
@@ -38,6 +40,11 @@ class TestResourceDisk(AgentTestCase):
|
|
|
488707 |
# assert
|
|
|
488707 |
assert os.path.exists(test_file)
|
|
|
488707 |
|
|
|
488707 |
+ # only the owner should have access
|
|
|
488707 |
+ mode = os.stat(test_file).st_mode & (
|
|
|
488707 |
+ stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
|
|
|
488707 |
+ assert mode == stat.S_IRUSR | stat.S_IWUSR
|
|
|
488707 |
+
|
|
|
488707 |
# cleanup
|
|
|
488707 |
os.remove(test_file)
|
|
|
488707 |
|
|
|
488707 |
@@ -49,7 +56,7 @@ class TestResourceDisk(AgentTestCase):
|
|
|
488707 |
file_size = 1024 * 128
|
|
|
488707 |
|
|
|
488707 |
# execute
|
|
|
488707 |
- if sys.version_info >= (3,3):
|
|
|
488707 |
+ if sys.version_info >= (3, 3):
|
|
|
488707 |
with patch("os.posix_fallocate",
|
|
|
488707 |
side_effect=Exception('failure')):
|
|
|
488707 |
get_resourcedisk_handler().mkfile(test_file, file_size)
|
|
|
488707 |
@@ -76,20 +83,20 @@ class TestResourceDisk(AgentTestCase):
|
|
|
488707 |
resource_disk_handler.mkfile(test_file, file_size)
|
|
|
488707 |
|
|
|
488707 |
# assert
|
|
|
488707 |
- if sys.version_info >= (3,3):
|
|
|
488707 |
+ if sys.version_info >= (3, 3):
|
|
|
488707 |
with patch("os.posix_fallocate") as posix_fallocate:
|
|
|
488707 |
self.assertEqual(0, posix_fallocate.call_count)
|
|
|
488707 |
|
|
|
488707 |
assert run_patch.call_count == 1
|
|
|
488707 |
assert "dd if" in run_patch.call_args_list[0][0][0]
|
|
|
488707 |
|
|
|
488707 |
-
|
|
|
488707 |
def test_change_partition_type(self):
|
|
|
488707 |
resource_handler = get_resourcedisk_handler()
|
|
|
488707 |
# test when sfdisk --part-type does not exist
|
|
|
488707 |
with patch.object(shellutil, "run_get_output",
|
|
|
488707 |
side_effect=[[1, ''], [0, '']]) as run_patch:
|
|
|
488707 |
- resource_handler.change_partition_type(suppress_message=True, option_str='')
|
|
|
488707 |
+ resource_handler.change_partition_type(
|
|
|
488707 |
+ suppress_message=True, option_str='')
|
|
|
488707 |
|
|
|
488707 |
# assert
|
|
|
488707 |
assert run_patch.call_count == 2
|
|
|
488707 |
@@ -99,12 +106,42 @@ class TestResourceDisk(AgentTestCase):
|
|
|
488707 |
# test when sfdisk --part-type exists
|
|
|
488707 |
with patch.object(shellutil, "run_get_output",
|
|
|
488707 |
side_effect=[[0, '']]) as run_patch:
|
|
|
488707 |
- resource_handler.change_partition_type(suppress_message=True, option_str='')
|
|
|
488707 |
+ resource_handler.change_partition_type(
|
|
|
488707 |
+ suppress_message=True, option_str='')
|
|
|
488707 |
|
|
|
488707 |
# assert
|
|
|
488707 |
assert run_patch.call_count == 1
|
|
|
488707 |
assert "sfdisk --part-type" in run_patch.call_args_list[0][0][0]
|
|
|
488707 |
|
|
|
488707 |
+ def test_check_existing_swap_file(self):
|
|
|
488707 |
+ test_file = os.path.join(self.tmp_dir, 'test_swap_file')
|
|
|
488707 |
+ file_size = 1024 * 128
|
|
|
488707 |
+ if os.path.exists(test_file):
|
|
|
488707 |
+ os.remove(test_file)
|
|
|
488707 |
+
|
|
|
488707 |
+ with open(test_file, "wb") as file:
|
|
|
488707 |
+ file.write(bytearray(file_size))
|
|
|
488707 |
+
|
|
|
488707 |
+ os.chmod(test_file, stat.S_ISUID | stat.S_ISGID | stat.S_IRUSR |
|
|
|
488707 |
+ stat.S_IWUSR | stat.S_IRWXG | stat.S_IRWXO) # 0o6677
|
|
|
488707 |
+
|
|
|
488707 |
+ def swap_on(_): # mimic the output of "swapon -s"
|
|
|
488707 |
+ return [
|
|
|
488707 |
+ "Filename Type Size Used Priority",
|
|
|
488707 |
+ "{0} partition 16498684 0 -2".format(test_file)
|
|
|
488707 |
+ ]
|
|
|
488707 |
+
|
|
|
488707 |
+ with patch.object(shellutil, "run_get_output", side_effect=swap_on):
|
|
|
488707 |
+ get_resourcedisk_handler().check_existing_swap_file(
|
|
|
488707 |
+ test_file, test_file, file_size)
|
|
|
488707 |
+
|
|
|
488707 |
+ # it should remove access from group, others
|
|
|
488707 |
+ mode = os.stat(test_file).st_mode & (stat.S_ISUID | stat.S_ISGID |
|
|
|
488707 |
+ stat.S_IRWXU | stat.S_IWUSR | stat.S_IRWXG | stat.S_IRWXO) # 0o6777
|
|
|
488707 |
+ assert mode == stat.S_ISUID | stat.S_ISGID | stat.S_IRUSR | stat.S_IWUSR # 0o6600
|
|
|
488707 |
+
|
|
|
488707 |
+ os.remove(test_file)
|
|
|
488707 |
+
|
|
|
488707 |
|
|
|
488707 |
if __name__ == '__main__':
|
|
|
488707 |
unittest.main()
|
|
|
488707 |
--
|
|
|
488707 |
1.8.3.1
|
|
|
488707 |
|