This folder contains the SQL schema and stored procedure scripts used by the Linux Broker for AVD Access solution.
The primary deployment path is now automated through the deployment hooks under deploy/, not manual sqlcmd execution. This document describes both paths:
- the supported automated path used by
azd up - the manual fallback path when you need to apply or verify scripts yourself
For the full deployment workflow around these SQL scripts, see ../deploy/DEPLOYMENT.md.
The supported deployment flow runs the SQL scripts automatically during postprovision.
The sequence is:
- ../deploy/Post-Provision.ps1 runs after infrastructure provisioning.
- That script calls ../deploy/Initialize-Database.ps1.
Initialize-Database.ps1loads every*.sqlfile in this folder, sorts them by filename, and applies them in order.- After the schema and procedures are in place, ../deploy/Register-LinuxHostSqlRecords.ps1 registers Linux hosts into
dbo.VirtualMachines.
The automated bootstrap has a few important behaviors:
- It connects to Azure SQL with ADO.NET from the machine running
azd up. - It splits scripts on
GObatch separators. - It rewrites
CREATE PROCEDUREandALTER PROCEDUREtoCREATE OR ALTER PROCEDUREbefore execution so reruns work cleanly. - It now fails on SQL errors instead of silently continuing.
- It can be skipped only by setting
SKIP_SQL_BOOTSTRAP=true.
Manual execution is still available when you want to inspect or repair the database outside the azd workflow.
Use that path when you need to:
- validate objects in an existing environment
- replay the scripts after a partial failure
- troubleshoot SQL connectivity or permissions
- apply the schema without running the full deployment flow
001_create_table-vm_scaling_rules.sql: createsdbo.VmScalingRules002_create_table-vm_scaling_activity_log.sql: createsdbo.VmScalingActivityLog003_create_table-virtual_machines.sql: createsdbo.VirtualMachines024_create_table-vmusers.sql: createsdbo.VmUsers026_add_lease_id_to_virtual_machines.sql: addsLeaseIdtodbo.VirtualMachinesfor lease-aware checkout and cleanup027_add_unique_index-virtual_machines_hostname.sql: enforcesHostnameuniqueness ondbo.VirtualMachines028_create_table-linux_host_settings.sql: createsdbo.LinuxHostSettingsand seeds the single global profile029_add_settings_tracking_to_virtual_machines.sql: addsSettingsVersionandSettingsAppliedDatetodbo.VirtualMachinesso settings drift is visible
The table scripts above are written to be rerunnable.
The scripts do not contain USE <database> statements. The target database comes from the connection, which ../deploy/Initialize-Database.ps1 builds from its -DatabaseName argument, so a non-default sqlDatabaseName works without editing any script.
Hostname is the natural key the broker resolves against: RegisterLinuxHostVm, ReleaseVm, and the Linux host agents all locate a VM by hostname alone. If an existing database already contains duplicate hostnames, 027 reports them and skips creating the index rather than failing the bootstrap. Remove the duplicates and rerun to gain the constraint.
005_create_procedure-CheckoutVm.sql: checks out a VM for a user006_create_procedure-DeleteVm.sql: deletes a VM record007_create_procedure-AddVm.sql: adds a VM record manually008_create_procedure-GetVmDetails.sql: gets details for a specific VM009_create_procedure-ReturnVm.sql: returns a VM to the pool010_create_procedure-GetScalingRules.sql: gets scaling rules011_create_procedure-UpdateScalingRule.sql: updates a scaling rule012_create_procedure-TriggerScalingLogic.sql: runs scaling logic013_create_procedure-GetScalingActivityLog.sql: gets scaling activity history014_create_procedure-GetVms.sql: gets the VM list015_create_procedure-CreateScalingRule.sql: creates a scaling rule016_create_procedure-ReleaseVm.sql: releases a checked-out VM017_create_procedure-UpdateVmAttributes.sql: updates VM attributes018_create_procedure-ReturnReleasedVms.sql: returns released VMs to the pool019_create_procedure-DeleteScalingRule.sql: deletes a scaling rule020_create_procedure-GetVmHistory.sql: gets VM history021_create_procedure-GetVmScalingRulesHistory.sql: gets scaling rule history022_create_procedure-GetScalingRuleDetails.sql: gets a specific scaling rule023_create_procedure-GetDeletedVirtualMachines.sql: gets deleted VM history025_create_procedure-RegisterLinuxHostVm.sql: upserts Linux host records intodbo.VirtualMachines030_create_procedure-GetLinuxHostSettings.sql: reads the global Linux host settings profile031_create_procedure-UpdateLinuxHostSettings.sql: updates the profile, bumpingSettingsVersiononly when a value actually changed032_create_procedure-RecordHostSettingsApplied.sql: records the settings version a host has applied033_alter_procedure-GetVms.sql: redefinesdbo.GetVmsto also returnSettingsVersionandSettingsAppliedDate034_create_procedure-GetVmSummary.sql: returns one aggregate row for dashboard VM counters035_alter_procedure-GetScalingActivityLog.sql: redefinesdbo.GetScalingActivityLogto parse optionalMM/DD/YYYYdate strings explicitly036_alter_procedure-GetVmScalingRulesHistory.sql: redefinesdbo.GetVmScalingRulesHistoryto parse optionalMM/DD/YYYYdate strings explicitly037_create_procedure-GetVmHistoryPaged.sql: returns paged VM history rows withTotalCount038_create_procedure-GetScalingActivityLogPaged.sql: returns paged scaling activity rows withTotalCount039_create_procedure-GetVmScalingRulesHistoryPaged.sql: returns paged scaling rule history rows withTotalCount
033 exists as its own file rather than being folded into 014 because 014 runs before 029 adds those columns, and SQL Server validates column references against existing tables when a procedure is created.
034 through 039 are also additive/redefinition files so fresh deployments keep procedure validation in numeric schema order. The paged history procedures intentionally omit the legacy @Limit parameter: @Offset and @PageSize are the only result-size controls, and NULL/empty/malformed date strings are treated as no date filter.
The current code and deployment flow depend on the following SQL objects being present:
dbo.VmScalingRulesdbo.VmScalingActivityLogdbo.VirtualMachinesdbo.VmUsersdbo.LinuxHostSettings- all of the stored procedures above
- especially
dbo.CheckoutVm,dbo.ReleaseVm,dbo.UpdateVmAttributes, anddbo.RegisterLinuxHostVm
Two current behaviors are worth calling out:
dbo.VmUsersis required by the API path that creates and tracks Linux-side user IDs.dbo.RegisterLinuxHostVmis the procedure used by post-provision automation to register Linux hosts automatically.
Linux host settings are a single fleet-wide profile:
dbo.LinuxHostSettingsis a singleton.SettingsScopeis constrained toGlobaland made unique, so only one active profile can exist.- The table is seeded with the values that were previously hardcoded in the release agent and the systemd units, so applying the schema changes no behavior.
- The
CHECKconstraints on that table are the last line of defence for values that reach the Linux hosts. The API andlinux_host/apply-host-settings.shvalidate the same bounds, and all three definitions must be kept in agreement. dbo.VirtualMachines.SettingsVersionandSettingsAppliedDaterecord what each host actually applied, which is what the portal uses to display drift.
The VM checkout lifecycle is now lease-aware:
dbo.CheckoutVmreuses an existingCheckedOutorReleasedassignment byUsernameand keeps the sameLeaseIduntil the VM is returned toAvailable.dbo.ReleaseVmcan validateHostname,Username, andLeaseIdtogether while still tolerating older hostname-only callers during rollout. It always returns aReleaseStatuscolumn ofReleased,NoActiveAssignment,LeaseMismatch, orNotFoundso the API can answer an already-released host with200instead of an error that the host agent would retry every minute.dbo.ReturnVmanddbo.ReturnReleasedVmsnow preserve the returned username and lease metadata long enough for the API to perform lease-safe Linux-side cleanup.dbo.ReturnReleasedVmsexpires released leases with a single set-basedUPDATE ... OUTPUT, so the sweep is atomic and does not depend onINSERT ... EXEC.
After the SQL scripts are applied, ../deploy/Register-LinuxHostSqlRecords.ps1 connects to Azure and SQL and runs dbo.RegisterLinuxHostVm for every VM tagged with broker-role=linux-host.
That automation:
- only registers Linux hosts
- does not register AVD hosts
- uses the VM name and resolved private IP address
- inserts a new record if the host is missing
- updates the existing record if the host already exists
This means future azd deployments no longer depend on a manual UI step just to seed Linux hosts into the database.
If you need to run the SQL setup manually, use the following flow.
- an Azure SQL Database instance already exists
- you can connect with an admin or equivalent SQL principal
- the client machine is allowed through the SQL firewall
When using the azd deployment flow, remember that SQL bootstrap runs from the local machine. If the SQL firewall does not allow that client IP, the automated bootstrap will fail.
Run all scripts in filename order.
That means:
- Run the table scripts.
- Run the stored procedure scripts.
- Verify the objects.
- Optionally register Linux hosts by executing
dbo.RegisterLinuxHostVmyourself or rerunning the post-provision script.
$server = "your_server.database.windows.net"
$database = "LinuxBroker"
$username = "your_username"
$password = "your_password"
Get-ChildItem -Path .\sql_queries -Filter *.sql |
Sort-Object Name |
ForEach-Object {
Write-Host "Applying $($_.Name)"
sqlcmd -S $server -d $database -U $username -P $password -i $_.FullName
}Manual execution is useful, but it does not automatically perform the newer post-provision Linux host registration unless you run that step separately.
After bootstrap, verify both tables and procedures.
SELECT name
FROM sys.tables
WHERE name IN ('VmScalingRules', 'VmScalingActivityLog', 'VirtualMachines', 'VmUsers', 'LinuxHostSettings')
ORDER BY name;SELECT name
FROM sys.procedures
WHERE name IN (
'CheckoutVm',
'DeleteVm',
'AddVm',
'GetVmDetails',
'ReturnVm',
'GetScalingRules',
'UpdateScalingRule',
'TriggerScalingLogic',
'GetScalingActivityLog',
'GetVms',
'CreateScalingRule',
'ReleaseVm',
'UpdateVmAttributes',
'ReturnReleasedVms',
'DeleteScalingRule',
'GetVmHistory',
'GetVmScalingRulesHistory',
'GetScalingRuleDetails',
'GetDeletedVirtualMachines',
'RegisterLinuxHostVm',
'GetLinuxHostSettings',
'UpdateLinuxHostSettings',
'RecordHostSettingsApplied',
'GetVmSummary',
'GetVmHistoryPaged',
'GetScalingActivityLogPaged',
'GetVmScalingRulesHistoryPaged'
)
ORDER BY name;SELECT Hostname, IPAddress, PowerState, NetworkStatus, VmStatus, LastUpdateDate
FROM dbo.VirtualMachines
ORDER BY Hostname;If the database bootstrap needs to be rerun, the preferred path is to rerun the deployment script rather than manually replaying only a subset of files.
From the deploy/ directory:
.\Initialize-Database.ps1 `
-SqlServerFqdn <server>.database.windows.net `
-DatabaseName LinuxBroker `
-SqlAdminLogin <login> `
-SqlAdminPassword <password> `
-ScriptsPath ..\sql_queriesIf you also want to refresh Linux host records after the schema run:
.\Register-LinuxHostSqlRecords.ps1 `
-ResourceGroupName <resource-group> `
-SqlServerFqdn <server>.database.windows.net `
-DatabaseName LinuxBroker `
-SqlAdminLogin <login> `
-SqlAdminPassword <password>Common causes:
- the local client IP is not allowed through the SQL firewall
- the SQL admin credentials are wrong
- an earlier script failed and blocked a later dependency
The automated bootstrap now stops at the first SQL error, so the failing file name and batch number are the first place to look.
This table is now part of the supported schema and is required by the API user-creation path. Rerun the bootstrap or apply 024_create_table-vmusers.sql manually.
Rerun ../deploy/Register-LinuxHostSqlRecords.ps1, or rerun ../deploy/Post-Provision.ps1 if you want the full post-provision sequence.
Keep the change in the numbered SQL file in source control, then rerun the bootstrap. The deployment script converts procedure creation statements into CREATE OR ALTER PROCEDURE, so reruns are supported.
Treat this folder as the source of truth for the broker database schema and procedure layer.
For new environments:
- let
azd updrive the SQL bootstrap automatically - use the deployment scripts under
deploy/to rerun or troubleshoot - expect Linux hosts to be auto-registered into SQL after bootstrap
For manual intervention:
- execute the scripts in filename order
- verify
VmUsersandRegisterLinuxHostVmin addition to the older objects - rerun the deployment scripts when you want behavior that matches the supported automated path