From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 09CB27A0ED for ; Thu, 6 May 2021 17:27:07 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id D81D422D86 for ; Thu, 6 May 2021 17:27:06 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 6191122D3D for ; Thu, 6 May 2021 17:27:04 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 3B7E24651C for ; Thu, 6 May 2021 17:27:04 +0200 (CEST) From: Stefan Reiter To: pbs-devel@lists.proxmox.com Date: Thu, 6 May 2021 17:26:16 +0200 Message-Id: <20210506152624.12605-2-s.reiter@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210506152624.12605-1-s.reiter@proxmox.com> References: <20210506152624.12605-1-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.014 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [buildcfg.rs] Subject: [pbs-devel] [PATCH proxmox-backup 1/9] file-restore: add debug mode with serial access X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 06 May 2021 15:27:07 -0000 Set PBS_QEMU_DEBUG=1 on a command that starts a VM and then connect to the debug root shell via: minicom -D \unix#/run/proxmox-backup/file-restore-serial-10.sock or similar. Note that this requires 'proxmox-backup-restore-image-debug' to work, the postinst script is updated to also generate the corresponding image. Signed-off-by: Stefan Reiter --- debian/proxmox-backup-file-restore.postinst | 12 ++++- src/bin/proxmox_file_restore/qemu_helper.rs | 55 ++++++++++++++++++--- src/buildcfg.rs | 4 ++ 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/debian/proxmox-backup-file-restore.postinst b/debian/proxmox-backup-file-restore.postinst index 27eb87e9..feb0557b 100755 --- a/debian/proxmox-backup-file-restore.postinst +++ b/debian/proxmox-backup-file-restore.postinst @@ -6,6 +6,7 @@ update_initramfs() { # regenerate initramfs for single file restore VM INST_PATH="/usr/lib/x86_64-linux-gnu/proxmox-backup/file-restore" CACHE_PATH="/var/cache/proxmox-backup/file-restore-initramfs.img" + CACHE_PATH_DBG="/var/cache/proxmox-backup/file-restore-initramfs-debug.img" # cleanup first, in case proxmox-file-restore was uninstalled since we do # not want an unuseable image lying around @@ -20,7 +21,7 @@ update_initramfs() { # avoid leftover temp file cleanup() { - rm -f "$CACHE_PATH.tmp" + rm -f "$CACHE_PATH.tmp" "$CACHE_PATH_DBG.tmp" } trap cleanup EXIT @@ -34,6 +35,15 @@ update_initramfs() { | cpio -o --format=newc -A -F "$CACHE_PATH.tmp" ) mv -f "$CACHE_PATH.tmp" "$CACHE_PATH" + if [ -f "$INST_PATH/initramfs-debug.img" ]; then + echo "Updating file-restore debug initramfs..." + cp "$INST_PATH/initramfs-debug.img" "$CACHE_PATH_DBG.tmp" + ( cd "$INST_PATH"; \ + printf "./proxmox-restore-daemon" \ + | cpio -o --format=newc -A -F "$CACHE_PATH_DBG.tmp" ) + mv -f "$CACHE_PATH_DBG.tmp" "$CACHE_PATH_DBG" + fi + trap - EXIT } diff --git a/src/bin/proxmox_file_restore/qemu_helper.rs b/src/bin/proxmox_file_restore/qemu_helper.rs index 9328addf..20b4f344 100644 --- a/src/bin/proxmox_file_restore/qemu_helper.rs +++ b/src/bin/proxmox_file_restore/qemu_helper.rs @@ -47,9 +47,13 @@ fn create_restore_log_dir() -> Result { Ok(logpath) } -fn validate_img_existance() -> Result<(), Error> { +fn validate_img_existance(debug: bool) -> Result<(), Error> { let kernel = PathBuf::from(buildcfg::PROXMOX_BACKUP_KERNEL_FN); - let initramfs = PathBuf::from(buildcfg::PROXMOX_BACKUP_INITRAMFS_FN); + let initramfs = PathBuf::from(if debug { + buildcfg::PROXMOX_BACKUP_INITRAMFS_DBG_FN + } else { + buildcfg::PROXMOX_BACKUP_INITRAMFS_FN + }); if !kernel.exists() || !initramfs.exists() { bail!("cannot run file-restore VM: package 'proxmox-backup-restore-image' is not (correctly) installed"); } @@ -79,7 +83,7 @@ fn try_kill_vm(pid: i32) -> Result<(), Error> { Ok(()) } -async fn create_temp_initramfs(ticket: &str) -> Result<(Fd, String), Error> { +async fn create_temp_initramfs(ticket: &str, debug: bool) -> Result<(Fd, String), Error> { use std::ffi::CString; use tokio::fs::File; @@ -88,8 +92,14 @@ async fn create_temp_initramfs(ticket: &str) -> Result<(Fd, String), Error> { nix::unistd::unlink(&tmp_path)?; tools::fd_change_cloexec(tmp_fd.0, false)?; + let initramfs = if debug { + buildcfg::PROXMOX_BACKUP_INITRAMFS_DBG_FN + } else { + buildcfg::PROXMOX_BACKUP_INITRAMFS_FN + }; + let mut f = File::from_std(unsafe { std::fs::File::from_raw_fd(tmp_fd.0) }); - let mut base = File::open(buildcfg::PROXMOX_BACKUP_INITRAMFS_FN).await?; + let mut base = File::open(initramfs).await?; tokio::io::copy(&mut base, &mut f).await?; @@ -122,18 +132,24 @@ pub async fn start_vm( files: impl Iterator, ticket: &str, ) -> Result<(i32, i32), Error> { - validate_img_existance()?; - if let Err(_) = std::env::var("PBS_PASSWORD") { bail!("environment variable PBS_PASSWORD has to be set for QEMU VM restore"); } + let debug = if let Ok(val) = std::env::var("PBS_QEMU_DEBUG") { + !val.is_empty() + } else { + false + }; + + validate_img_existance(debug)?; + let pid; let (pid_fd, pid_path) = make_tmp_file("/tmp/file-restore-qemu.pid.tmp", CreateOptions::new())?; nix::unistd::unlink(&pid_path)?; tools::fd_change_cloexec(pid_fd.0, false)?; - let (_ramfs_pid, ramfs_path) = create_temp_initramfs(ticket).await?; + let (_ramfs_pid, ramfs_path) = create_temp_initramfs(ticket, debug).await?; let logpath = create_restore_log_dir()?; let logfile = &format!("{}/qemu.log", logpath); @@ -174,7 +190,11 @@ pub async fn start_vm( "-initrd", &ramfs_path, "-append", - "quiet panic=1", + if debug { + "debug panic=1" + } else { + "quiet panic=1" + }, "-daemonize", "-pidfile", &format!("/dev/fd/{}", pid_fd.as_raw_fd()), @@ -218,6 +238,19 @@ pub async fn start_vm( cid )); + 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()); + } + qemu_cmd.stdout(std::process::Stdio::null()); qemu_cmd.stderr(std::process::Stdio::piped()); @@ -260,6 +293,12 @@ pub async fn start_vm( if let Ok(Ok(_)) = time::timeout(Duration::from_secs(2), client.get("api2/json/status", None)).await { + if debug { + eprintln!( + "Connect to '/run/proxmox-backup/file-restore-serial-{}.sock' for shell access", + cid + ) + } return Ok((pid, cid as i32)); } if kill(pid_t, None).is_err() { diff --git a/src/buildcfg.rs b/src/buildcfg.rs index b0f61efb..c70ab6ea 100644 --- a/src/buildcfg.rs +++ b/src/buildcfg.rs @@ -43,6 +43,10 @@ pub const PROXMOX_BACKUP_API_PID_FN: &str = concat!(PROXMOX_BACKUP_RUN_DIR_M!(), pub const PROXMOX_BACKUP_INITRAMFS_FN: &str = concat!(PROXMOX_BACKUP_CACHE_DIR_M!(), "/file-restore-initramfs.img"); +/// filename of the cached initramfs to use for debugging single file restore +pub const PROXMOX_BACKUP_INITRAMFS_DBG_FN: &str = + concat!(PROXMOX_BACKUP_CACHE_DIR_M!(), "/file-restore-initramfs-debug.img"); + /// filename of the kernel to use for booting single file restore VMs pub const PROXMOX_BACKUP_KERNEL_FN: &str = concat!(PROXMOX_BACKUP_FILE_RESTORE_BIN_DIR_M!(), "/bzImage"); -- 2.20.1