Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Host rtc sync #143

Open
wants to merge 2 commits into
base: celadon/r/mr0/stable
Choose a base branch
from
Open
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
63 changes: 5 additions & 58 deletions scripts/guest_time_keeping.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
# QMP RTC_EVENT JSON Sample:
# {"timestamp": {"seconds": 1585714481, "microseconds": 85991}, "event": "RTC_CHANGE", "data": {"offset": -1}}

# QMP RTC_ALARM JSON Sample:
# {"timestamp": {"seconds": 1593413313, "microseconds": 568267}, "event": "RTC_ALARM", "data": {"expire": 1593499713567502000}}

# If guest time is set earlier than the image build time, the guest time will become the image build time after
# reboot

set -eE

#------------------------------------ Global Variables -----------------------------------------
QMP_PIPE=$1
RTC_MONITOR=$2

#------------------------------------ Functions -----------------------------------------
function send_qmp_cmd() {
Expand Down Expand Up @@ -66,63 +70,6 @@ function connect_qmp_pipe() {
fi
}

function update_host_date() {
local offset=$1

[[ $offset -eq 0 ]] && return

echo "Guest time changed"
local old_time=$(date)
date -s "$offset seconds"
local updated_time=$(date)

if [ $? = "0" ]; then
hwclock --systohc
echo "Host time is set from \"$old_time\" to \"$updated_time\""
else
echo "Fail to set host time"
fi
}

function monitor_guest_rtc_change() {
if [[ -z $1 ]]; then
echo "E: Empty QMP pipe"
return -1
fi

local qmp_pipe_out=$1".out"
if [[ ! -p $qmp_pipe_out ]]; then
echo "E: Not named pipe: $qmp_pipe_out"
return -1
fi

local pout
while true; do
if [ ! `pgrep qemu-system` ]; then
echo "E: Guest is not alive!"
return -1
fi

read pout < $qmp_pipe_out
local event=$(jq -r .event <<< "$pout")

case $event in
RTC_CHANGE)
local offset=$(jq -r .data.offset <<< "$pout")
update_host_date "$offset"
;;
SHUTDOWN)
return
;;
*)
continue
;;
esac

sleep 1
done
}

function create_pipe() {
[[ -z $1 ]] && return
[[ -p $1".in" ]] || mkfifo $1".in"
Expand All @@ -132,4 +79,4 @@ function create_pipe() {
#------------------------------------ Main process -----------------------------------------
create_pipe "$QMP_PIPE" || exit -1
connect_qmp_pipe "$QMP_PIPE" || exit -1
monitor_guest_rtc_change "$QMP_PIPE"
exec "$RTC_MONITOR" "$QMP_PIPE.in" "$QMP_PIPE.out"
2 changes: 2 additions & 0 deletions scripts/setup_host.sh
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ function prepare_required_scripts(){
chmod +x $CIV_WORK_DIR/scripts/findall.py
chmod +x $CIV_WORK_DIR/scripts/thermsys
chmod +x $CIV_WORK_DIR/scripts/batsys
sudo chown root $CIV_WORK_DIR/scripts/guest_rtc_monitor
sudo chmod u+s $CIV_WORK_DIR/scripts/guest_rtc_monitor
}

function install_auto_start_service(){
Expand Down
17 changes: 14 additions & 3 deletions scripts/start_civ.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ EMULATOR_PATH=$(which qemu-system-x86_64)
GUEST_MEM="-m 2G"
GUEST_CPU_NUM="-smp 1"
GUEST_DISK="-drive file=$WORK_DIR/android.qcow2,if=none,id=disk1,discard=unmap,detect-zeroes=unmap"
GUEST_EXTERNAL_WAKEUP=
GUEST_FIRMWARE="-drive file=$WORK_DIR/OVMF.fd,format=raw,if=pflash"
GUEST_DISP_TYPE="-display gtk,gl=on"
GUEST_KIRQ_CHIP="-machine kernel_irqchip=on"
Expand Down Expand Up @@ -584,9 +585,10 @@ function set_guest_pm() {

function set_guest_time_keep() {
local guest_time_keep_daemon=$SCRIPTS_DIR/guest_time_keeping.sh
if [ -f $guest_keep_daemon ]; then
local guest_time_keep_rtc_daemon=$SCRIPTS_DIR/guest_rtc_monitor
if [ -f $guest_keep_daemon ] && [ -f $guest_time_keep_rtc_daemon ]; then
local guest_time_keep_pipe=$WORK_DIR/qmp-time-keep-pipe
$guest_time_keep_daemon "$guest_time_keep_pipe" &
$guest_time_keep_daemon "$guest_time_keep_pipe" "$guest_time_keep_rtc_daemon" &
GUEST_TIME_KEEP="-qmp pipe:$guest_time_keep_pipe"
fi
}
Expand Down Expand Up @@ -617,6 +619,10 @@ function set_guest_pwr_vol_button() {
cd -
}

function enable_external_wakeup_mode() {
GUEST_EXTERNAL_WAKEUP='-global mc146818rtc.external_wakeup=on'
}

function cleanup() {
cleanup_rpmb_dev
cleanup_thermal_mediation
Expand Down Expand Up @@ -662,6 +668,7 @@ function launch_guest() {
$GUEST_WIFI_PT_DEV \
$GUEST_PM_CTRL \
$GUEST_TIME_KEEP \
$GUEST_EXTERNAL_WAKEUP \
$GUEST_QMP_PIPE \
$GUEST_POWER_BUTTON \
$GUSET_VTPM \
Expand Down Expand Up @@ -704,7 +711,7 @@ function show_help() {
printf "\t--thermal-mediation enable thermal mediation.\n"
printf "\t--battery-mediation enable battery mediation.\n"
printf "\t--guest-pm-control allow guest control host PM.\n"
printf "\t--guest-time-keep reflect guest time setting on Host OS.\n"
printf "\t--guest-time-keep reflect guest RTC settings on Host OS.\n"
printf "\t--qmp-pipe specify the name of the pipe used for qmp communication.\n"
printf "\t--allow-suspend option allow guest enter S3 state, by default guest cannot enter S3 state.\n"
printf "\t--disable-kernel-irqchip set kernel_irqchip=off.\n"
Expand Down Expand Up @@ -811,6 +818,10 @@ function parse_arg() {
set_guest_time_keep
;;

--external-wakeup-mode)
enable_external_wakeup_mode
;;

--allow-suspend)
allow_guest_suspend
;;
Expand Down