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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dstack-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ license.workspace = true
serde = { workspace = true, features = ["derive"] }
serde-human-bytes.workspace = true
sha3.workspace = true
size-parser = { workspace = true, features = ["serde"] }
3 changes: 3 additions & 0 deletions dstack-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use serde::{Deserialize, Serialize};
use serde_human_bytes as hex_bytes;
use size_parser::human_size;

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct AppCompose {
Expand Down Expand Up @@ -41,6 +42,8 @@ pub struct AppCompose {
pub secure_time: bool,
#[serde(default)]
pub storage_fs: Option<String>,
#[serde(default, with = "human_size")]
pub swap_size: u64,
}

fn default_true() -> bool {
Expand Down
83 changes: 82 additions & 1 deletion dstack-util/src/system_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
path::{Path, PathBuf},
process::Command,
str::FromStr,
time::Duration,
};

use anyhow::{anyhow, bail, Context, Result};
Expand Down Expand Up @@ -685,6 +686,85 @@ impl<'a> Stage0<'a> {
}
}

async fn setup_swap(&self, swap_size: u64, opts: &DstackOptions) -> Result<()> {
match opts.storage_fs {
FsType::Zfs => self.setup_swap_zvol(swap_size).await,
FsType::Ext4 => self.setup_swapfile(swap_size).await,
}
}

async fn setup_swapfile(&self, swap_size: u64) -> Result<()> {
let swapfile = self.args.mount_point.join("swapfile");
if swapfile.exists() {
fs::remove_file(&swapfile).context("Failed to remove swapfile")?;
info!("Removed existing swapfile");
}
if swap_size == 0 {
return Ok(());
}
let swapfile = swapfile.display().to_string();
info!("Creating swapfile at {swapfile} (size {swap_size} bytes)");
let size_str = swap_size.to_string();
cmd! {
fallocate -l $size_str $swapfile;
chmod 600 $swapfile;
mkswap $swapfile;
swapon $swapfile;
swapon --show;
}
.context("Failed to enable swap on swapfile")?;
Ok(())
}

async fn setup_swap_zvol(&self, swap_size: u64) -> Result<()> {
let swapvol_path = "dstack/swap";
let swapvol_device_path = format!("/dev/zvol/{swapvol_path}");

if Path::new(&swapvol_device_path).exists() {
cmd! {
zfs set volmode=none $swapvol_path;
zfs destroy $swapvol_path;
}
.context("Failed to destroy swap zvol")?;
}

if swap_size == 0 {
return Ok(());
}

info!("Creating swap zvol at {swapvol_device_path} (size {swap_size} bytes)");

let size_str = swap_size.to_string();
cmd! {
zfs create -V $size_str
-o compression=zle
-o logbias=throughput
-o sync=always
-o primarycache=metadata
-o com.sun:auto-snapshot=false
$swapvol_path
}
.with_context(|| format!("Failed to create swap zvol {swapvol_path}"))?;

let mut count = 0u32;
while !Path::new(&swapvol_device_path).exists() && count < 10 {
std::thread::sleep(Duration::from_secs(1));
count += 1;
}
if !Path::new(&swapvol_device_path).exists() {
bail!("Device {swapvol_device_path} did not appear after 10 seconds");
}

cmd! {
mkswap $swapvol_device_path;
swapon $swapvol_device_path;
swapon --show;
}
.context("Failed to enable swap on zvol")?;

Ok(())
}

async fn mount_data_disk(
&self,
initialized: bool,
Expand Down Expand Up @@ -958,13 +1038,14 @@ impl<'a> Stage0<'a> {
opts.storage_encrypted, opts.storage_fs
);

self.vmm.notify_q("boot.progress", "unsealing env").await;
self.mount_data_disk(
is_initialized,
&hex::encode(&app_keys.disk_crypt_key),
&opts,
)
.await?;
self.setup_swap(self.shared.app_compose.swap_size, &opts)
.await?;
self.vmm
.notify_q(
"instance.info",
Expand Down
102 changes: 99 additions & 3 deletions vmm/src/console.html
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,19 @@ <h2>Deploy a new instance</h2>
</div>
</div>

<div class="form-group">
<label for="swapSize">Swap (optional)</label>
<div style="display: flex; align-items: center; gap: 8px;">
<input id="swapSize" v-model.number="vmForm.swapValue" type="number" min="0"
step="0.1" placeholder="Swap size" style="flex: 1;">
<select v-model="vmForm.swapUnit" style="width: 80px;">
<option value="MB">MB</option>
<option value="GB">GB</option>
</select>
</div>
<small style="color: #666;">Leave as 0 to disable swap.</small>
</div>

<div class="form-group">
<label for="diskSize">Storage (GB)</label>
<input id="diskSize" v-model.number="vmForm.disk_size" type="number"
Expand Down Expand Up @@ -1009,6 +1022,10 @@ <h4 style="margin: 0 0 12px 0;">VM Configuration</h4>
<span class="detail-value">{{ formatMemory(vm.configuration?.memory)
}}</span>
</div>
<div class="detail-item" v-if="vm.configuration?.swap_size">
<span class="detail-label">Swap:</span>
<span class="detail-value">{{ formatMemory(bytesToMB(vm.configuration.swap_size)) }}</span>
</div>
<div class="detail-item">
<span class="detail-label">Disk Size:</span>
<span class="detail-value">{{ vm.configuration?.disk_size }} GB</span>
Expand Down Expand Up @@ -1136,6 +1153,21 @@ <h3>Update VM Config</h3>
</div>
</div>

<div class="form-group">
<label for="upgradeSwap">Swap (optional)</label>
<div style="display: flex; align-items: center; gap: 8px;">
<input id="upgradeSwap" v-model.number="upgradeDialog.swapValue" type="number" min="0"
step="0.1" placeholder="Swap size" style="flex: 1;"
:disabled="!upgradeDialog.updateCompose">
<select v-model="upgradeDialog.swapUnit" style="width: 80px;"
:disabled="!upgradeDialog.updateCompose">
<option value="MB">MB</option>
<option value="GB">GB</option>
</select>
</div>
<small style="color: #666;">Enable "Update compose" to change swap size.</small>
</div>

<div class="form-group">
<label for="diskSize">Disk Size (GB)</label>
<input id="diskSize" v-model.number="upgradeDialog.disk_size" type="number"
Expand Down Expand Up @@ -1527,6 +1559,9 @@ <h3>Derive VM</h3>
memory: 2048, // This will be computed from memoryValue and memoryUnit
memoryValue: 2,
memoryUnit: 'GB',
swap_size: 0,
swapValue: 0,
swapUnit: 'GB',
disk_size: 20,
selectedGpus: [],
attachAllGpus: false,
Expand Down Expand Up @@ -1566,6 +1601,9 @@ <h3>Derive VM</h3>
memory: 0, // This will be computed from memoryValue and memoryUnit
memoryValue: 0,
memoryUnit: 'MB',
swap_size: 0,
swapValue: 0,
swapUnit: 'GB',
disk_size: 0,
image: '',
ports: [],
Expand Down Expand Up @@ -1724,6 +1762,11 @@ <h3>Derive VM</h3>
app_compose.pre_launch_script = vmForm.value.preLaunchScript;
}

const swapBytes = Math.max(0, Math.round(vmForm.value.swap_size || 0));
if (swapBytes > 0) {
app_compose.swap_size = swapBytes;
}

// If APP_LAUNCH_TOKEN is set, add it's sha256 hash to the app compose file
const launchToken = vmForm.value.encryptedEnvs.find(env => env.key === 'APP_LAUNCH_TOKEN');
if (launchToken) {
Expand Down Expand Up @@ -1831,15 +1874,39 @@ <h3>Derive VM</h3>
showCreateDialog.value = true;
vmForm.value.encryptedEnvs = [];
vmForm.value.app_id = null;
vmForm.value.swapValue = 0;
vmForm.value.swapUnit = 'GB';
vmForm.value.swap_size = 0;
loadGpus();
};

// Memory conversion functions
const convertMemoryToMB = (value, unit) => {
const numericValue = Number(value);
if (!Number.isFinite(numericValue) || numericValue < 0) {
return 0;
}
if (unit === 'GB') {
return value * 1024;
return numericValue * 1024;
}
return value;
return numericValue;
};

const BYTES_PER_MB = 1024 * 1024;

const convertSwapToBytes = (value, unit) => {
const mb = convertMemoryToMB(value, unit);
if (!Number.isFinite(mb) || mb <= 0) {
return 0;
}
return Math.max(0, Math.round(mb * BYTES_PER_MB));
};

const bytesToMB = (bytes) => {
if (!bytes) {
return 0;
}
return bytes / BYTES_PER_MB;
};

const formatMemory = (memoryMB) => {
Expand All @@ -1862,6 +1929,14 @@ <h3>Derive VM</h3>
upgradeDialog.value.memory = convertMemoryToMB(upgradeDialog.value.memoryValue, upgradeDialog.value.memoryUnit);
});

watch([() => vmForm.value.swapValue, () => vmForm.value.swapUnit], () => {
vmForm.value.swap_size = convertSwapToBytes(vmForm.value.swapValue, vmForm.value.swapUnit);
});

watch([() => upgradeDialog.value.swapValue, () => upgradeDialog.value.swapUnit], () => {
upgradeDialog.value.swap_size = convertSwapToBytes(upgradeDialog.value.swapValue, upgradeDialog.value.swapUnit);
});

const createVm = async () => {
try {
// Convert memory based on selected unit
Expand All @@ -1879,6 +1954,12 @@ <h3>Derive VM</h3>
user_config: vmForm.value.user_config,
gpus: configGpu(vmForm.value),
};
const swapBytes = Math.max(0, Math.round(form.swap_size || 0));
if (swapBytes > 0) {
form.swap_size = swapBytes;
} else {
delete form.swap_size;
}
const _response = await rpcCall('CreateVm', form);
loadVMList();
showCreateDialog.value = false;
Expand Down Expand Up @@ -2054,6 +2135,10 @@ <h3>Derive VM</h3>
const selectedGpuSlots = currentGpuConfig.gpus?.map(gpu => gpu.slot) || [];
const appCompose = JSON.parse(updatedVM.configuration?.compose_file || "{}");

const swapBytes = Number(appCompose.swap_size || 0);
const swapMb = bytesToMB(swapBytes);
const swapDisplay = autoMemoryDisplay(swapMb);

upgradeDialog.value = {
show: true,
vm: updatedVM,
Expand All @@ -2066,6 +2151,9 @@ <h3>Derive VM</h3>
vcpu: updatedVM.configuration?.vcpu || 1,
memory: updatedVM.configuration?.memory || 1024,
...autoMemoryDisplay(updatedVM.configuration?.memory),
swap_size: swapBytes,
swapValue: Number(swapDisplay.memoryValue),
swapUnit: swapDisplay.memoryUnit,
disk_size: updatedVM.configuration?.disk_size || 10,
image: updatedVM.configuration?.image || '',
ports: updatedVM.configuration?.ports?.map(port => ({ ...port })) || [],
Expand Down Expand Up @@ -2100,6 +2188,13 @@ <h3>Derive VM</h3>
}
}
app_compose.pre_launch_script = upgradeDialog.value.preLaunchScript?.trim();

const swapBytes = Math.max(0, Math.round(upgradeDialog.value.swap_size || 0));
if (swapBytes > 0) {
app_compose.swap_size = swapBytes;
} else {
delete app_compose.swap_size;
}
return JSON.stringify(app_compose);
}

Expand Down Expand Up @@ -2493,6 +2588,7 @@ <h3>Derive VM</h3>
composeHashPreview,
upgradeComposeHashPreview,
formatMemory,
bytesToMB,
// Pagination and search variables
searchQuery,
currentPage,
Expand All @@ -2514,4 +2610,4 @@ <h3>Derive VM</h3>
</script>
</body>

</html>
</html>
32 changes: 16 additions & 16 deletions vmm/src/main_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,22 @@ pub fn create_manifest_from_vm_config(
None => GpuConfig::default(),
};

Ok(Manifest::builder()
.id(id)
.name(request.name.clone())
.app_id(app_id)
.image(request.image.clone())
.vcpu(request.vcpu)
.memory(request.memory)
.disk_size(request.disk_size)
.port_map(port_map)
.created_at_ms(now)
.hugepages(request.hugepages)
.pin_numa(request.pin_numa)
.gpus(gpus)
.kms_urls(request.kms_urls.clone())
.gateway_urls(request.gateway_urls.clone())
.build())
Ok(Manifest {
id,
name: request.name.clone(),
app_id,
vcpu: request.vcpu,
memory: request.memory,
disk_size: request.disk_size,
image: request.image.clone(),
port_map,
created_at_ms: now,
hugepages: request.hugepages,
pin_numa: request.pin_numa,
gpus: Some(gpus),
kms_urls: request.kms_urls.clone(),
gateway_urls: request.gateway_urls.clone(),
})
}

impl RpcHandler {
Expand Down
Loading
Loading