|
| 1 | +--- azurelinuxagent/daemon/resourcedisk/freebsd.py.orig 2020-05-22 12:55:32 UTC |
| 2 | ++++ azurelinuxagent/daemon/resourcedisk/freebsd.py |
| 3 | +@@ -1,6 +1,7 @@ |
| 4 | + # Microsoft Azure Linux Agent |
| 5 | + # |
| 6 | + # Copyright 2018 Microsoft Corporation |
| 7 | ++# Copyright 2020 The FreeBSD Foundation |
| 8 | + # |
| 9 | + # Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + # you may not use this file except in compliance with the License. |
| 11 | +@@ -24,6 +25,8 @@ import azurelinuxagent.common.conf as conf |
| 12 | + from azurelinuxagent.common.exception import ResourceDiskError |
| 13 | + from azurelinuxagent.daemon.resourcedisk.default import ResourceDiskHandler |
| 14 | + |
| 15 | ++# Keeping 1GB sapce if configured swap size is larger than the disk size |
| 16 | ++MINIMAL_RESOURCE_PARTITION_SIZE = 1024 ** 3 |
| 17 | + |
| 18 | + class FreeBSDResourceDiskHandler(ResourceDiskHandler): |
| 19 | + """ |
| 20 | +@@ -32,37 +35,45 @@ class FreeBSDResourceDiskHandler(ResourceDiskHandler): |
| 21 | + The resource disk locates at following slot: |
| 22 | + scbus2 on blkvsc1 bus 0: |
| 23 | + <Msft Virtual Disk 1.0> at scbus2 target 1 lun 0 (da1,pass2) |
| 24 | +- |
| 25 | +- There are 2 variations based on partition table type: |
| 26 | +- 1. MBR: The resource disk partition is /dev/da1s1 |
| 27 | +- 2. GPT: The resource disk partition is /dev/da1p2, /dev/da1p1 is for reserved usage. |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__(self): |
| 31 | + super(FreeBSDResourceDiskHandler, self).__init__() |
| 32 | + |
| 33 | + @staticmethod |
| 34 | +- def parse_gpart_list(data): |
| 35 | ++ def parse_gpart_show(data): |
| 36 | + dic = {} |
| 37 | + for line in data.split('\n'): |
| 38 | +- if line.find("Geom name: ") != -1: |
| 39 | +- geom_name = line[11:] |
| 40 | +- elif line.find("scheme: ") != -1: |
| 41 | +- dic[geom_name] = line[8:] |
| 42 | ++ if line: |
| 43 | ++ l = line.split() |
| 44 | ++ dic[l[3]] = l[4] |
| 45 | + return dic |
| 46 | + |
| 47 | ++ @staticmethod |
| 48 | ++ def parse_mount_list(data): |
| 49 | ++ dic = {} |
| 50 | ++ for line in data.split('\n'): |
| 51 | ++ if line: |
| 52 | ++ l = line.split() |
| 53 | ++ dic[l[2]] = l[0] |
| 54 | ++ return dic |
| 55 | ++ |
| 56 | ++ @staticmethod |
| 57 | ++ def get_next_partition(x): |
| 58 | ++ return x[:-1] + str(int(x[-1]) + 1) |
| 59 | ++ |
| 60 | + def mount_resource_disk(self, mount_point): |
| 61 | + fs = self.fs |
| 62 | + if fs != 'ufs': |
| 63 | +- raise ResourceDiskError( |
| 64 | +- "Unsupported filesystem type:{0}, only ufs is supported.".format(fs)) |
| 65 | ++ raise ResourceDiskError("Unsupported filesystem type: " |
| 66 | ++ "{0}, only ufs is supported.".format(fs)) |
| 67 | + |
| 68 | + # 1. Detect device |
| 69 | +- err, output = shellutil.run_get_output('gpart list') |
| 70 | ++ err, output = shellutil.run_get_output("gpart show | grep '=>'") |
| 71 | + if err: |
| 72 | + raise ResourceDiskError( |
| 73 | + "Unable to detect resource disk device:{0}".format(output)) |
| 74 | +- disks = self.parse_gpart_list(output) |
| 75 | ++ disks = self.parse_gpart_show(output) |
| 76 | + |
| 77 | + device = self.osutil.device_for_ide_port(1) |
| 78 | + if device is None or device not in disks: |
| 79 | +@@ -90,94 +101,186 @@ class FreeBSDResourceDiskHandler(ResourceDiskHandler): |
| 80 | + raise ResourceDiskError("Unable to detect resource disk device.") |
| 81 | + logger.info('Resource disk device {0} found.', device) |
| 82 | + |
| 83 | +- # 2. Detect partition |
| 84 | +- partition_table_type = disks[device] |
| 85 | ++ # 2. Detect/create partition |
| 86 | + |
| 87 | +- if partition_table_type == 'MBR': |
| 88 | +- provider_name = device + 's1' |
| 89 | +- elif partition_table_type == 'GPT': |
| 90 | +- provider_name = device + 'p2' |
| 91 | +- else: |
| 92 | +- raise ResourceDiskError( |
| 93 | +- "Unsupported partition table type:{0}".format(output)) |
| 94 | ++ # count the target size of each partition |
| 95 | ++ err, output = shellutil.run_get_output("diskinfo {0}".format(device)) |
| 96 | ++ if err: |
| 97 | ++ raise ResourceDiskError("Cannot get resource disk size.") |
| 98 | ++ disk_info = output.split() |
| 99 | ++ block_size = int(disk_info[1]) |
| 100 | ++ disk_size = int(disk_info[2]) |
| 101 | + |
| 102 | ++ swap_size = 0 |
| 103 | ++ if conf.get_resourcedisk_enable_swap(): |
| 104 | ++ swap_size_mb = conf.get_resourcedisk_swap_size_mb() |
| 105 | ++ swap_size = swap_size_mb * 1024 * 1024 |
| 106 | ++ resource_size = disk_size - swap_size |
| 107 | ++ if resource_size < MINIMAL_RESOURCE_PARTITION_SIZE: |
| 108 | ++ resource_size = MINIMAL_RESOURCE_PARTITION_SIZE |
| 109 | ++ swap_size = disk_size - resource_size |
| 110 | ++ |
| 111 | ++ # get size of the current swap partition |
| 112 | ++ current_swap_size = 0 |
| 113 | + err, output = shellutil.run_get_output( |
| 114 | +- 'gpart show -p {0}'.format(device)) |
| 115 | +- if err or output.find(provider_name) == -1: |
| 116 | +- raise ResourceDiskError("Resource disk partition not found.") |
| 117 | ++ "gpart show {0} 2>/dev/null | grep freebsd-swap".format(device), |
| 118 | ++ chk_err=False) |
| 119 | ++ if output: |
| 120 | ++ current_swap_size = int(output.split()[1]) * block_size |
| 121 | + |
| 122 | +- partition = '/dev/' + provider_name |
| 123 | +- logger.info('Resource disk partition {0} found.', partition) |
| 124 | ++ partition_table_type = disks.get(device) |
| 125 | + |
| 126 | +- # 3. Mount partition |
| 127 | +- mount_list = shellutil.run_get_output("mount")[1] |
| 128 | +- existing = self.osutil.get_mount_point(mount_list, partition) |
| 129 | ++ resource_provider_name = device + 'p1' |
| 130 | ++ swap_provider_name = device + 'p2' |
| 131 | + |
| 132 | ++ # re-partition if needed |
| 133 | ++ if partition_table_type != 'GPT' or current_swap_size != swap_size: |
| 134 | ++ # unmount and swapoff if needed |
| 135 | ++ mount_list = shellutil.run_get_output("mount")[1] |
| 136 | ++ existing = self.osutil.get_mount_point(mount_list, |
| 137 | ++ resource_provider_name) |
| 138 | ++ if existing: |
| 139 | ++ err, output = shellutil.run_get_output( |
| 140 | ++ "umount {0}".format(mount_point), chk_err=False) |
| 141 | ++ |
| 142 | ++ swap_info = shellutil.run_get_output("swapctl -l")[1].split('\n') |
| 143 | ++ swap_device = None |
| 144 | ++ if len(swap_info) > 2: |
| 145 | ++ swap_device = swap_info[1].split()[0] |
| 146 | ++ if swap_device: |
| 147 | ++ err, output = shellutil.run_get_output( |
| 148 | ++ "swapoff {0}".format(swap_device), chk_err=False) |
| 149 | ++ if swap_device.endswith('.eli'): |
| 150 | ++ err, output = shellutil.run_get_output( |
| 151 | ++ "geli detach {0}".format(swap_device), chk_err=False) |
| 152 | ++ |
| 153 | ++ if partition_table_type is not None: |
| 154 | ++ gaprt_destroy_cmd = "gpart destroy -F {0}".format(device) |
| 155 | ++ err, output = shellutil.run_get_output(gaprt_destroy_cmd, |
| 156 | ++ chk_err=False) |
| 157 | ++ if err: |
| 158 | ++ raise ResourceDiskError("Failed to destroy the " |
| 159 | ++ "partitioning scheme on {0}, " |
| 160 | ++ "error: {1}".format(device, output)) |
| 161 | ++ gaprt_create_cmd = "gpart create -s GPT {0}".format(device) |
| 162 | ++ err, output = shellutil.run_get_output(gaprt_create_cmd, |
| 163 | ++ chk_err=False) |
| 164 | ++ if err: |
| 165 | ++ raise ResourceDiskError("Failed to create new GPT on {0}, " |
| 166 | ++ "error: {1}".format(device, output)) |
| 167 | ++ |
| 168 | ++ mount_list = shellutil.run_get_output("mount")[1] |
| 169 | ++ existing = self.osutil.get_mount_point(mount_list, |
| 170 | ++ resource_provider_name) |
| 171 | + if existing: |
| 172 | +- logger.info("Resource disk {0} is already mounted", partition) |
| 173 | ++ logger.info("Resource disk {0} is already mounted".format( |
| 174 | ++ resource_provider_name)) |
| 175 | + return existing |
| 176 | + |
| 177 | ++ # create resource partition |
| 178 | ++ if not os.path.exists("/dev/{0}".format(resource_provider_name)): |
| 179 | ++ if swap_size > 0: |
| 180 | ++ err, output = shellutil.run_get_output( |
| 181 | ++ 'gpart add -t freebsd-ufs -s {0}b {1}'.format(resource_size, |
| 182 | ++ device)) |
| 183 | ++ else: |
| 184 | ++ err, output = shellutil.run_get_output( |
| 185 | ++ 'gpart add -t freebsd-ufs {0}'.format(device)) |
| 186 | ++ if err: |
| 187 | ++ raise ResourceDiskError( |
| 188 | ++ "Failed to add new freebsd-ufs partition to {0}, " |
| 189 | ++ "error: {1}" .format(device, output)) |
| 190 | ++ |
| 191 | ++ # create swap partition, just use all the space left |
| 192 | ++ if swap_size > 0: |
| 193 | ++ err, output = shellutil.run_get_output( |
| 194 | ++ 'gpart add -t freebsd-swap {0}'.format(device)) |
| 195 | ++ if err: |
| 196 | ++ raise ResourceDiskError( |
| 197 | ++ "Failed to add new freebsd-swap partition to {0}, " |
| 198 | ++ "error: {1}" .format(device, output)) |
| 199 | ++ |
| 200 | ++ # 3. Mount partition |
| 201 | + fileutil.mkdir(mount_point, mode=0o755) |
| 202 | +- mount_cmd = 'mount -t {0} {1} {2}'.format(fs, partition, mount_point) |
| 203 | +- err = shellutil.run(mount_cmd, chk_err=False) |
| 204 | +- if err: |
| 205 | +- logger.info( |
| 206 | +- 'Creating {0} filesystem on partition {1}'.format( |
| 207 | +- fs, partition)) |
| 208 | ++ |
| 209 | ++ need_newfs = True |
| 210 | ++ if current_swap_size == swap_size: |
| 211 | ++ # swap size is not adjusted, |
| 212 | ++ # i.e., the resource partition is not changed |
| 213 | ++ # check if a fs already exists |
| 214 | ++ fstyp_cmd = 'fstyp /dev/{0}'.format(resource_provider_name) |
| 215 | ++ err, output = shellutil.run_get_output(fstyp_cmd, chk_err=False) |
| 216 | ++ if not err and output == fs: |
| 217 | ++ need_newfs = False |
| 218 | ++ logger.info( |
| 219 | ++ "Resource disk partition {0} is found at {1} " |
| 220 | ++ "with fstype {2}".format( |
| 221 | ++ resource_provider_name, mount_point, fs)) |
| 222 | ++ elif swap_size < current_swap_size: |
| 223 | ++ # resource partition size is increased, try to growfs first |
| 224 | + err, output = shellutil.run_get_output( |
| 225 | +- 'newfs -U {0}'.format(partition)) |
| 226 | ++ 'growfs -y {0}'.format(resource_provider_name), chk_err=False) |
| 227 | ++ if not err: |
| 228 | ++ need_newfs = False |
| 229 | ++ logger.info( |
| 230 | ++ "Resource disk partition {0} is found and enlarged at {1} " |
| 231 | ++ "with fstype {2}".format( |
| 232 | ++ resource_provider_name, mount_point, fs)) |
| 233 | ++ # else |
| 234 | ++ # resource partition is shrunk and newfs is needed |
| 235 | ++ |
| 236 | ++ if need_newfs: |
| 237 | ++ logger.info('Creating {0} filesystem on partition {1}'.format( |
| 238 | ++ fs, resource_provider_name)) |
| 239 | ++ err, output = shellutil.run_get_output( |
| 240 | ++ 'newfs -U {0}'.format(resource_provider_name)) |
| 241 | + if err: |
| 242 | + raise ResourceDiskError( |
| 243 | +- "Failed to create new filesystem on partition {0}, error:{1}" .format( |
| 244 | +- partition, output)) |
| 245 | +- err, output = shellutil.run_get_output(mount_cmd, chk_err=False) |
| 246 | +- if err: |
| 247 | +- raise ResourceDiskError( |
| 248 | +- "Failed to mount partition {0}, error {1}".format( |
| 249 | +- partition, output)) |
| 250 | ++ "Failed to create new filesystem on partition {0}, " |
| 251 | ++ "error: {1}" .format(resource_provider_name, output)) |
| 252 | + |
| 253 | ++ mount_cmd = 'mount -t {0} /dev/{1} {2}'.format( |
| 254 | ++ fs, resource_provider_name, mount_point) |
| 255 | ++ err, output = shellutil.run_get_output(mount_cmd, chk_err=False) |
| 256 | ++ if err: |
| 257 | ++ raise ResourceDiskError( |
| 258 | ++ "Failed to mount partition {0}, error {1}".format( |
| 259 | ++ resource_provider_name, output)) |
| 260 | ++ |
| 261 | + logger.info( |
| 262 | +- "Resource disk partition {0} is mounted at {1} with fstype {2}", |
| 263 | +- partition, |
| 264 | +- mount_point, |
| 265 | +- fs) |
| 266 | ++ "Resource disk partition {0} is mounted at {1} " |
| 267 | ++ "with fstype {2}".format( |
| 268 | ++ resource_provider_name, mount_point, fs)) |
| 269 | + return mount_point |
| 270 | + |
| 271 | + def create_swap_space(self, mount_point, size_mb): |
| 272 | +- size_kb = size_mb * 1024 |
| 273 | +- size = size_kb * 1024 |
| 274 | +- swapfile = os.path.join(mount_point, 'swapfile') |
| 275 | +- swaplist = shellutil.run_get_output("swapctl -l")[1] |
| 276 | ++ # done in mount_resource_disk() |
| 277 | ++ pass |
| 278 | + |
| 279 | +- if self.check_existing_swap_file(swapfile, swaplist, size): |
| 280 | ++ def enable_swap(self, mount_point): |
| 281 | ++ if conf.get_resourcedisk_swap_size_mb() <=0: |
| 282 | + return |
| 283 | + |
| 284 | +- if os.path.isfile(swapfile) and os.path.getsize(swapfile) != size: |
| 285 | +- logger.info("Remove old swap file") |
| 286 | +- shellutil.run("swapoff -a", chk_err=False) |
| 287 | +- os.remove(swapfile) |
| 288 | ++ # get swap partition (geom provider) |
| 289 | ++ err, output = shellutil.run_get_output('mount') |
| 290 | ++ if err: |
| 291 | ++ raise ResourceDiskError("Unable to get mount information.") |
| 292 | ++ devices = self.parse_mount_list(output) |
| 293 | ++ resource_provider_name = devices[mount_point] |
| 294 | ++ swap_provider_name = self.get_next_partition(resource_provider_name) |
| 295 | + |
| 296 | +- if not os.path.isfile(swapfile): |
| 297 | +- logger.info("Create swap file") |
| 298 | +- self.mkfile(swapfile, size_kb * 1024) |
| 299 | ++ if conf.get_resourcedisk_enable_swap_encryption(): |
| 300 | ++ shellutil.run("kldload -n aesni") |
| 301 | ++ shellutil.run("kldload -n cryptodev") |
| 302 | ++ shellutil.run("kldload -n geom_eli") |
| 303 | ++ shellutil.run("geli onetime -e AES-XTS -l 256" |
| 304 | ++ " -d {0}".format(swap_provider_name)) |
| 305 | ++ swap_provider_name += ".eli" |
| 306 | ++ shellutil.run("chmod 0600 {0}".format(swap_provider_name)) |
| 307 | + |
| 308 | +- mddevice = shellutil.run_get_output( |
| 309 | +- "mdconfig -a -t vnode -f {0}".format(swapfile))[1].rstrip() |
| 310 | +- shellutil.run("chmod 0600 /dev/{0}".format(mddevice)) |
| 311 | ++ if shellutil.run("swapon {0}".format(swap_provider_name)): |
| 312 | ++ raise ResourceDiskError(swap_provider_name) |
| 313 | + |
| 314 | +- if conf.get_resourcedisk_enable_swap_encryption(): |
| 315 | +- shellutil.run("kldload aesni") |
| 316 | +- shellutil.run("kldload cryptodev") |
| 317 | +- shellutil.run("kldload geom_eli") |
| 318 | +- shellutil.run( |
| 319 | +- "geli onetime -e AES-XTS -l 256 -d /dev/{0}".format(mddevice)) |
| 320 | +- shellutil.run("chmod 0600 /dev/{0}.eli".format(mddevice)) |
| 321 | +- if shellutil.run("swapon /dev/{0}.eli".format(mddevice)): |
| 322 | +- raise ResourceDiskError("/dev/{0}.eli".format(mddevice)) |
| 323 | +- logger.info( |
| 324 | +- "Enabled {0}KB of swap at /dev/{1}.eli ({2})".format(size_kb, mddevice, swapfile)) |
| 325 | +- else: |
| 326 | +- if shellutil.run("swapon /dev/{0}".format(mddevice)): |
| 327 | +- raise ResourceDiskError("/dev/{0}".format(mddevice)) |
| 328 | +- logger.info( |
| 329 | +- "Enabled {0}KB of swap at /dev/{1} ({2})".format(size_kb, mddevice, swapfile)) |
| 330 | ++ size_mb = shellutil.run_get_output("swapctl -lm")[1].split()[1] |
| 331 | ++ logger.info( |
| 332 | ++ "Enabled {0}MB of swap at {1}".format(size_mb, swap_provider_name)) |
0 commit comments