Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ jobs:
sqlserver: sql-2019
variant: no-updates
install-updates: 'false'
# choosing the directories turns off the move away from a volume with large sectors, and checks the setup
# switches that the move uses on every run, not only on the runners that need it
- os: windows-2022
sqlserver: sql-2022
variant: custom-dirs
install-arguments: |
/INSTANCEDIR="D:\SQL Server"
/INSTALLSQLDATADIR="D:\SQL Server"
data-root: 'D:\SQL Server\'
name: runaction (${{ matrix.os }}, ${{ matrix.sqlserver }}${{ matrix.variant && format(', {0}', matrix.variant) || '' }})
runs-on: ${{ matrix.os }}
timeout-minutes: 30
Expand Down Expand Up @@ -205,6 +214,7 @@ jobs:
odbc-version: ${{ matrix.odbc-version || '17' }}
install-updates: ${{ matrix.install-updates || 'true' }}
wait-for-ready: ${{ matrix.wait-for-ready || 'true' }}
install-arguments: ${{ matrix.install-arguments || '' }}
# the action only warns when the database isn't ready, so check that the instance it reports really works:
# log in over TCP with its sa-password output and compare what the server reports with what was asked for
- name: Check the installed instance
Expand All @@ -216,6 +226,7 @@ jobs:
SQLSERVER_VERSION: ${{ matrix.sqlserver }}
ODBC_VERSION: ${{ matrix.odbc-version || '17' }}
INSTALL_UPDATES: ${{ matrix.install-updates || 'true' }}
DATA_ROOT: ${{ matrix.data-root || '' }}
run: |
# handle failures here instead of stopping at the first error, so every check reports
$ErrorActionPreference = 'Continue'
Expand All @@ -224,7 +235,7 @@ jobs:
Write-Host "::error::The instance-name output is '$env:INSTANCE_NAME', expected 'MSSQLSERVER'"
$failed = $true
}
$query = "SET NOCOUNT ON; SELECT CAST(SERVERPROPERTY('ProductMajorVersion') AS nvarchar(128)), CAST(SERVERPROPERTY('ProductLevel') AS nvarchar(128)), ISNULL(CAST(SERVERPROPERTY('ProductUpdateLevel') AS nvarchar(128)), ''), CAST(SERVERPROPERTY('Collation') AS nvarchar(128))"
$query = "SET NOCOUNT ON; SELECT CAST(SERVERPROPERTY('ProductMajorVersion') AS nvarchar(128)), CAST(SERVERPROPERTY('ProductLevel') AS nvarchar(128)), ISNULL(CAST(SERVERPROPERTY('ProductUpdateLevel') AS nvarchar(128)), ''), CAST(SERVERPROPERTY('Collation') AS nvarchar(128)), CAST(SERVERPROPERTY('InstanceDefaultDataPath') AS nvarchar(260))"
$row = $null
for ($attempt = 1; $attempt -le 12; $attempt++) {
$output = sqlcmd -S 'tcp:127.0.0.1,1433' -U sa -b -l 5 -h -1 -W -s '|' -Q $query 2>&1
Expand All @@ -239,8 +250,12 @@ jobs:
Write-Host "::error::Could not log in as sa over TCP with the sa-password output"
exit 1
}
$major, $level, $updateLevel, $collation = "$row".Trim().Split('|')
Write-Host "SQL Server major version $major, level $level, update level '$updateLevel', collation $collation"
$major, $level, $updateLevel, $collation, $dataPath = "$row".Trim().Split('|')
Write-Host "SQL Server major version $major, level $level, update level '$updateLevel', collation $collation, data path $dataPath"
if ($env:DATA_ROOT -and -not $dataPath.StartsWith($env:DATA_ROOT, [StringComparison]::OrdinalIgnoreCase)) {
Write-Host "::error::SQL Server reports data path $dataPath, expected a path in $env:DATA_ROOT"
$failed = $true
}
$expectedMajor = @{ 'sql-latest' = '17'; 'sql-2025' = '17'; 'sql-2022' = '16'; 'sql-2019' = '15'; 'sql-2017' = '14'; 'sql-2016' = '13' }[$env:SQLSERVER_VERSION]
if ($major -ne $expectedMajor) {
Write-Host "::error::SQL Server reports major version $major, expected $expectedMajor for $env:SQLSERVER_VERSION"
Expand Down Expand Up @@ -278,6 +293,8 @@ jobs:
path: |
C:/Program Files/Microsoft SQL Server/*/Setup Bootstrap/Log/
C:/Program Files/Microsoft SQL Server/MSSQL*/MSSQL/Log/
D:/Microsoft SQL Server/MSSQL*/MSSQL/Log/
D:/SQL Server/MSSQL*/MSSQL/Log/
if-no-files-found: ignore
release:
name: Release
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ logs a warning and carries on without rebooting. With `wait-for-ready` enabled,
the action then fails if the instance doesn't respond to queries, rather than
only warning as it does after a normal install.

### Disks with large sectors

SQL Server can't start from a volume whose physical sector size is over 4 KB.
Some GitHub-hosted `windows-2022` runners have a C: drive that reports 8 KB
sectors, so SQL Server setup fails there with its default directories. Before
setup, the action reads the sector size of the Program Files volume with
`fsutil fsinfo sectorinfo`. If it is over 4 KB, and the volume of the runner's
temporary directory (D: on GitHub-hosted runners) reports 4 KB or less, the
action installs the instance in `D:\Microsoft SQL Server` and logs a warning.
The system databases, user databases, logs and backups then go to
`D:\Microsoft SQL Server\MSSQLnn.MSSQLSERVER\MSSQL\`, so use
`SERVERPROPERTY('InstanceDefaultDataPath')` rather than a fixed path to find
them. If no such volume exists, the action logs a warning and runs setup with
the default directories.

To choose the directories yourself, set `/INSTANCEDIR` and `/INSTALLSQLDATADIR`
in `install-arguments`. If either one is set, the action doesn't change the
directories. Both must be on a volume with sectors of 4 KB or less.

### Basic usage

```yml
Expand Down
6 changes: 3 additions & 3 deletions lib/main/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/main/index.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
downloadExeInstaller,
downloadSseiInstaller,
downloadUpdateInstaller,
findInstanceDirArgs,
gatherErrorLogs,
gatherInputs,
gatherSummaryFiles,
Expand Down Expand Up @@ -148,6 +149,8 @@ export default async function install() {
}
}
const instanceName = 'MSSQLSERVER';
// move the instance off the default volume if SQL Server can't use it
const instanceDirArgs = await findInstanceDirArgs(installArgs);
try {
// @todo - make sure that the arguments are unique / don't conflict
const exitCode = await core.group('Installing SQL Server', () => exec.exec(`"${toolPath}"`, [
Expand All @@ -163,6 +166,7 @@ export default async function install() {
'/SECURITYMODE=SQL',
`/SAPWD="${password}"`,
'/IACCEPTSQLSERVERLICENSETERMS',
...instanceDirArgs,
...(config.installArgs ?? []),
...installArgs,
], {
Expand Down
78 changes: 78 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,81 @@ async function queryRegistry(key: string): Promise<Map<string, string>> {
}
return values;
}

/**
* The largest physical sector size, in bytes, of a volume that SQL Server can use.
*
* See: https://learn.microsoft.com/en-us/troubleshoot/sql/database-engine/database-file-operations/troubleshoot-os-4kb-disk-sector-size
*/
const MAX_SECTOR_SIZE = 4096;

/**
* Get the drive of a Windows path, eg: "C:" for "C:\Program Files".
*
* @param {string | undefined} path
* @returns {string | undefined} The drive in upper case, undefined if the path doesn't start with one
*/
function driveOf(path: string | undefined): string | undefined {
return path?.match(/^([a-z]:)\\/i)?.[1].toUpperCase();
}

/**
* Read the physical sector size of a volume: the larger of the two physical values that fsutil reports, which is
* the value that Microsoft says to compare with 4 KB.
*
* @param {string} drive eg: "C:"
* @returns {Promise<number | undefined>} The size in bytes, undefined if it can't be read
*/
async function getPhysicalSectorSize(drive: string): Promise<number | undefined> {
try {
const { exitCode, stdout } = await exec.getExecOutput('fsutil', ['fsinfo', 'sectorinfo', drive], {
ignoreReturnCode: true,
silent: true,
});
const sizes = [...stdout.matchAll(/^\s*PhysicalBytesPerSectorFor(?:Atomicity|Performance)\s*:\s*(\d+)/gm)]
.map(([, size]) => Number(size));
if (sizes.length) {
core.debug(`Physical sector size of ${drive} is ${Math.max(...sizes)} bytes`);
return Math.max(...sizes);
}
core.debug(`Unable to read the sector size of ${drive}: fsutil exited with code ${exitCode} and printed no physical sector sizes`);
} catch (error) {
const reason = error instanceof Error ? error.message : String(error);
core.debug(`Unable to read the sector size of ${drive}: ${reason}`);
}
return undefined;
}

/**
* Find setup arguments that move the instance off a volume that SQL Server can't use.
*
* SQL Server can't start from a volume whose physical sector size is over 4 KB, and the OS disk of some
* GitHub-hosted runners reports 8 KB. Microsoft's fix is a registry value that needs a reboot, so instead install
* the instance on the volume of the runner's temporary directory. Both directories must move: the data directory
* holds the system databases, and the instance directory holds the resource database.
*
* @param {string[]} installArgs The setup arguments from the install-arguments input
* @returns {Promise<string[]>} The setup arguments to add, empty if none are needed
*/
export async function findInstanceDirArgs(installArgs: string[]): Promise<string[]> {
// the user has chosen where the instance goes
if (installArgs.some((arg) => /^[/-](INSTANCEDIR|INSTALLSQLDATADIR)=/i.test(arg))) {
return [];
}
// setup installs the instance in the Program Files directory by default
const defaultDrive = driveOf(process.env.ProgramFiles) ?? 'C:';
const defaultSize = await getPhysicalSectorSize(defaultDrive);
if (!defaultSize || defaultSize <= MAX_SECTOR_SIZE) {
return [];
}
const problem = `${defaultDrive} reports a physical sector size of ${defaultSize} bytes, which SQL Server can't use.`;
const tempDrive = driveOf(process.env.RUNNER_TEMP);
const tempSize = tempDrive && tempDrive !== defaultDrive ? await getPhysicalSectorSize(tempDrive) : undefined;
if (!tempDrive || !tempSize || tempSize > MAX_SECTOR_SIZE) {
core.warning(`${problem} No other volume with a supported sector size was found, so setup is likely to fail. See https://learn.microsoft.com/en-us/troubleshoot/sql/database-engine/database-file-operations/troubleshoot-os-4kb-disk-sector-size`);
return [];
}
const directory = `${tempDrive}\\Microsoft SQL Server`;
core.warning(`${problem} Installing the instance in ${directory} instead. To choose the directory yourself, set /INSTANCEDIR and /INSTALLSQLDATADIR in install-arguments.`);
return [`/INSTANCEDIR="${directory}"`, `/INSTALLSQLDATADIR="${directory}"`];
}
22 changes: 21 additions & 1 deletion test/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const utils = {
downloadBoxInstaller: mock.fn(async () => 'C:\\tmp\\box\\setup.exe'),
downloadSseiInstaller: mock.fn(async () => 'C:\\tmp\\ssei\\setup.exe'),
downloadUpdateInstaller: mock.fn(async () => 'C:\\tmp\\exe\\sqlupdate.exe'),
findInstanceDirArgs: mock.fn(async () => [] as string[]),
waitForDatabase: mock.fn(async () => 0),
downloadTool: mock.fn(async () => ''),
};
Expand Down Expand Up @@ -72,7 +73,7 @@ function resetAllMocks() {
installNativeClient, installOdbc,
utils.gatherInputs, utils.getOsVersion, utils.gatherSummaryFiles, utils.gatherErrorLogs,
utils.downloadExeInstaller, utils.downloadBoxInstaller,
utils.downloadSseiInstaller, utils.downloadUpdateInstaller,
utils.downloadSseiInstaller, utils.downloadUpdateInstaller, utils.findInstanceDirArgs,
utils.waitForDatabase, utils.downloadTool,
];
for (const fn of fns) {
Expand All @@ -97,6 +98,7 @@ describe('install', () => {
utils.downloadBoxInstaller.mock.mockImplementation(async () => 'C:\\tmp\\box\\setup.exe');
utils.downloadSseiInstaller.mock.mockImplementation(async () => 'C:\\tmp\\ssei\\setup.exe');
utils.downloadUpdateInstaller.mock.mockImplementation(async () => 'C:\\tmp\\exe\\sqlupdate.exe');
utils.findInstanceDirArgs.mock.mockImplementation(async () => []);
utils.waitForDatabase.mock.mockImplementation(async () => 0);

VERSIONS.clear();
Expand Down Expand Up @@ -193,6 +195,24 @@ describe('install', () => {
]);
});
}
it('passes the instance directory arguments before the version and user arguments', async () => {
VERSIONS.set('box', { ...VERSIONS.get('box')!, installArgs: ['/ENABLERANU=1'] });
utils.gatherInputs.mock.mockImplementation(() => defaultInputs({ installArgs: ['/SQLTEMPDBFILECOUNT=4'] }));
utils.findInstanceDirArgs.mock.mockImplementation(async () => [
'/INSTANCEDIR="D:\\Microsoft SQL Server"',
'/INSTALLSQLDATADIR="D:\\Microsoft SQL Server"',
]);
await install();
assert.equal(utils.findInstanceDirArgs.mock.callCount(), 1);
assert.deepEqual(utils.findInstanceDirArgs.mock.calls[0].arguments, [['/SQLTEMPDBFILECOUNT=4']]);
assert.deepEqual(exec.exec.mock.calls[0].arguments[1].slice(-5), [
'/IACCEPTSQLSERVERLICENSETERMS',
'/INSTANCEDIR="D:\\Microsoft SQL Server"',
'/INSTALLSQLDATADIR="D:\\Microsoft SQL Server"',
'/ENABLERANU=1',
'/SQLTEMPDBFILECOUNT=4',
]);
});
it('warns and waits for the database if setup requests a reboot', async () => {
exec.exec.mock.mockImplementation(async () => 3010);
await install();
Expand Down
Loading
Loading