Modify configuration backends to use readFromBoot

This ensures all reads from the boot partition are made using fatrw
This commit is contained in:
Felipe Lalanne 2022-06-16 13:40:48 -04:00
parent c6f5af1fd9
commit 675bfc8873
7 changed files with 21 additions and 18 deletions

View File

@ -103,7 +103,10 @@ export class ConfigFs extends ConfigBackend {
// read the config file... // read the config file...
try { try {
const content = await fs.readFile(this.ConfigFilePath, 'utf8'); const content = await hostUtils.readFromBoot(
this.ConfigFilePath,
'utf-8',
);
return JSON.parse(content); return JSON.parse(content);
} catch (err) { } catch (err) {
log.error('Unable to deserialise ConfigFS configuration.', err); log.error('Unable to deserialise ConfigFS configuration.', err);

View File

@ -1,5 +1,4 @@
import * as _ from 'lodash'; import * as _ from 'lodash';
import { promises as fs } from 'fs';
import { ConfigOptions, ConfigBackend } from './backend'; import { ConfigOptions, ConfigBackend } from './backend';
import * as constants from '../../lib/constants'; import * as constants from '../../lib/constants';
@ -64,7 +63,10 @@ export class ConfigTxt extends ConfigBackend {
let configContents = ''; let configContents = '';
if (await exists(ConfigTxt.bootConfigPath)) { if (await exists(ConfigTxt.bootConfigPath)) {
configContents = await fs.readFile(ConfigTxt.bootConfigPath, 'utf-8'); configContents = await hostUtils.readFromBoot(
ConfigTxt.bootConfigPath,
'utf-8',
);
} else { } else {
await hostUtils.writeToBoot(ConfigTxt.bootConfigPath, ''); await hostUtils.writeToBoot(ConfigTxt.bootConfigPath, '');
} }

View File

@ -59,7 +59,10 @@ export class Extlinux extends ConfigBackend {
let confContents: string; let confContents: string;
try { try {
confContents = await fs.readFile(Extlinux.bootConfigPath, 'utf-8'); confContents = await hostUtils.readFromBoot(
Extlinux.bootConfigPath,
'utf-8',
);
} catch { } catch {
// In the rare case where the user might have deleted extlinux conf file between linux boot and supervisor boot // In the rare case where the user might have deleted extlinux conf file between linux boot and supervisor boot
// We do not have any backup to fallback too; warn the user of a possible brick // We do not have any backup to fallback too; warn the user of a possible brick

View File

@ -1,5 +1,4 @@
import * as _ from 'lodash'; import * as _ from 'lodash';
import { promises as fs } from 'fs';
import { ConfigOptions, ConfigBackend } from './backend'; import { ConfigOptions, ConfigBackend } from './backend';
import * as constants from '../../lib/constants'; import * as constants from '../../lib/constants';
@ -195,7 +194,7 @@ export class ExtraUEnv extends ConfigBackend {
private static async readBootConfigPath(): Promise<string> { private static async readBootConfigPath(): Promise<string> {
try { try {
return await fs.readFile(ExtraUEnv.bootConfigPath, 'utf-8'); return await hostUtils.readFromBoot(ExtraUEnv.bootConfigPath, 'utf-8');
} catch { } catch {
// In the rare case where the user might have deleted extra_uEnv conf file between linux boot and supervisor boot // In the rare case where the user might have deleted extra_uEnv conf file between linux boot and supervisor boot
// We do not have any backup to fallback too; warn the user of a possible brick // We do not have any backup to fallback too; warn the user of a possible brick

View File

@ -71,7 +71,7 @@ export class SplashImage extends ConfigBackend {
where = where ?? (await this.getSplashPath()); where = where ?? (await this.getSplashPath());
// read the image file... // read the image file...
return (await fs.readFile(where)).toString('base64'); return (await hostUtils.readFromBoot(where)).toString('base64');
} }
// Write a splash image provided as a base64 string // Write a splash image provided as a base64 string

View File

@ -1,10 +1,9 @@
import * as Bluebird from 'bluebird'; import * as Bluebird from 'bluebird';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { promises as fs } from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as constants from '../lib/constants'; import * as constants from '../lib/constants';
import { writeAndSyncFile } from '../lib/fs-utils'; import * as hostUtils from '../lib/host-utils';
import * as osRelease from '../lib/os-release'; import * as osRelease from '../lib/os-release';
import log from '../lib/supervisor-console'; import log from '../lib/supervisor-console';
@ -87,10 +86,8 @@ export default class ConfigJsonConfigBackend {
} }
private async write(): Promise<void> { private async write(): Promise<void> {
// We use writeAndSyncFile since /mnt/boot partition is a vfat // writeToBoot uses fatrw to safely write to the boot partition
// filesystem which dows not provide atomic file renames. The best return hostUtils.writeToBoot(
// course of action on that case is to write and sync as soon as possible
return writeAndSyncFile(
await this.pathOnHost(), await this.pathOnHost(),
JSON.stringify(this.cache), JSON.stringify(this.cache),
); );
@ -98,7 +95,7 @@ export default class ConfigJsonConfigBackend {
private async read(): Promise<string> { private async read(): Promise<string> {
const filename = await this.pathOnHost(); const filename = await this.pathOnHost();
return JSON.parse(await fs.readFile(filename, 'utf-8')); return JSON.parse(await hostUtils.readFromBoot(filename, 'utf-8'));
} }
private async resolveConfigPath(): Promise<string> { private async resolveConfigPath(): Promise<string> {

View File

@ -10,7 +10,7 @@ import * as constants from './lib/constants';
import * as dbus from './lib/dbus'; import * as dbus from './lib/dbus';
import { ENOENT, InternalInconsistencyError } from './lib/errors'; import { ENOENT, InternalInconsistencyError } from './lib/errors';
import { mkdirp, unlinkAll } from './lib/fs-utils'; import { mkdirp, unlinkAll } from './lib/fs-utils';
import { writeToBoot } from './lib/host-utils'; import { writeToBoot, readFromBoot } from './lib/host-utils';
import * as updateLock from './lib/update-lock'; import * as updateLock from './lib/update-lock';
const redsocksHeader = stripIndent` const redsocksHeader = stripIndent`
@ -66,7 +66,7 @@ async function readProxy(): Promise<ProxyConfig | undefined> {
const conf: ProxyConfig = {}; const conf: ProxyConfig = {};
let redsocksConf: string; let redsocksConf: string;
try { try {
redsocksConf = await fs.readFile(redsocksConfPath, 'utf-8'); redsocksConf = await readFromBoot(redsocksConfPath, 'utf-8');
} catch (e) { } catch (e) {
if (!ENOENT(e)) { if (!ENOENT(e)) {
throw e; throw e;
@ -91,8 +91,7 @@ async function readProxy(): Promise<ProxyConfig | undefined> {
} }
try { try {
const noProxy = await fs const noProxy = await readFromBoot(noProxyPath, 'utf-8')
.readFile(noProxyPath, 'utf-8')
// Prevent empty newline from being reported as a noProxy address // Prevent empty newline from being reported as a noProxy address
.then((addrs) => addrs.split('\n').filter((addr) => addr !== '')); .then((addrs) => addrs.split('\n').filter((addr) => addr !== ''));