Hyper-V Debian Stretch Tvheadend

Added by Fred Fred over 5 years ago

This is how I run my Tvheadend Server on Debian Stretch on a Windows 10 Hyper-V virtual machine.
I write how I have done it and the choices I have made, not every choice you can make.
This may be useful if you like my are not to comfortable with *nix systems, however if you are and you find something in here that is not good or correct, please let me know.
Before you start doing things, read trough it as there are some useful info at the end about checkpoints (snapshots).

  • The steps are these:
  • Create Hyper-V Guest
  • Install Debian
  • Config Debian
  • Install Tvheadend
  • Add recordings to Windows PC
  • Info about checkpoints (snapshots) import/export (backup)

Create a machine in Hyper-V as a gen 2 guest, disable secure boot.
The disk size I set as dynamic expanding end a maximum size of 20GB, this leaves enough space for transcoding timeshift ans some local recordings,
larger and longer recordings are stored on a Windows machine containing all my other mediafiles.
Start the machine and stop it, assign it a static mac adress under network adapter advanced features.
Start it and install Debian, DO NOT set a root password and enable ssh and standar utilities at the end of the guide.
I place all my stuff on one partition when it asks for partitioning.

When the setup is done connect to it using SSH and do these steps for configuration.
Enable the NOOP scheduler, using the following commands:
sudo sed -i '/GRUB_CMDLINE_LINUX_DEFAULT/ s/^\(.*\)\("\)/\1 elevator=noop\2/' /etc/default/grub
sudo update-grub
sudo reboot

After reboot make sure brackets have moved from cfq to noop:
cat /sys/block/sda/queue/scheduler
More info:

Some stuff to make Debian run nicer under Hyper-V
sudo apt-get install hyperv-daemons
sudo echo "" >> /etc/initramfs-tools/modules
sudo echo "# Hyper-V Modules" >> /etc/initramfs-tools/modules
sudo echo "hv_vmbus" >> /etc/initramfs-tools/modules
sudo echo "hv_storvsc" >> /etc/initramfs-tools/modules
sudo echo "hv_blkvsc" >> /etc/initramfs-tools/modules
sudo echo "hv_netvsc" >> /etc/initramfs-tools/modules
sudo echo "hv_balloon" >> /etc/initramfs-tools/modules
sudo echo "hv_utils" >> /etc/initramfs-tools/modules
sudo update-initramfs -u
sudo reboot

Set a static IP:
Change to this (if your network is different yous your own stuff):
sudo nano /etc/network/interfaces
And paste this:
auto eth0
iface eth0 inet static

To save: ctrl+o (enter)
To exit: ctrl+x

DNS servers:
(if your network is different yous your own stuff)
sudo nano /etc/resolv.conf
domain workgroup
search workgroup

To save: ctrl+o (enter)
To exit: ctrl+x

If you want to disable IPv6, paste this at the very bottom of the file (this is optional)
sudo nano /etc/sysctl.conf
#Disable IPv6
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
net.ipv6.conf.eth0.disable_ipv6 = 1

To save: ctrl+o (enter)
To exit: ctrl+x
sudo reboot

This installs latest stabel TVH, look at documentation if you want something else
sudo apt-get -y install coreutils wget apt-transport-https lsb-release ca-certificates
sudo wget -qO- | sudo apt-key add -
sudo sh -c 'echo "deb stretch main" | tee -a /etc/apt/sources.list.d/tvheadend.list'
sudo apt-get update
sudo apt-get install tvheadend

And from there it is just like installing tvh on anything and configure it.

As I wrote above I use a Windows machine to store all larger recordings, its a Win10 called mypc and have ip with a share called recordedtv, ther is a local user on it called kodi with the passwork kodi and this user have full permissions on the share and folder on the machine.
I dont want this to be permanently attachaed - so the solution is autofs.

To add recordings to my Windows machine here is how:
sudo apt-get install cifs-utils autofs
sudo mkdir /mnt/mypc
sudo chmod 7777 /mnt/mypc

Add this at the bottom of the file and comment the line +auto.master and paste the info
sudo nano /etc/auto.master
/mnt/mypc /etc/auto.mypc --timeout=60 --ghost

To save: ctrl+o (enter)
To exit: ctrl+x

sudo id hts
Replace uid and gid with what the user hts have on you system and then paste the line
sudo nano /etc/auto.mypc
recordedtv -fstype=cifs,rw,username=kodi,password=kodi,uid=107,gid=111 ://

To save: ctrl+o (enter)
To exit: ctrl+x

Restart it:
sudo /etc/init.d/autofs restart

In TVH webgui enter this as recordingpath to store recordings on you Win PC:

General info...
So now you have a TVH server that is up and running, but if you are like me and feel a bit worried whenever you make changes to it and have no or a very basic understanding of how the machine works and how to troubleshoot it if you make a bad choice or if your hard drive explode... let's use checkpoints and import/export as a backup. Also, during this setup you can use checkpoints anywhere before making any of the changes I suggest, much easier that starting over every time :)
Checkpoint, this is useful if you want to make a small change and you are unsure if it is a good or bad choice. Before you do that change create a checkpoint. If the choice was bad and you want to go back to the way things where just apply the checkpoint, if things where good then just delete the checkpoint. There are great guides and documentation so just google how to use it. It is however very important to NOT leave the checkpoints a long time, a few days are just fine but always try to delete them as soon as you can after creating one.
For longer and regular backups use import and export. You can do this trough the GUI or Powershell, I have created a scheduled task that export my Tvheadend server once a month so that I have a backup of it on a regular interval if i break it or the disk breaks.
I hope you find this guide and info useful :)

Replies (7)

RE: Hyper-V Debian Stretch Tvheadend - Added by Fred Fred over 5 years ago

I did this setup a few months ago and it have been running great. But once tvheadend stopped and once debian stopped for unknown reasons and my *nix knowledge is still very limited so I never did troubleshoot this from the Linux perspective. Both times all I did was restart the virtual machine so I wrote a small monitor to do just that.
So, the Windows machine have a scheduled task that checks if the tvheadend website us up, if it isn't it writes this to the eventlog of the windows machine and shutdown and then starts up the virtual machine.

Here is how to do that (all done on the Windows machine):
Open powershell ise as administrator.
First create the eventlog source for this script, run this command:
New-EventLog –LogName Application –Source “TVH-Monitor”

Create a new powershell script containing this and update the ip of your tvh and the name of you hyper-v machine and save it:
#Variables to set are TVH Website URL and the Hyper-V name of your virtual machine
$tvhwebsite = ""
$tvhvmname = "tvhs"

$x=Invoke-RestMethod -Method Get -Uri $tvhwebsite -ea stop -TimeoutSec 5
Write-EventLog -EntryType Error -LogName Application -Source “TVH-Monitor” -EventId 9981 -Message "TVH Website is DOWN - Shutdown and Startup $tvhvmname"
Stop-VM -Name $tvhvmname -Force
while ((get-vm -name $tvhvmname).state -ne 'Off') { start-sleep -s 5 }
Start-VM -Name $tvhvmname

Create a scheduled task that runs the script at the disered interval, I run mine every thirty minutes. Thats it, you are now monitoring you tvheadend from Windows :)

RE: Hyper-V Debian Stretch Tvheadend - Added by René R. over 4 years ago

THX for the nice guide!

I really want to run TVHeadend as backend for all my Kodi clients because only TVHeadend can handle "in progress recordings" as I want.
The main server is Windows based.

Are there any possibilities to wake the Windows server (VM host) for scheduled recordings of the TVHeadend-Server (VM)? Maybe an export tool from the TVH recordings to the Windows tasks?
A totally switch from my perfect running Windows server to a native Linux isn't an option for me.


RE: Hyper-V Debian Stretch Tvheadend - Added by Fred Fred over 4 years ago

I'm glad you liked it :)

Well there actually is a way to do it but I'm not really sure exactly how to, maybe you can do some digging and figure it out?

You can get recordings from the REST api with something like this:
(replace username:password: with ip of you tvh)
$response = Invoke-RestMethod 'http://username::9981/api/dvr/entry/grid_upcoming?sort=start'
I'm not very familiar with rest and stuff like that so I'm sad to say that is about what I know... also, what will wake the Windows machine up?
Depending on hardware and that config of you Win machine I'm not sure a scheduled task can actually wake a computer from sleep/hibernate/poweredoff state?

(updated instructions in original post above, leave corrections for people who find this..)
On a not totally related note, this guide may fail... there have been problems with the source for installation, more info here:
So the part "This installs latest stabel TVH" needs to be updated due to the repo change...
If you already have tvh installed using this guide do this:
sudo sh -c 'echo "deb stretch main" | tee -a /etc/apt/sources.list.d/tvheadend.list'
Then sudo nano /etc/apt/sources.list
And remove the line for site.

Happy computing! :)

RE: Hyper-V Debian Stretch Tvheadend - Added by Walter av over 4 years ago

When running Tvheadend on baremetal, there is a working shutdown/wake script for scheduled recordings by @Mr Rooster in this forum (not really on topic yet about a hyper v host/guest relation) but its a start.

I have enhanced this [url=""]script[/url] for suspend and described a step by step suspend/rtc wake system analyses for debian/ubuntu on bare metal (again not tested hyperv guest). Its just a answer to Fred Fred Fred its question "...I'm not sure a scheduled task can actually wake a computer from sleep/hibernate/poweredoff state..." that its perfectly possible to let Tvheadend get woken by its own RTC alarm for a scheduled recording. In that same post in the end I posted a way how a Raspberry Pi with Raspbian and Kodi can WOL the system for just Adhoc viewing, or watchback/schedule a recording.

But back again to Rene Herbrich R. usecase "let a sleeping hyperV vm host gets woken by its sleeping guest..." challenging. I guess if M$ really did a good job on HyperV host design, it should detect the RTC events set by the Guest machine and sleep/wake x-minutes/x-seconds before one of the guest machines would go to sleep/wake. Also the wake on lan mac address of the guest must be the same as host, maybe possible with bridging, or the raspberry must sent to Wake on Lan events first one for the HyperV host and MAC address second for the HyperV linux guest MAC address?

Whatever you do, I think you first have to tackle HyperV linux guest with suspend while the HyperV host keeps on, from there you can advance.

Good Luck!

RE: Hyper-V Debian Stretch Tvheadend - Added by Fred Fred over 4 years ago

The way I think this must be done is the physical computer reading tvh recordings trough rest like I wrote above and create a scheduled task to wake itself, or let another device wake it like @Walter suggest.
The script itself could be a scheduled task triggered by an event, like the physical computer shutting down or going to sleep, to add another schedule task to wake it a few minutes before it's time to record.

RE: Hyper-V Debian Stretch Tvheadend - Added by alexandre mendes over 4 years ago

Hello everyone, could anyone send a step by step tutorial to pop tvheadend on ubuntu 14.04

RE: Hyper-V Debian Stretch Tvheadend - Added by saen acro over 4 years ago

alexandre mendes wrote:

Hello everyone, could anyone send a step by step tutorial to pop tvheadend on ubuntu 14.04