3030//! [`Config::network`] reads back for `start` to render. It replaces the network options that
3131//! `nvx.py` recorded, which no reader reads: `start` refuses a configuration without it.
3232
33- use std:: ffi:: OsString ;
3433use std:: fs:: OpenOptions ;
3534use std:: io:: Write ;
36- use std:: path:: { Path , PathBuf } ;
35+ use std:: path:: Path ;
3736
3837use serde:: de:: DeserializeOwned ;
3938use serde:: { Deserialize , Deserializer , Serialize } ;
@@ -44,6 +43,7 @@ use super::{MAX_MEMORY_MIB, path_string};
4443use crate :: ErrorCode ;
4544use crate :: host:: cli:: json:: { Object , pretty_serde, read_object} ;
4645use crate :: host:: cli:: output:: LINE_SEPARATOR ;
46+ use crate :: host:: cli:: snapshots:: Name as SnapshotName ;
4747use crate :: host:: cli:: { Exit , Result , io_error, io_message} ;
4848use crate :: host:: filesystem:: ExportPlan ;
4949use crate :: host:: paths;
@@ -53,6 +53,11 @@ use crate::host::sandbox::scratch::ScratchBinding;
5353use crate :: host:: sandbox:: { SandboxLaunch , SandboxNetwork , SandboxSettings , SandboxUpper } ;
5454use crate :: protocol:: root:: RootUpper ;
5555
56+ pub ( crate ) use crate :: host:: cli:: { hex, random_bytes, temporary_path} ;
57+
58+ #[ cfg( test) ]
59+ mod tests;
60+
5661/// The configuration that earlier builds wrote for a sandbox without shares, whose `mount` is
5762/// `null`, which `start` still reads.
5863pub ( crate ) const CONFIG_FORMAT : u64 = 1 ;
@@ -69,44 +74,6 @@ pub(crate) const CONFIG_FORMATS: [u64; 3] =
6974/// What messages call a configuration.
7075const DESCRIPTION : & str = "sandbox configuration" ;
7176
72- /// Returns the name of a temporary file next to `path`: `.NAME.UUID.tmp`, with a fresh UUID4 in
73- /// hexadecimal.
74- pub ( crate ) fn temporary_path ( path : & Path ) -> Result < PathBuf > {
75- let Some ( name) = path. file_name ( ) else {
76- return Err ( Exit :: error (
77- ErrorCode :: Other ,
78- format ! ( "{} has an empty name" , path. display( ) ) ,
79- ) ) ;
80- } ;
81- let mut temporary = OsString :: from ( "." ) ;
82- temporary. push ( name) ;
83- temporary. push ( format ! ( ".{}.tmp" , uuid4_hex( ) ?) ) ;
84- Ok ( path. with_file_name ( temporary) )
85- }
86-
87- /// Returns 32 lowercase hexadecimal digits of 122 random bits, with the version and variant bits
88- /// of a version-4 UUID.
89- pub ( crate ) fn uuid4_hex ( ) -> Result < String > {
90- let mut bytes = random_bytes :: < 16 > ( ) ?;
91- bytes[ 6 ] = ( bytes[ 6 ] & 0x0f ) | 0x40 ;
92- bytes[ 8 ] = ( bytes[ 8 ] & 0x3f ) | 0x80 ;
93- Ok ( hex ( & bytes) )
94- }
95-
96- /// Returns `N` random bytes from the operating system.
97- pub ( crate ) fn random_bytes < const N : usize > ( ) -> Result < [ u8 ; N ] > {
98- let mut bytes = [ 0u8 ; N ] ;
99- getrandom:: fill ( & mut bytes) . map_err ( |error| {
100- Exit :: error ( ErrorCode :: Io , format ! ( "cannot read random bytes: {error}" ) )
101- } ) ?;
102- Ok ( bytes)
103- }
104-
105- /// Returns `bytes` in lowercase hexadecimal.
106- pub ( crate ) fn hex ( bytes : & [ u8 ] ) -> String {
107- bytes. iter ( ) . map ( |byte| format ! ( "{byte:02x}" ) ) . collect ( )
108- }
109-
11077/// Atomically replaces `path` with the JSON `text` and a line end, with the line separator of the
11178/// host for each `\n`, in a temporary file of mode 0600 that then replaces `path`.
11279pub ( crate ) fn write_json ( path : & Path , text : & str ) -> Result {
@@ -280,6 +247,9 @@ struct Record<'a> {
280247 scratch_binding : Option < & ' a ScratchBinding > ,
281248 /// The export plan of the shares, or `None` without shares.
282249 export : Option < & ' a ExportPlan > ,
250+ /// The immutable named snapshot that supplies this sandbox's restore view.
251+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
252+ source_snapshot : Option < & ' a SnapshotName > ,
283253}
284254
285255/// Returns the JSON text of the configuration that `provision` records for `launch` and the other
@@ -291,6 +261,17 @@ pub(crate) fn serialize_launch(
291261 distro : & RecordedDistro ,
292262 scratch : Option < & RecordedScratch > ,
293263 config : & ProvisionConfig < ' _ > ,
264+ ) -> Result < String > {
265+ serialize_launch_with_snapshot ( launch, distro, scratch, config, None )
266+ }
267+
268+ /// Records a restored sandbox's source alongside its own launch, without a snapshot index.
269+ pub ( crate ) fn serialize_launch_with_snapshot (
270+ launch : & SandboxLaunch ,
271+ distro : & RecordedDistro ,
272+ scratch : Option < & RecordedScratch > ,
273+ config : & ProvisionConfig < ' _ > ,
274+ source_snapshot : Option < & SnapshotName > ,
294275) -> Result < String > {
295276 let layer = & distro. layer ;
296277 let settings = launch. settings ( ) ;
@@ -314,6 +295,7 @@ pub(crate) fn serialize_launch(
314295 distro_binding : & distro. binding ,
315296 scratch_binding : scratch. map ( |scratch| & scratch. binding ) ,
316297 export : launch. export ( ) ,
298+ source_snapshot,
317299 } ;
318300 pretty_serde ( & record, DESCRIPTION )
319301}
@@ -375,9 +357,22 @@ pub(crate) struct Config {
375357 /// The sandbox's network, `null` for none, if recorded.
376358 #[ serde( default , deserialize_with = "present" ) ]
377359 network : Option < Value > ,
360+ /// The source of a restored sandbox, absent for a cold launch.
361+ source_snapshot : Option < SnapshotName > ,
378362}
379363
380364impl Config {
365+ #[ cfg_attr(
366+ not( test) ,
367+ expect(
368+ dead_code,
369+ reason = "SL3.8a locks a restored sandbox's source snapshot"
370+ )
371+ ) ]
372+ pub ( crate ) fn source_snapshot ( & self ) -> Option < & SnapshotName > {
373+ self . source_snapshot . as_ref ( )
374+ }
375+
381376 /// Returns the fields of `object`, a configuration that [`read_config`] read, which fails as
382377 /// `sandbox configuration is malformed` without a field that it requires, or with a value of
383378 /// another type.
@@ -403,6 +398,7 @@ impl Config {
403398 ) ,
404399 ) ) ;
405400 }
401+
406402 serde_json:: from_value ( Value :: Object ( object) ) . map_err ( |_| malformed ( ) )
407403 }
408404
0 commit comments