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
65 changes: 53 additions & 12 deletions sh/upgrade_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,41 @@ if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
fi

DB_PATH="$1"
DB_ACTUAL_VERSION=21
DB_ACTUAL_VERSION=22
DB_CURRENT_VERSION=$(sqlite3 "$DB_PATH" -line 'PRAGMA user_version;' | sed 's/.*[[:space:]]=[[:space:]]//')
USER=$(whoami)
RC=0
CFG_PATH=""

if [ "$#" -eq 2 ]; then
QEMU_BIN_PATH="$3"
USER_DIR=$(grep ${USER} /etc/passwd | awk 'BEGIN { FS = ":" }; { printf "%s\n", $6 }')
if [ ! -d "$USER_DIR" ]; then
echo "Couldn't find user home directory" >&2
exit 1
fi

if [ -n "$XDG_CONFIG_HOME" ]; then
if [ -f "${XDG_CONFIG_HOME}/nemu/nemu.cfg" ]; then
CFG_PATH="${XDG_CONFIG_HOME}/nemu/nemu.cfg"
fi
else
USER_DIR=$(grep ${USER} /etc/passwd | awk 'BEGIN { FS = ":" }; { printf "%s\n", $6 }')
if [ ! -d "$USER_DIR" ]; then
echo "Couldn't find user home directory" >&2
exit 1
if [ -f "${USER_DIR}/.config/nemu/nemu.cfg" ]; then
CFG_PATH="${USER_DIR}/.config/nemu/nemu.cfg"
fi
#TODO we shouldn't expect to find .nemu.cfg in homedir
if [ ! -f ${USER_DIR}/.nemu.cfg ]; then
echo "Couldn't find .nemu.cfg in user home directory" >&2
fi

if [ -z "$CFG_PATH" ]; then
CFG_PATH="${USER_DIR}/.nemu.cfg"

if [ ! -f "$CFG_PATH" ]; then
echo "Couldn't find nemu configuration file" >&2
exit 1
fi
fi

#TODO we shouldn't expect to find .nemu.cfg in homedir
QEMU_BIN_PATH=$(grep qemu_bin_path ${USER_DIR}/.nemu.cfg | awk '{ printf "%s\n", $3 }')
if [ "$#" -eq 2 ]; then
QEMU_BIN_PATH="$3"
else
QEMU_BIN_PATH=$(grep qemu_bin_path $CFG_PATH | awk '{ printf "%s\n", $3 }')
if [ -z "$QEMU_BIN_PATH" ]; then
echo "Couldn't get qemu_bin_path from .nemu.cfg" >&2
exit 1
Expand All @@ -43,6 +57,22 @@ if [ "$DB_CURRENT_VERSION" = "$DB_ACTUAL_VERSION" ]; then
exit 0
fi

db_update_paths()
{
local rc=0
local vm_dir=$(grep vmdir $CFG_PATH | awk '{ printf "%s\n", $3 }')
local vms=$(sqlite3 "$DB_PATH" -list 'SELECT name FROM vms')

for vm in $vms; do
sqlite3 "$DB_PATH" -line \
"UPDATE vms SET pid_path='${vm_dir}/${vm}/qemu.pid' WHERE name='${vm}'" || rc=1
sqlite3 "$DB_PATH" -line \
"UPDATE vms SET qmp_sock_path='${vm_dir}/${vm}/qmp.sock' WHERE name='${vm}'" || rc=1
done

return $rc
}

db_update_machine()
{
local a
Expand Down Expand Up @@ -383,6 +413,17 @@ while [ "$DB_CURRENT_VERSION" != "$DB_ACTUAL_VERSION" ]; do
) || RC=1
;;

( 21 )
(
sqlite3 "$DB_PATH" -line 'ALTER TABLE vms ADD pid_path '`
`'TEXT NOT NULL DEFAULT "_must_be_set_";' &&
sqlite3 "$DB_PATH" -line 'ALTER TABLE vms ADD qmp_sock_path '`
`'TEXT NOT NULL DEFAULT "_must_be_set_";' &&
db_update_paths &&
sqlite3 "$DB_PATH" -line 'PRAGMA user_version=22'
) || RC=1
;;

( * )
echo "Unsupported database user_version" >&2
exit 1
Expand Down
2 changes: 1 addition & 1 deletion src/nm_add_drive.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void nm_del_drive(const nm_str_t *name)
NULL);
}

m_drvs.v = &drv_list;
m_drvs.items = &drv_list;

do {
nm_menu_scroll(&m_drvs, drv_list_len, ch);
Expand Down
15 changes: 14 additions & 1 deletion src/nm_add_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ static void nm_add_vm_main(void)
}
}

nm_add_vm_set_default_paths(&vm);
nm_add_vm_to_fs(&vm);
nm_add_vm_to_db(&vm, last_mac, import, NULL);

Expand Down Expand Up @@ -403,7 +404,9 @@ void nm_add_vm_to_db(nm_vm_t *vm, uint64_t mac,
"", /* GDB debug port */
NM_DISABLE, /* disable GDB debug by default */
NM_DEFAULT_DISPLAY, /* Display type */
NM_DISABLE /* disable SPICE agent debug by default */
NM_DISABLE, /* disable SPICE agent debug by default */
vm->pid_path.data,
vm->qmp_path.data
);

nm_db_edit(query.data);
Expand Down Expand Up @@ -522,4 +525,14 @@ static void nm_add_vm_to_fs(nm_vm_t *vm)
nm_str_free(&dst_img);
}

void nm_add_vm_set_default_paths(nm_vm_t *vm)
{
const nm_cfg_t *cfg = nm_cfg_get();

nm_str_format(&vm->pid_path,
"%s/%s/%s", cfg->vm_dir.data, vm->name.data, NM_VM_PID_FILE);
nm_str_format(&vm->qmp_path,
"%s/%s/%s", cfg->vm_dir.data, vm->name.data, NM_VM_QMP_FILE);
}

/* vim:set ts=4 sw=4: */
1 change: 1 addition & 0 deletions src/nm_add_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

void nm_add_vm(void);
void nm_import_vm(void);
void nm_add_vm_set_default_paths(nm_vm_t *vm);
void nm_add_vm_to_db(nm_vm_t *vm, uint64_t mac,
int import, const nm_vect_t *drives);

Expand Down
12 changes: 11 additions & 1 deletion src/nm_clone_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,28 @@ static void nm_clone_vm_to_db(const nm_str_t *src, const nm_str_t *dst,
const nm_vmctl_data_t *vm)
{
nm_str_t query = NM_INIT_STR;
nm_str_t pid_path = NM_INIT_STR;
nm_str_t qmp_path = NM_INIT_STR;
uint64_t last_mac;
uint32_t last_vnc;
size_t ifs_count;
size_t drives_count;
char drv_ch = 'a';
const nm_cfg_t *cfg = nm_cfg_get();

last_mac = nm_form_get_last_mac();
last_vnc = nm_form_get_free_vnc();

nm_str_format(&pid_path,
"%s/%s/%s", cfg->vm_dir.data, dst->data, NM_VM_PID_FILE);
nm_str_format(&qmp_path,
"%s/%s/%s", cfg->vm_dir.data, dst->data, NM_VM_QMP_FILE);

nm_str_format(&query, NM_SQL_VMS_INSERT_CLONE,
dst->data, last_vnc, src->data);
dst->data, last_vnc, pid_path.data, qmp_path.data, src->data);
nm_db_edit(query.data);
nm_str_free(&pid_path);
nm_str_free(&qmp_path);

/* insert network interface info */
ifs_count = vm->ifs.n_memb / NM_IFS_IDX_COUNT;
Expand Down
40 changes: 34 additions & 6 deletions src/nm_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <sqlite3.h>

#define NM_DB_VERSION "21"
#define NM_DB_VERSION "22"

/* PRAGMAS */
static const char NM_SQL_PRAGMA_FOREIGN_ON[] =
Expand All @@ -28,7 +28,8 @@ static const char NM_SQL_VMS_CREATE[] =
"fs9p_enable INTEGER NOT NULL, fs9p_path TEXT, fs9p_name TEXT, "
"usb_type TEXT NOT NULL, spice INTEGER NOT NULL, debug_port INTEGER, "
"debug_freeze INTEGER NOT NULL, cmdappend TEXT, team TEXT, "
"display_type TEXT NOT NULL, pflash TEXT, spice_agent INTEGER NOT NULL)";
"display_type TEXT NOT NULL, pflash TEXT, spice_agent INTEGER NOT NULL, "
"pid_path TEXT NOT NULL, qmp_sock_path TEXT NOT NULL)";

static const char NM_SQL_IFACES_CREATE[] =
"CREATE TABLE ifaces(if_name TEXT NOT NULL, "
Expand Down Expand Up @@ -70,15 +71,16 @@ static const char NM_SQL_VMS_INSERT_CLONE[] =
"iso, install, usb, usb_status, bios, kernel, mouse_override, "
"kernel_append, tty_path, socket_path, initrd, machine, fs9p_enable, "
"fs9p_path, fs9p_name, usb_type, spice, debug_port, debug_freeze, "
"cmdappend, team, display_type, pflash, spice_agent "
"cmdappend, team, display_type, pflash, spice_agent, '%s', '%s' "
"FROM vms WHERE name='%s'";

static const char NM_SQL_VMS_INSERT_NEW[] =
"INSERT INTO vms(name, mem, smp, kvm, hcpu, vnc, arch, iso, "
"install, machine, mouse_override, usb, usb_type, usb_status, fs9p_enable, "
"spice, debug_port, debug_freeze, display_type, spice_agent) "
"VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', "
"'%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %s)";
"spice, debug_port, debug_freeze, display_type, spice_agent, "
"pid_path, qmp_sock_path) "
"VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', "
"'%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %s, '%s', '%s')";

static const char NM_SQL_VMS_SELECT_ID[] =
"SELECT id FROM vms WHERE name='%s'";
Expand All @@ -97,12 +99,24 @@ static const char NM_SQL_VMS_SELECT_ALL_VNC[] =
static const char NM_SQL_VMS_SELECT_VNC[] =
"SELECT vnc, spice FROM vms WHERE name='%s'";

static const char NM_SQL_VMS_SELECT_QMP_PATH[] =
"SELECT qmp_sock_path FROM vms WHERE name='%s'";

static const char NM_SQL_VMS_SELECT_PID_PATH[] =
"SELECT pid_path FROM vms WHERE name='%s'";

static const char NM_SQL_VMS_SELECT_NAMES[] =
"SELECT name FROM vms ORDER BY name ASC";

static const char NM_SQL_VMS_SELECT_NAMES_BY_TEAM[] =
"SELECT name FROM vms WHERE team='%s' ORDER BY name ASC";

static const char NM_SQL_VMS_SELECT_NAMES_WITH_SOCK[] =
"SELECT name, qmp_sock_path FROM vms ORDER BY name ASC";

static const char NM_SQL_VMS_SELECT_NAMES_WITH_SOCK_BY_TEAM[] =
"SELECT name, qmp_sock_path FROM vms WHERE team='%s' ORDER BY name ASC";

static const char NM_SQL_VMS_SELECT_PROPS[] =
"SELECT if_name, mac_addr, if_drv, ipv4_addr, vhost, "
"macvtap, parent_eth, altname, netuser, hostfwd, smb FROM ifaces "
Expand Down Expand Up @@ -206,6 +220,12 @@ static const char NM_SQL_VMS_UPDATE_9P_NAME[] =
static const char NM_SQL_VMS_UPDATE_USB_STATUS[] =
"UPDATE vms SET usb_status=%s WHERE name='%s'";

static const char NM_SQL_VMS_UPDATE_PID_PATH[] =
"UPDATE vms SET pid_path='%s' WHERE name='%s'";

static const char NM_SQL_VMS_UPDATE_QMP_PATH[] =
"UPDATE vms SET qmp_sock_path='%s' WHERE name='%s'";

static const char NM_SQL_VMS_DELETE_VM[] =
"DELETE FROM vms WHERE name='%s'";

Expand Down Expand Up @@ -494,6 +514,8 @@ enum select_main_idx {
NM_SQL_DISPLAY,
NM_SQL_FLASH,
NM_SQL_AGENT,
NM_SQL_PID,
NM_SQL_QMP,
NM_VM_IDX_COUNT
};

Expand Down Expand Up @@ -530,6 +552,12 @@ enum select_usb_idx {
NM_USB_IDX_COUNT
};

enum select_vms_idx {
NM_SQL_VMS_NAME = 0,
NM_SQL_VMS_QMP,
NM_VMS_IDX_COUNT,
};

enum {
NM_SQL_VMSNAP_NAME = 0,
NM_SQL_VMSNAP_LOAD,
Expand Down
43 changes: 42 additions & 1 deletion src/nm_edit_boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ static const char NM_LC_EDIT_BOOT_FORM_CMDL[] = "Kernel cmdline";
static const char NM_LC_EDIT_BOOT_FORM_INIT[] = "Path to initrd";
static const char NM_LC_EDIT_BOOT_FORM_DEBP[] = "GDB debug port";
static const char NM_LC_EDIT_BOOT_FORM_DEBF[] = "Freeze after start";
static const char NM_LC_EDIT_BOOT_FORM_PIDF[] = "Path to PID file";
static const char NM_LC_EDIT_BOOT_FORM_SOCF[] = "Path to QMP socket";

static void nm_edit_boot_init_windows(nm_form_t *form);
static void nm_edit_boot_fields_setup(const nm_vmctl_data_t *cur);
Expand All @@ -34,6 +36,8 @@ enum {
NM_LBL_INIT, NM_FLD_INIT,
NM_LBL_DEBP, NM_FLD_DEBP,
NM_LBL_DEBF, NM_FLD_DEBF,
NM_LBL_PIDF, NM_FLD_PIDF,
NM_LBL_SOCF, NM_FLD_SOCF,
NM_FLD_COUNT
};

Expand Down Expand Up @@ -114,6 +118,12 @@ void nm_edit_boot(const nm_str_t *name)
fields[n] = nm_field_enum_new(
n / 2, form_data, nm_form_yes_no, false, false);
break;
case NM_FLD_PIDF:
fields[n] = nm_field_regexp_new(n / 2, form_data, "^/.*");
break;
case NM_FLD_SOCF:
fields[n] = nm_field_regexp_new(n / 2, form_data, "^/.*");
break;
default:
fields[n] = nm_field_label_new(n / 2, form_data);
break;
Expand Down Expand Up @@ -181,6 +191,11 @@ static void nm_edit_boot_fields_setup(const nm_vmctl_data_t *cur)
} else {
set_field_buffer(fields[NM_FLD_DEBF], 0, nm_form_yes_no[1]);
}

set_field_buffer(fields[NM_FLD_PIDF], 0,
nm_vect_str_ctx(&cur->main, NM_SQL_PID));
set_field_buffer(fields[NM_FLD_SOCF], 0,
nm_vect_str_ctx(&cur->main, NM_SQL_QMP));
}

static size_t nm_edit_boot_labels_setup(void)
Expand Down Expand Up @@ -218,6 +233,12 @@ static size_t nm_edit_boot_labels_setup(void)
case NM_LBL_DEBF:
nm_str_format(&buf, "%s", _(NM_LC_EDIT_BOOT_FORM_DEBF));
break;
case NM_LBL_PIDF:
nm_str_format(&buf, "%s", _(NM_LC_EDIT_BOOT_FORM_PIDF));
break;
case NM_LBL_SOCF:
nm_str_format(&buf, "%s", _(NM_LC_EDIT_BOOT_FORM_SOCF));
break;
default:
continue;
}
Expand Down Expand Up @@ -252,9 +273,17 @@ static int nm_edit_boot_get_data(nm_vm_boot_t *vm)
nm_get_field_buf(fields[NM_FLD_INIT], &vm->initrd);
nm_get_field_buf(fields[NM_FLD_DEBP], &vm->debug_port);
nm_get_field_buf(fields[NM_FLD_DEBF], &debug_freeze);
nm_get_field_buf(fields[NM_FLD_PIDF], &vm->pid_path);
nm_get_field_buf(fields[NM_FLD_SOCF], &vm->qmp_path);

if (field_status(fields[NM_FLD_INST])) {
nm_form_check_data(_("OS Installed"), inst, err);
nm_form_check_data(_(NM_LC_EDIT_BOOT_FORM_INST), inst, err);
}
if (field_status(fields[NM_FLD_PIDF])) {
nm_form_check_data(_(NM_LC_EDIT_BOOT_FORM_PIDF), vm->pid_path, err);
}
if (field_status(fields[NM_FLD_SOCF])) {
nm_form_check_data(_(NM_LC_EDIT_BOOT_FORM_SOCF), vm->qmp_path, err);
}

if ((rc = nm_print_empty_fields(&err)) == NM_ERR) {
Expand Down Expand Up @@ -343,6 +372,18 @@ static void nm_edit_boot_update_db(const nm_str_t *name, nm_vm_boot_t *vm)
nm_db_edit(query.data);
}

if (field_status(fields[NM_FLD_PIDF])) {
nm_str_format(&query, NM_SQL_VMS_UPDATE_PID_PATH,
vm->pid_path.data, name->data);
nm_db_edit(query.data);
}

if (field_status(fields[NM_FLD_SOCF])) {
nm_str_format(&query, NM_SQL_VMS_UPDATE_QMP_PATH,
vm->qmp_path.data, name->data);
nm_db_edit(query.data);
}

nm_str_free(&query);
}

Expand Down
Loading
Loading