emu_cli/
command.rs

1use clap::{Parser, Subcommand};
2use std::path::PathBuf;
3
4#[derive(Debug, Parser, Clone)]
5#[command(author, version, about, long_about=None)]
6pub struct Commands {
7    #[command(subcommand)]
8    pub command: CommandType,
9}
10
11#[derive(Debug, Subcommand, Clone)]
12pub enum CommandType {
13    /// Create vm with a sized image, or append storage to an existing VM
14    Create {
15        /// Append this VM image to an existing VM?
16        #[arg(short, long, default_value = "false")]
17        append: bool,
18        /// Name of VM
19        name: String,
20        /// Size in GB of VM image
21        size: usize,
22    },
23    /// Delete existing vm
24    Delete {
25        /// Name of VM
26        name: String,
27        /// (Optional) ID of disk to remove
28        disk: Option<String>,
29    },
30    /// Rename VM to new name
31    Rename {
32        /// Original name of VM
33        old: String,
34        /// New name of VM
35        new: String,
36    },
37    /// List all disks a VM has available
38    ListDisks {
39        /// Name of VM
40        name: String,
41    },
42    /// Open standard input to a port on the VM
43    NC {
44        /// Name of VM
45        name: String,
46        /// Port of VM
47        port: u16,
48    },
49    /// Uses ssh_port configuration variable to SSH into the host
50    SSH {
51        /// Name of VM
52        name: String,
53        /// Arguments to pass to `ssh` program (use -- to stop CLI processing)
54        args: Option<Vec<String>>,
55    },
56    /// Configure supervision of an already existing VM
57    Supervise {
58        /// ISO of CD-ROM image -- will be embedded into supervision
59        #[arg(short = 'c')]
60        cdrom: Option<PathBuf>,
61        /// Name of VM
62        name: String,
63    },
64    /// Remove supervision of an already existing vm
65    Unsupervise {
66        /// Name of VM
67        name: String,
68    },
69    /// Just run a pre-created VM; no systemd involved
70    Run {
71        /// Run without a video window
72        #[arg(short = 'e', long, default_value = "false")]
73        headless: bool,
74        /// Do not wait for qemu to exit
75        #[arg(short, long, default_value = "false")]
76        detach: bool,
77        /// ISO of CD-ROM image
78        #[arg(short, long)]
79        cdrom: Option<PathBuf>,
80        /// Supply an extra ISO image (useful for windows installations)
81        #[arg(long = "extra")]
82        extra_disk: Option<PathBuf>,
83        /// Name of VM
84        name: String,
85    },
86    /// Gracefully shutdown a pre-created VM
87    Shutdown {
88        /// Do not wait for the VM to terminate
89        #[arg(short, long, default_value = "false")]
90        nowait: bool,
91        /// Name of VM
92        name: String,
93    },
94    /// Issue QMP commands to the guest
95    QMP {
96        /// Name of VM
97        name: String,
98        /// Command to issue
99        command: String,
100        /// Arguments to send for command, JSON literal in single argument
101        arguments: Option<String>,
102    },
103    /// Yield a list of VMs, one on each line
104    List {
105        /// List only currently running VMs
106        #[arg(short, long, default_value = "false")]
107        running: bool,
108    },
109    /// Yield a list of supervised VMs, one on each line
110    Supervised,
111    /// Clone one VM to another
112    Clone {
113        /// Copy configuration as well
114        #[arg(short, long, default_value = "false")]
115        config: bool,
116        /// VM to clone from
117        from: String,
118        /// VM to clone to
119        to: String,
120    },
121    /// Import a VM from a VM image file
122    Import {
123        /// Format of incoming image
124        #[arg(short, long, required = true)]
125        format: String,
126        /// VM to import to
127        name: String,
128        /// VM image to import from
129        from_file: PathBuf,
130    },
131    /// Show and manipulate VM configuration
132    #[command(subcommand)]
133    Config(ConfigSubcommand),
134    /// Show and manipulate VM snapshots
135    #[command(subcommand)]
136    Snapshot(SnapshotSubcommand),
137    /// Is this VM currently active?
138    IsActive {
139        /// Name of VM
140        name: String,
141    },
142    /// Quickly save a snapshot of the named VM
143    Save {
144        /// Name of VM
145        name: String,
146    },
147    /// Load the quick save of the named VM
148    Load {
149        /// Name of VM
150        name: String,
151    },
152    /// Delete the quick save of the named VM
153    ClearState {
154        /// Name of VM
155        name: String,
156    },
157    /// Hard Reset a VM
158    Reset {
159        /// Name of VM
160        name: String,
161    },
162    /// Shutdown and re-launch the VM. Does not work with supervisors.
163    Restart {
164        /// Name of VM
165        name: String,
166    },
167}
168
169#[derive(Debug, Subcommand, Clone)]
170pub enum ConfigSubcommand {
171    /// Show the written+inferred configuration for a VM
172    Show {
173        /// Name of VM
174        name: String,
175    },
176    /// Set a value int the configuration; type-safe
177    Set {
178        /// Name of VM
179        name: String,
180        /// Name of key to set
181        key: String,
182        /// Value of key to set
183        value: String,
184    },
185    /// Copy settings from one VM to another
186    Copy {
187        /// Name of VM to copy settings from
188        from: String,
189        /// Name of VM to copy settings to
190        to: String,
191    },
192    /// Adjust port mappings
193    #[command(subcommand)]
194    Port(ConfigPortSubcommand),
195}
196
197#[derive(Debug, Subcommand, Clone)]
198pub enum SnapshotSubcommand {
199    /// Save a snapshot for a VM
200    Save {
201        /// Name of VM
202        name: String,
203        /// Name of snapshot to take (must not already exist)
204        snapshot_name: String,
205    },
206    /// Delete an existing snapshot from a VM
207    Delete {
208        /// Name of VM
209        name: String,
210        /// Name of snapshot to take (must not already exist)
211        snapshot_name: String,
212    },
213    /// Load an existing snapshot from a VM into the current state
214    Load {
215        /// Name of VM
216        name: String,
217        /// Name of snapshot to take (must not already exist)
218        snapshot_name: String,
219    },
220}
221
222#[derive(Debug, Subcommand, Clone)]
223pub enum ConfigPortSubcommand {
224    /// Add a port mapping from localhost:<HOSTPORT> -> <GUEST IP>:<GUESTPORT>
225    Map {
226        /// Name of VM
227        name: String,
228        /// Port on localhost to map to guest remote IP
229        hostport: u16,
230        /// Port on guest to expose
231        guestport: u16,
232    },
233    /// Undo a port mapping
234    Unmap {
235        /// Name of VM
236        name: String,
237        /// Port on localhost mapped to guest
238        hostport: u16,
239    },
240}