Skip to content

Commit

Permalink
Merge pull request #1490 from Azure/release-2.2.38
Browse files Browse the repository at this point in the history
Release 2.2.38
  • Loading branch information
vrdmr authored Mar 22, 2019
2 parents 3f83843 + a8eb911 commit ca1d395
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 52 deletions.
2 changes: 1 addition & 1 deletion azurelinuxagent/common/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def get_distro():

AGENT_NAME = "WALinuxAgent"
AGENT_LONG_NAME = "Azure Linux Agent"
AGENT_VERSION = '2.2.37'
AGENT_VERSION = '2.2.38'
AGENT_LONG_VERSION = "{0}-{1}".format(AGENT_NAME, AGENT_VERSION)
AGENT_DESCRIPTION = """
The Azure Linux Agent supports the provisioning and running of Linux
Expand Down
74 changes: 55 additions & 19 deletions azurelinuxagent/daemon/resourcedisk/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import os
import re
import stat
import sys
import threading
from time import sleep
Expand Down Expand Up @@ -124,12 +125,13 @@ def mount_resource_disk(self, mount_point):
force_option = 'F'
if self.fs == 'xfs':
force_option = 'f'
mkfs_string = "mkfs.{0} -{2} {1}".format(self.fs, partition, force_option)
mkfs_string = "mkfs.{0} -{2} {1}".format(
self.fs, partition, force_option)

if "gpt" in ret[1]:
logger.info("GPT detected, finding partitions")
parts = [x for x in ret[1].split("\n") if
re.match("^\s*[0-9]+", x)]
re.match(r"^\s*[0-9]+", x)]
logger.info("Found {0} GPT partition(s).", len(parts))
if len(parts) > 1:
logger.info("Removing old GPT partitions")
Expand All @@ -138,18 +140,23 @@ def mount_resource_disk(self, mount_point):
shellutil.run("parted {0} rm {1}".format(device, i))

logger.info("Creating new GPT partition")
shellutil.run("parted {0} mkpart primary 0% 100%".format(device))
shellutil.run(
"parted {0} mkpart primary 0% 100%".format(device))

logger.info("Format partition [{0}]", mkfs_string)
shellutil.run(mkfs_string)
else:
logger.info("GPT not detected, determining filesystem")
ret = self.change_partition_type(suppress_message=True, option_str="{0} 1 -n".format(device))
ret = self.change_partition_type(
suppress_message=True,
option_str="{0} 1 -n".format(device))
ptype = ret[1].strip()
if ptype == "7" and self.fs != "ntfs":
logger.info("The partition is formatted with ntfs, updating "
"partition type to 83")
self.change_partition_type(suppress_message=False, option_str="{0} 1 83".format(device))
self.change_partition_type(
suppress_message=False,
option_str="{0} 1 83".format(device))
self.reread_partition_table(device)
logger.info("Format partition [{0}]", mkfs_string)
shellutil.run(mkfs_string)
Expand All @@ -169,7 +176,8 @@ def mount_resource_disk(self, mount_point):
attempts -= 1

if not os.path.exists(partition):
raise ResourceDiskError("Partition was not created [{0}]".format(partition))
raise ResourceDiskError(
"Partition was not created [{0}]".format(partition))

logger.info("Mount resource disk [{0}]", mount_string)
ret, output = shellutil.run_get_output(mount_string, chk_err=False)
Expand Down Expand Up @@ -215,14 +223,19 @@ def change_partition_type(self, suppress_message, option_str):
"""

command_to_use = '--part-type'
input = "sfdisk {0} {1} {2}".format(command_to_use, '-f' if suppress_message else '', option_str)
err_code, output = shellutil.run_get_output(input, chk_err=False, log_cmd=True)
input = "sfdisk {0} {1} {2}".format(
command_to_use, '-f' if suppress_message else '', option_str)
err_code, output = shellutil.run_get_output(
input, chk_err=False, log_cmd=True)

# fall back to -c
if err_code != 0:
logger.info("sfdisk with --part-type failed [{0}], retrying with -c", err_code)
logger.info(
"sfdisk with --part-type failed [{0}], retrying with -c",
err_code)
command_to_use = '-c'
input = "sfdisk {0} {1} {2}".format(command_to_use, '-f' if suppress_message else '', option_str)
input = "sfdisk {0} {1} {2}".format(
command_to_use, '-f' if suppress_message else '', option_str)
err_code, output = shellutil.run_get_output(input, log_cmd=True)

if err_code == 0:
Expand All @@ -245,16 +258,30 @@ def get_mount_string(mount_options, partition, mount_point):
else:
return 'mount {0} {1}'.format(partition, mount_point)

@staticmethod
def check_existing_swap_file(swapfile, swaplist, size):
if swapfile in swaplist and os.path.isfile(
swapfile) and os.path.getsize(swapfile) == size:
logger.info("Swap already enabled")
# restrict access to owner (remove all access from group, others)
swapfile_mode = os.stat(swapfile).st_mode
if swapfile_mode & (stat.S_IRWXG | stat.S_IRWXO):
swapfile_mode = swapfile_mode & ~(stat.S_IRWXG | stat.S_IRWXO)
logger.info(
"Changing mode of {0} to {1:o}".format(
swapfile, swapfile_mode))
os.chmod(swapfile, swapfile_mode)
return True

return False

def create_swap_space(self, mount_point, size_mb):
size_kb = size_mb * 1024
size = size_kb * 1024
swapfile = os.path.join(mount_point, 'swapfile')
swaplist = shellutil.run_get_output("swapon -s")[1]

if swapfile in swaplist \
and os.path.isfile(swapfile) \
and os.path.getsize(swapfile) == size:
logger.info("Swap already enabled")
if self.check_existing_swap_file(swapfile, swaplist, size):
return

if os.path.isfile(swapfile) and os.path.getsize(swapfile) != size:
Expand Down Expand Up @@ -296,7 +323,8 @@ def mkfile(self, filename, nbytes):
os.remove(filename)

# If file system is xfs, use dd right away as we have been reported that
# swap enabling fails in xfs fs when disk space is allocated with fallocate
# swap enabling fails in xfs fs when disk space is allocated with
# fallocate
ret = 0
fn_sh = shellutil.quote((filename,))
if self.fs != 'xfs':
Expand All @@ -305,13 +333,21 @@ def mkfile(self, filename, nbytes):
# Probable errors:
# - OSError: Seen on Cygwin, libc notimpl?
# - AttributeError: What if someone runs this under...
fd = None

try:
with open(filename, 'w') as f:
os.posix_fallocate(f.fileno(), 0, nbytes)
return 0
except:
fd = os.open(
filename,
os.O_CREAT | os.O_WRONLY | os.O_EXCL,
stat.S_IRUSR | stat.S_IWUSR)
os.posix_fallocate(fd, 0, nbytes)
return 0
except BaseException:
# Not confident with this thing, just keep trying...
pass
finally:
if fd is not None:
os.close(fd)

# fallocate command
ret = shellutil.run(
Expand Down
74 changes: 47 additions & 27 deletions azurelinuxagent/daemon/resourcedisk/freebsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from azurelinuxagent.common.exception import ResourceDiskError
from azurelinuxagent.daemon.resourcedisk.default import ResourceDiskHandler


class FreeBSDResourceDiskHandler(ResourceDiskHandler):
"""
This class handles resource disk mounting for FreeBSD.
Expand All @@ -36,6 +37,7 @@ class FreeBSDResourceDiskHandler(ResourceDiskHandler):
1. MBR: The resource disk partition is /dev/da1s1
2. GPT: The resource disk partition is /dev/da1p2, /dev/da1p1 is for reserved usage.
"""

def __init__(self):
super(FreeBSDResourceDiskHandler, self).__init__()

Expand All @@ -52,25 +54,30 @@ def parse_gpart_list(data):
def mount_resource_disk(self, mount_point):
fs = self.fs
if fs != 'ufs':
raise ResourceDiskError("Unsupported filesystem type:{0}, only ufs is supported.".format(fs))
raise ResourceDiskError(
"Unsupported filesystem type:{0}, only ufs is supported.".format(fs))

# 1. Detect device
err, output = shellutil.run_get_output('gpart list')
if err:
raise ResourceDiskError("Unable to detect resource disk device:{0}".format(output))
raise ResourceDiskError(
"Unable to detect resource disk device:{0}".format(output))
disks = self.parse_gpart_list(output)

device = self.osutil.device_for_ide_port(1)
if device is None or not device in disks:
# fallback logic to find device
err, output = shellutil.run_get_output('camcontrol periphlist 2:1:0')
if device is None or device not in disks:
# fallback logic to find device
err, output = shellutil.run_get_output(
'camcontrol periphlist 2:1:0')
if err:
# try again on "3:1:0"
err, output = shellutil.run_get_output('camcontrol periphlist 3:1:0')
err, output = shellutil.run_get_output(
'camcontrol periphlist 3:1:0')
if err:
raise ResourceDiskError("Unable to detect resource disk device:{0}".format(output))
raise ResourceDiskError(
"Unable to detect resource disk device:{0}".format(output))

# 'da1: generation: 4 index: 1 status: MORE\npass2: generation: 4 index: 2 status: LAST\n'
# 'da1: generation: 4 index: 1 status: MORE\npass2: generation: 4 index: 2 status: LAST\n'
for line in output.split('\n'):
index = line.find(':')
if index > 0:
Expand All @@ -91,9 +98,11 @@ def mount_resource_disk(self, mount_point):
elif partition_table_type == 'GPT':
provider_name = device + 'p2'
else:
raise ResourceDiskError("Unsupported partition table type:{0}".format(output))
raise ResourceDiskError(
"Unsupported partition table type:{0}".format(output))

err, output = shellutil.run_get_output('gpart show -p {0}'.format(device))
err, output = shellutil.run_get_output(
'gpart show -p {0}'.format(device))
if err or output.find(provider_name) == -1:
raise ResourceDiskError("Resource disk partition not found.")

Expand All @@ -112,16 +121,26 @@ def mount_resource_disk(self, mount_point):
mount_cmd = 'mount -t {0} {1} {2}'.format(fs, partition, mount_point)
err = shellutil.run(mount_cmd, chk_err=False)
if err:
logger.info('Creating {0} filesystem on partition {1}'.format(fs, partition))
err, output = shellutil.run_get_output('newfs -U {0}'.format(partition))
logger.info(
'Creating {0} filesystem on partition {1}'.format(
fs, partition))
err, output = shellutil.run_get_output(
'newfs -U {0}'.format(partition))
if err:
raise ResourceDiskError("Failed to create new filesystem on partition {0}, error:{1}"
.format(partition, output))
raise ResourceDiskError(
"Failed to create new filesystem on partition {0}, error:{1}" .format(
partition, output))
err, output = shellutil.run_get_output(mount_cmd, chk_err=False)
if err:
raise ResourceDiskError("Failed to mount partition {0}, error {1}".format(partition, output))

logger.info("Resource disk partition {0} is mounted at {1} with fstype {2}", partition, mount_point, fs)
raise ResourceDiskError(
"Failed to mount partition {0}, error {1}".format(
partition, output))

logger.info(
"Resource disk partition {0} is mounted at {1} with fstype {2}",
partition,
mount_point,
fs)
return mount_point

def create_swap_space(self, mount_point, size_mb):
Expand All @@ -130,10 +149,7 @@ def create_swap_space(self, mount_point, size_mb):
swapfile = os.path.join(mount_point, 'swapfile')
swaplist = shellutil.run_get_output("swapctl -l")[1]

if swapfile in swaplist \
and os.path.isfile(swapfile) \
and os.path.getsize(swapfile) == size:
logger.info("Swap already enabled")
if self.check_existing_swap_file(swapfile, swaplist, size):
return

if os.path.isfile(swapfile) and os.path.getsize(swapfile) != size:
Expand All @@ -144,20 +160,24 @@ def create_swap_space(self, mount_point, size_mb):
if not os.path.isfile(swapfile):
logger.info("Create swap file")
self.mkfile(swapfile, size_kb * 1024)

mddevice = shellutil.run_get_output("mdconfig -a -t vnode -f {0}".format(swapfile))[1].rstrip()

mddevice = shellutil.run_get_output(
"mdconfig -a -t vnode -f {0}".format(swapfile))[1].rstrip()
shellutil.run("chmod 0600 /dev/{0}".format(mddevice))

if conf.get_resourcedisk_enable_swap_encryption():
shellutil.run("kldload aesni")
shellutil.run("kldload cryptodev")
shellutil.run("kldload geom_eli")
shellutil.run("geli onetime -e AES-XTS -l 256 -d /dev/{0}".format(mddevice))
shellutil.run(
"geli onetime -e AES-XTS -l 256 -d /dev/{0}".format(mddevice))
shellutil.run("chmod 0600 /dev/{0}.eli".format(mddevice))
if shellutil.run("swapon /dev/{0}.eli".format(mddevice)):
raise ResourceDiskError("/dev/{0}.eli".format(mddevice))
logger.info("Enabled {0}KB of swap at /dev/{1}.eli ({2})".format(size_kb, mddevice, swapfile))
logger.info(
"Enabled {0}KB of swap at /dev/{1}.eli ({2})".format(size_kb, mddevice, swapfile))
else:
if shellutil.run("swapon /dev/{0}".format(mddevice)):
raise ResourceDiskError("/dev/{0}".format(mddevice))
logger.info("Enabled {0}KB of swap at /dev/{1} ({2})".format(size_kb, mddevice, swapfile))
logger.info(
"Enabled {0}KB of swap at /dev/{1} ({2})".format(size_kb, mddevice, swapfile))
Binary file removed tests/data/ga/WALinuxAgent-2.2.36.zip
Binary file not shown.
Binary file removed tests/data/ga/WALinuxAgent-2.2.37.zip
Binary file not shown.
Binary file added tests/data/ga/WALinuxAgent-2.2.38.zip
Binary file not shown.
Loading

0 comments on commit ca1d395

Please sign in to comment.