2021-04-01 15:43:52 +00:00
|
|
|
//! Helper to start a QEMU VM for single file restore.
|
|
|
|
use std::fs::{File, OpenOptions};
|
|
|
|
use std::io::prelude::*;
|
2021-07-14 11:37:26 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2021-04-01 15:43:52 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use anyhow::{bail, format_err, Error};
|
|
|
|
use tokio::time;
|
|
|
|
|
|
|
|
use nix::sys::signal::{kill, Signal};
|
|
|
|
use nix::unistd::Pid;
|
|
|
|
|
2021-11-23 16:57:00 +00:00
|
|
|
use proxmox_sys::fs::{create_path, file_read_string, make_tmp_file, CreateOptions};
|
|
|
|
use proxmox_sys::fd::fd_change_cloexec;
|
2021-11-19 09:51:41 +00:00
|
|
|
use proxmox_sys::logrotate::LogRotate;
|
2021-04-01 15:43:52 +00:00
|
|
|
|
2021-07-19 08:50:18 +00:00
|
|
|
use pbs_client::{VsockClient, DEFAULT_VSOCK_PORT};
|
|
|
|
|
2021-09-01 10:21:51 +00:00
|
|
|
use crate::{cpio, backup_user};
|
2021-04-01 15:43:52 +00:00
|
|
|
use super::SnapRestoreDetails;
|
|
|
|
|
|
|
|
const PBS_VM_NAME: &str = "pbs-restore-vm";
|
|
|
|
const MAX_CID_TRIES: u64 = 32;
|
|
|
|
|
|
|
|
fn create_restore_log_dir() -> Result<String, Error> {
|
2021-07-06 09:56:35 +00:00
|
|
|
let logpath = format!("{}/file-restore", pbs_buildcfg::PROXMOX_BACKUP_LOG_DIR);
|
2021-04-01 15:43:52 +00:00
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
proxmox_lang::try_block!({
|
2021-04-01 15:43:52 +00:00
|
|
|
let backup_user = backup_user()?;
|
|
|
|
let opts = CreateOptions::new()
|
|
|
|
.owner(backup_user.uid)
|
|
|
|
.group(backup_user.gid);
|
|
|
|
|
|
|
|
let opts_root = CreateOptions::new()
|
|
|
|
.owner(nix::unistd::ROOT)
|
|
|
|
.group(nix::unistd::Gid::from_raw(0));
|
|
|
|
|
2021-07-06 09:56:35 +00:00
|
|
|
create_path(pbs_buildcfg::PROXMOX_BACKUP_LOG_DIR, None, Some(opts))?;
|
2021-04-01 15:43:52 +00:00
|
|
|
create_path(&logpath, None, Some(opts_root))?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.map_err(|err: Error| format_err!("unable to create file-restore log dir - {}", err))?;
|
|
|
|
|
|
|
|
Ok(logpath)
|
|
|
|
}
|
|
|
|
|
2021-05-06 15:26:16 +00:00
|
|
|
fn validate_img_existance(debug: bool) -> Result<(), Error> {
|
2021-07-06 09:56:35 +00:00
|
|
|
let kernel = PathBuf::from(pbs_buildcfg::PROXMOX_BACKUP_KERNEL_FN);
|
2021-05-06 15:26:16 +00:00
|
|
|
let initramfs = PathBuf::from(if debug {
|
2021-07-06 09:56:35 +00:00
|
|
|
pbs_buildcfg::PROXMOX_BACKUP_INITRAMFS_DBG_FN
|
2021-05-06 15:26:16 +00:00
|
|
|
} else {
|
2021-07-06 09:56:35 +00:00
|
|
|
pbs_buildcfg::PROXMOX_BACKUP_INITRAMFS_FN
|
2021-05-06 15:26:16 +00:00
|
|
|
});
|
2021-04-01 15:43:52 +00:00
|
|
|
if !kernel.exists() || !initramfs.exists() {
|
2021-04-27 08:48:16 +00:00
|
|
|
bail!("cannot run file-restore VM: package 'proxmox-backup-restore-image' is not (correctly) installed");
|
2021-04-01 15:43:52 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-05-06 15:26:17 +00:00
|
|
|
pub fn try_kill_vm(pid: i32) -> Result<(), Error> {
|
2021-04-01 15:43:52 +00:00
|
|
|
let pid = Pid::from_raw(pid);
|
2021-12-30 14:01:31 +00:00
|
|
|
if kill(pid, None).is_ok() {
|
2021-04-01 15:43:52 +00:00
|
|
|
// process is running (and we could kill it), check if it is actually ours
|
|
|
|
// (if it errors assume we raced with the process's death and ignore it)
|
|
|
|
if let Ok(cmdline) = file_read_string(format!("/proc/{}/cmdline", pid)) {
|
|
|
|
if cmdline.split('\0').any(|a| a == PBS_VM_NAME) {
|
|
|
|
// yes, it's ours, kill it brutally with SIGKILL, no reason to take
|
|
|
|
// any chances - in this state it's most likely broken anyway
|
|
|
|
if let Err(err) = kill(pid, Signal::SIGKILL) {
|
|
|
|
bail!(
|
|
|
|
"reaping broken VM (pid {}) with SIGKILL failed: {}",
|
|
|
|
pid,
|
|
|
|
err
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-14 11:37:26 +00:00
|
|
|
async fn create_temp_initramfs(ticket: &str, debug: bool) -> Result<(File, String), Error> {
|
2021-04-01 15:43:52 +00:00
|
|
|
use std::ffi::CString;
|
|
|
|
use tokio::fs::File;
|
|
|
|
|
2021-07-14 11:37:26 +00:00
|
|
|
let (tmp_file, tmp_path) =
|
2021-04-01 15:43:52 +00:00
|
|
|
make_tmp_file("/tmp/file-restore-qemu.initramfs.tmp", CreateOptions::new())?;
|
|
|
|
nix::unistd::unlink(&tmp_path)?;
|
2021-11-18 12:43:41 +00:00
|
|
|
fd_change_cloexec(tmp_file.as_raw_fd(), false)?;
|
2021-04-01 15:43:52 +00:00
|
|
|
|
2021-05-06 15:26:16 +00:00
|
|
|
let initramfs = if debug {
|
2021-07-06 09:56:35 +00:00
|
|
|
pbs_buildcfg::PROXMOX_BACKUP_INITRAMFS_DBG_FN
|
2021-05-06 15:26:16 +00:00
|
|
|
} else {
|
2021-07-06 09:56:35 +00:00
|
|
|
pbs_buildcfg::PROXMOX_BACKUP_INITRAMFS_FN
|
2021-05-06 15:26:16 +00:00
|
|
|
};
|
|
|
|
|
2021-07-14 11:37:26 +00:00
|
|
|
let mut f = File::from_std(tmp_file);
|
2021-05-06 15:26:16 +00:00
|
|
|
let mut base = File::open(initramfs).await?;
|
2021-04-01 15:43:52 +00:00
|
|
|
|
|
|
|
tokio::io::copy(&mut base, &mut f).await?;
|
|
|
|
|
|
|
|
let name = CString::new("ticket").unwrap();
|
2021-09-01 10:21:51 +00:00
|
|
|
cpio::append_file(
|
2021-04-01 15:43:52 +00:00
|
|
|
&mut f,
|
|
|
|
ticket.as_bytes(),
|
|
|
|
&name,
|
|
|
|
0,
|
|
|
|
(libc::S_IFREG | 0o400) as u16,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
ticket.len() as u32,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-09-01 10:21:51 +00:00
|
|
|
cpio::append_trailer(&mut f).await?;
|
2021-04-01 15:43:52 +00:00
|
|
|
|
2021-07-14 11:37:26 +00:00
|
|
|
let tmp_file = f.into_std().await;
|
|
|
|
let path = format!("/dev/fd/{}", &tmp_file.as_raw_fd());
|
2021-04-01 15:43:52 +00:00
|
|
|
|
2021-07-14 11:37:26 +00:00
|
|
|
Ok((tmp_file, path))
|
2021-04-01 15:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn start_vm(
|
|
|
|
// u16 so we can do wrapping_add without going too high
|
|
|
|
mut cid: u16,
|
|
|
|
details: &SnapRestoreDetails,
|
|
|
|
files: impl Iterator<Item = String>,
|
|
|
|
ticket: &str,
|
|
|
|
) -> Result<(i32, i32), Error> {
|
2021-12-30 14:01:31 +00:00
|
|
|
if std::env::var("PBS_PASSWORD").is_err() {
|
2021-04-01 15:43:52 +00:00
|
|
|
bail!("environment variable PBS_PASSWORD has to be set for QEMU VM restore");
|
|
|
|
}
|
|
|
|
|
2021-05-06 15:26:16 +00:00
|
|
|
let debug = if let Ok(val) = std::env::var("PBS_QEMU_DEBUG") {
|
|
|
|
!val.is_empty()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
validate_img_existance(debug)?;
|
|
|
|
|
2021-04-01 15:43:52 +00:00
|
|
|
let pid;
|
2021-07-14 11:37:26 +00:00
|
|
|
let (mut pid_file, pid_path) = make_tmp_file("/tmp/file-restore-qemu.pid.tmp", CreateOptions::new())?;
|
2021-04-01 15:43:52 +00:00
|
|
|
nix::unistd::unlink(&pid_path)?;
|
2021-11-18 12:43:41 +00:00
|
|
|
fd_change_cloexec(pid_file.as_raw_fd(), false)?;
|
2021-04-01 15:43:52 +00:00
|
|
|
|
2021-05-06 15:26:16 +00:00
|
|
|
let (_ramfs_pid, ramfs_path) = create_temp_initramfs(ticket, debug).await?;
|
2021-04-01 15:43:52 +00:00
|
|
|
|
|
|
|
let logpath = create_restore_log_dir()?;
|
|
|
|
let logfile = &format!("{}/qemu.log", logpath);
|
2021-11-19 09:51:41 +00:00
|
|
|
let mut logrotate = LogRotate::new(logfile, false, Some(16), None)?;
|
2021-04-01 15:43:52 +00:00
|
|
|
|
2021-11-19 09:51:41 +00:00
|
|
|
if let Err(err) = logrotate.do_rotate() {
|
2021-04-01 15:43:52 +00:00
|
|
|
eprintln!("warning: logrotate for QEMU log file failed - {}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut logfd = OpenOptions::new()
|
|
|
|
.append(true)
|
|
|
|
.create_new(true)
|
|
|
|
.open(logfile)?;
|
2021-11-18 12:43:41 +00:00
|
|
|
fd_change_cloexec(logfd.as_raw_fd(), false)?;
|
2021-04-01 15:43:52 +00:00
|
|
|
|
|
|
|
// preface log file with start timestamp so one can see how long QEMU took to start
|
|
|
|
writeln!(logfd, "[{}] PBS file restore VM log", {
|
2021-10-08 09:19:37 +00:00
|
|
|
let now = proxmox_time::epoch_i64();
|
|
|
|
proxmox_time::epoch_to_rfc3339(now)?
|
2021-04-01 15:43:52 +00:00
|
|
|
},)?;
|
|
|
|
|
|
|
|
let base_args = [
|
|
|
|
"-chardev",
|
|
|
|
&format!(
|
|
|
|
"file,id=log,path=/dev/null,logfile=/dev/fd/{},logappend=on",
|
|
|
|
logfd.as_raw_fd()
|
|
|
|
),
|
|
|
|
"-serial",
|
|
|
|
"chardev:log",
|
|
|
|
"-vnc",
|
|
|
|
"none",
|
|
|
|
"-enable-kvm",
|
|
|
|
"-kernel",
|
2021-07-06 09:56:35 +00:00
|
|
|
pbs_buildcfg::PROXMOX_BACKUP_KERNEL_FN,
|
2021-04-01 15:43:52 +00:00
|
|
|
"-initrd",
|
|
|
|
&ramfs_path,
|
|
|
|
"-append",
|
2021-06-16 10:55:50 +00:00
|
|
|
&format!(
|
|
|
|
"{} panic=1 zfs_arc_min=0 zfs_arc_max=0",
|
|
|
|
if debug { "debug" } else { "quiet" }
|
|
|
|
),
|
2021-04-01 15:43:52 +00:00
|
|
|
"-daemonize",
|
|
|
|
"-pidfile",
|
2021-07-14 11:37:26 +00:00
|
|
|
&format!("/dev/fd/{}", pid_file.as_raw_fd()),
|
2021-04-01 15:43:52 +00:00
|
|
|
"-name",
|
|
|
|
PBS_VM_NAME,
|
|
|
|
];
|
|
|
|
|
|
|
|
// Generate drive arguments for all fidx files in backup snapshot
|
|
|
|
let mut drives = Vec::new();
|
|
|
|
let mut id = 0;
|
|
|
|
for file in files {
|
|
|
|
if !file.ends_with(".img.fidx") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
drives.push("-drive".to_owned());
|
2021-04-22 15:34:47 +00:00
|
|
|
let keyfile = if let Some(ref keyfile) = details.keyfile {
|
|
|
|
format!(",,keyfile={}", keyfile)
|
|
|
|
} else {
|
|
|
|
"".to_owned()
|
|
|
|
};
|
2021-04-01 15:43:52 +00:00
|
|
|
drives.push(format!(
|
2021-04-22 15:34:47 +00:00
|
|
|
"file=pbs:repository={},,snapshot={},,archive={}{},read-only=on,if=none,id=drive{}",
|
|
|
|
details.repo, details.snapshot, file, keyfile, id
|
2021-04-01 15:43:52 +00:00
|
|
|
));
|
2021-05-06 15:26:19 +00:00
|
|
|
|
|
|
|
// a PCI bus can only support 32 devices, so add a new one every 32
|
|
|
|
let bus = (id / 32) + 2;
|
|
|
|
if id % 32 == 0 {
|
|
|
|
drives.push("-device".to_owned());
|
|
|
|
drives.push(format!("pci-bridge,id=bridge{},chassis_nr={}", bus, bus));
|
|
|
|
}
|
|
|
|
|
2021-04-01 15:43:52 +00:00
|
|
|
drives.push("-device".to_owned());
|
|
|
|
// drive serial is used by VM to map .fidx files to /dev paths
|
2021-04-27 13:35:45 +00:00
|
|
|
let serial = file.strip_suffix(".img.fidx").unwrap_or(&file);
|
2021-05-06 15:26:19 +00:00
|
|
|
drives.push(format!(
|
|
|
|
"virtio-blk-pci,drive=drive{},serial={},bus=bridge{}",
|
|
|
|
id, serial, bus
|
|
|
|
));
|
2021-04-01 15:43:52 +00:00
|
|
|
id += 1;
|
|
|
|
}
|
|
|
|
|
2021-05-06 15:26:18 +00:00
|
|
|
let ram = if debug {
|
|
|
|
1024
|
|
|
|
} else {
|
|
|
|
// add more RAM if many drives are given
|
|
|
|
match id {
|
2021-06-16 10:55:50 +00:00
|
|
|
f if f < 10 => 192,
|
|
|
|
f if f < 20 => 256,
|
|
|
|
_ => 384,
|
2021-05-06 15:26:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-01 15:43:52 +00:00
|
|
|
// Try starting QEMU in a loop to retry if we fail because of a bad 'cid' value
|
|
|
|
let mut attempts = 0;
|
|
|
|
loop {
|
|
|
|
let mut qemu_cmd = std::process::Command::new("qemu-system-x86_64");
|
|
|
|
qemu_cmd.args(base_args.iter());
|
2021-05-06 15:26:18 +00:00
|
|
|
qemu_cmd.arg("-m");
|
|
|
|
qemu_cmd.arg(ram.to_string());
|
2021-04-01 15:43:52 +00:00
|
|
|
qemu_cmd.args(&drives);
|
|
|
|
qemu_cmd.arg("-device");
|
|
|
|
qemu_cmd.arg(format!(
|
|
|
|
"vhost-vsock-pci,guest-cid={},disable-legacy=on",
|
|
|
|
cid
|
|
|
|
));
|
|
|
|
|
2021-05-06 15:26:16 +00:00
|
|
|
if debug {
|
|
|
|
let debug_args = [
|
|
|
|
"-chardev",
|
|
|
|
&format!(
|
|
|
|
"socket,id=debugser,path=/run/proxmox-backup/file-restore-serial-{}.sock,server,nowait",
|
|
|
|
cid
|
|
|
|
),
|
|
|
|
"-serial",
|
|
|
|
"chardev:debugser",
|
|
|
|
];
|
|
|
|
qemu_cmd.args(debug_args.iter());
|
|
|
|
}
|
|
|
|
|
2021-04-01 15:43:52 +00:00
|
|
|
qemu_cmd.stdout(std::process::Stdio::null());
|
|
|
|
qemu_cmd.stderr(std::process::Stdio::piped());
|
|
|
|
|
|
|
|
let res = tokio::task::block_in_place(|| qemu_cmd.spawn()?.wait_with_output())?;
|
|
|
|
|
|
|
|
if res.status.success() {
|
|
|
|
// at this point QEMU is already daemonized and running, so if anything fails we
|
|
|
|
// technically leave behind a zombie-VM... this shouldn't matter, as it will stop
|
|
|
|
// itself soon enough (timer), and the following operations are unlikely to fail
|
|
|
|
let mut pidstr = String::new();
|
|
|
|
pid_file.read_to_string(&mut pidstr)?;
|
|
|
|
pid = pidstr.trim_end().parse().map_err(|err| {
|
|
|
|
format_err!("cannot parse PID returned by QEMU ('{}'): {}", &pidstr, err)
|
|
|
|
})?;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
let out = String::from_utf8_lossy(&res.stderr);
|
|
|
|
if out.contains("unable to set guest cid: Address already in use") {
|
|
|
|
attempts += 1;
|
|
|
|
if attempts >= MAX_CID_TRIES {
|
|
|
|
bail!("CID '{}' in use, but max attempts reached, aborting", cid);
|
|
|
|
}
|
|
|
|
// CID in use, try next higher one
|
|
|
|
eprintln!("CID '{}' in use by other VM, attempting next one", cid);
|
|
|
|
// skip special-meaning low values
|
|
|
|
cid = cid.wrapping_add(1).max(10);
|
|
|
|
} else {
|
|
|
|
eprint!("{}", out);
|
|
|
|
bail!("Starting VM failed. See output above for more information.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// QEMU has started successfully, now wait for virtio socket to become ready
|
|
|
|
let pid_t = Pid::from_raw(pid);
|
|
|
|
for _ in 0..60 {
|
|
|
|
let client = VsockClient::new(cid as i32, DEFAULT_VSOCK_PORT, Some(ticket.to_owned()));
|
|
|
|
if let Ok(Ok(_)) =
|
|
|
|
time::timeout(Duration::from_secs(2), client.get("api2/json/status", None)).await
|
|
|
|
{
|
2021-05-06 15:26:16 +00:00
|
|
|
if debug {
|
|
|
|
eprintln!(
|
|
|
|
"Connect to '/run/proxmox-backup/file-restore-serial-{}.sock' for shell access",
|
|
|
|
cid
|
|
|
|
)
|
|
|
|
}
|
2021-04-01 15:43:52 +00:00
|
|
|
return Ok((pid, cid as i32));
|
|
|
|
}
|
2021-07-23 06:25:34 +00:00
|
|
|
if kill(pid_t, None).is_err() { // check if QEMU process exited in between
|
2021-04-01 15:43:52 +00:00
|
|
|
bail!("VM exited before connection could be established");
|
|
|
|
}
|
|
|
|
time::sleep(Duration::from_millis(200)).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start failed
|
|
|
|
if let Err(err) = try_kill_vm(pid) {
|
|
|
|
eprintln!("killing failed VM failed: {}", err);
|
|
|
|
}
|
|
|
|
bail!("starting VM timed out");
|
|
|
|
}
|