Skip to content

Commit

Permalink
Detect ceph volume device name from rbd
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie committed Sep 21, 2023
1 parent 699874c commit 945eb5c
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/utils/mounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export async function ensureMounted(
console.log(`Ensuring ${device} is mounted at ${path}`)

if (cephVolume) {
await attachCeph(cephVolume)
device = await attachCeph(cephVolume)
console.log(`Mapped ${cephVolume.volumeName} to ${device}`)
}

await waitForDevice(device)
Expand Down Expand Up @@ -49,15 +50,18 @@ export async function ensureMounted(
await mountDevice(realDevice, path, fstype, options)
}

async function attachCeph(cephVolume: RegisterMachineResponse_Mount_CephVolume) {
async function attachCeph(cephVolume: RegisterMachineResponse_Mount_CephVolume): Promise<string> {
const {volumeName, clientName, cephConf, key} = cephVolume
console.log(`Installing ceph configuration for ${clientName}`)
await writeCephConf(clientName, cephConf, key)

try {
await fsp.stat('/dev/rbd0')
console.log(`RBD device already exists`)
return
const device = await getCephDeviceName(cephVolume)
if (device) {
await fsp.stat(device)
console.log(`RBD device ${device} already exists`)
return device
}
} catch {}

console.log(`Attaching ceph ${volumeName} for ${clientName}`)
Expand All @@ -69,6 +73,23 @@ async function attachCeph(cephVolume: RegisterMachineResponse_Mount_CephVolume)
const keyringPath = `/etc/ceph/ceph.${clientName}.keyring`
await execa('rbd', ['map', imageSpec, '--name', clientName, '--keyring', keyringPath], {stdio: 'inherit'})
console.log(`Mapped ${imageSpec}`)
const device = await getCephDeviceName(cephVolume)
if (!device) throw new Error(`Failed to map ${imageSpec}, could not find device name`)
return device
}

interface CephDevice {
pool: string
namespace: string
name: string
device: string
}

async function getCephDeviceName(cephVolume: RegisterMachineResponse_Mount_CephVolume): Promise<string | null> {
const {stdout} = await execa('rbd', ['device', 'list', '--format', 'json'])
const devices: CephDevice[] = JSON.parse(stdout)
const device = devices.find((d) => d.name === cephVolume.volumeName)
return device?.device ?? null
}

async function mountDevice(
Expand Down

0 comments on commit 945eb5c

Please sign in to comment.