Category: Computers

Setup grub on new drive

I always figure this out from scratch each time, this is just a doc of quick copy/paste to make it happen

NEW_DRIVE=/dev/disk/by-label/linux
MNT_POINT=/mnt/temp
sudo mount $NEW_DRIVE  $MNT_POINT

# To copy current OS onto new drive
sudo rsync -a -v -h -P --delete -x / $MNT_POINT/

sudo mount -o bind /dev $MNT_POINT/dev
sudo mount -t proc none $MNT_POINT/proc
sudo mount -t sysfs none $MNT_POINT/sys
sudo mount -t devpts none $MNT_POINT/dev/pts

sudo chroot $MNT_POINT /usr/bin/env NEW_DRIVE=$NEW_DRIVE /bin/bash -l
PART=`realpath $NEW_DRIVE`
DRIVE=`echo $PART | perl -pe 's/^(.*?)[0-9]$/$1/'`
sudo grub-install $DRIVE
sudo update-grub

exit
for m in $MNT_POINT/{dev/pts,dev,proc,sys,}; do sudo umount $m; done
# DONE

Running XBMC and Mythfrontend seamlessly on the same machine

I want to run both XBMC and Mthfrontend on my htpc’s. I record a lot of free to air, and XBMC’s PVR plugins are still just too far away from myth’s functionalty, however xbmc is better with some media/streaming applications. So I’d like to switch between the two when needed.

XBMC has a issue however where in fullscreen it blocks keystroked going to anything else, including the window manager, so some extra help is required to switch from a fullscreen xbmc back to mythtv.

Bring on chvt, a command line tool that will let you switch virtual terminals!

I decided it’s best for me to keep xbmc on a separate xserver, which can be separately managed. Then behind the scenes use chvt to switch between the two x servers.

Firstly, it will need an init script to start xbmc on startup. I use linux mint so it’s upstart all the way for startup scripts

/etc/init/xbmc

# xbmc-upstart
# starts XBMC on startup by using xinit.
# by default runs as xbmc, to change edit below.
env USER=corona

description "XBMC-barebones-upstart-script"
author "Andrew Leech, based on script by Matt Filetto"

start on ((filesystem
           and runlevel [!06]
           and started dbus
           and plymouth-ready)
          or runlevel PREVLEVEL=S)
stop on runlevel [016]

# tell upstart to respawn the process if abnormal exit
respawn

script
  exec su -c "xinit /usr/bin/xbmc-standalone -- :1 -nocursor" $USER
end script

Switching back and forth

I wrote myself a nice python daemon to switch between the two.
https://gitlab.alelec.net/corona/switch_desktop_macro/tree/master

On debian/ubuntu (upstart) copy switch-destop-macro.conf to /etc/init
Edit it to ensure the path to the main script is right, then either reboot or start it:

sudo service switch-destop-macro start

It’ll run as a daemon watching for the defined keystrokes and then switching back and forth between xbmc and myth. The keystrokes already in the script correspond to convenient keys on my multiple remotes, one of which is \

It’ll log keystrokes (in X) to /var/log/upstart/switch-desktop-macro.log which is a massive security hole, so you should disable the logging once you’ve used it to figure out the keystrokes you want as your macro.

Cursor Problem

Then I had problems with dead cursors in the middle of my mythfrontend x session when I switched back to it. Best method I’ve found to fix it so far is to simply disable the corsor altogether, by adding the -nocursor arg to xserver config.

/etc/mdm/mdm.conf : added to bottom:

[server-Standard]
name=Standard server
command=/usr/bin/X -audit 0 -nocursor
flexible=true

Audio Sharing

So, by default mythfrontend will control either the Master or PCM (alsa) audio mixer control. XBMC on the other hand ignores hardware mixers and changes the volume in software.
This means if you turn the volume down in myth, it limits the max volume in xbmc to the new level in myth. Not a good result.
Mythtv does have a built in software volume control however in my experience it really damages the quality of the audio.

Also, if one of the two applications holds open a normal alsa device, it means the other can’t get to it.

I should probably go the route of trying to get pulseaudio working for me, although I always end up finding it working against me. To date I’ve gone down a more convoluted path with alsa

Trick 1: use a dmix alsa device for both programs so they can share the audio
Trick 2: create an alsa softvol device for mythtv so you can leave the hardware mixer at full
Trick 3: map a new fake Master or PCM mixer to the softvol so mythtv will use it by default

The third one is the most hit and miss to set up as it required you to have a hardware alsa device that’s missing either Master or PCM, such that we can map softvol to it, as you can’t override an exiting volume slider and I couldn’t get myth to use anything other than a Master or PCM control. I also haven’t found any way to add a new fake control device to alsa to attach the slider too. Luckily so far my motherboards have generally had a separate hw device with only a couple of digital out volumes on them to use.

To check run alsamixer, hit F6 and go through each of the devices listed to see if one of them is missing Master and/or PCM

Here’s my ~/.asoundrc

pcm.!default {
        type plug
        slave.pcm "dmixer"
}

pcm.default {
        type plug
        slave.pcm "dmixer"
}

pcm.dmixer  {
        type dmix
        ipc_key 1024
        slave {
                pcm "hw:CARD=0,DEV=0"
                period_time 0
                period_size 1024
                buffer_size 4096
                rate 44100
        }
        bindings {
                0 0
                1 1
                }
}

ctl.dmixer {
        type hw
        card 0
}

pcm.softvol {
        type softvol
        slave {
                pcm "dmixer"
        }
        control {
                name "Master"
                card 1
        }
}

This creates a dmix device which sits over card 0, my default analog out card as seen in alsamixer.
This dmix gets set to the default output, xbmc uses it seamlessly then.
I also make a softvol device which sits over the dmix, with it’s control called Master and attached to card 1, which was missing a Master control.
Mythfrontend is then configured manually to use the softvol device, a keyboard helps here:

  1. Go to Settings -> Audio
  2. In Audio output device type:
    ALSA:softvol
  3. I have all the dolby checkboxes off and Speaker configuration set to Stereo as that’s how I roll, if you’ve got
    surround you’ll have to modify settings accordingly. I haven’t changed and of the Advanced Audio Settings behind the button half way down the screen.
  4. Go to Next
  5. Ensure “Use internal volume controls” is enabled
  6. Set “Mixer device” (change card number to match that in the softvol ctrl section):
    ALSA:hw:CARD=1
  7. Set “Mixer control” (change to name set in softvol control):
    Master

That’s it, hopefully it’ll work for you. I tried Mixer control with arbitrarily named controls and it simply did not work at all, I had to stick to one of the two listed names. Probably should fix that in source and pull request it, however my current solution works for me even if it’s pretty hacky.

Cheap HID remote on linux – Sapphire Driver

I’ve got a cheap USB remote that acts as a HID keyboard, with the key presses all hard coded to MCE suitable settings. Unfortunately these don’t all work well with Linux and mythtv in particular.

I found a replacement HID driver for remotes here: http://rtr.ca/sapphire_remote/

I had to modify it to recognise my remote instead, and also figured out how to automatically attach the driver rather than the normal hid one when plugging the remote in / boot.

Driver

My remote has completely different keycodes to the sapphire one, so I modified the driver to print the keycodes to syslog of any button press that’s not recognised. In this way I can have the driver attached and running, but pressing buttons will simply print the codes. I can then modify a copy of the driver to add the codes to the lookup table, every now and again recompiling the driver to check that they’re working. I haven’t actually mapped all the keys on my remote, just the ones I want to use.
I also added the HOLTEK’s vid/pid to the driver to recognise my remote.

The makefile automatically handles reinstalling the driver and resetting the remote, so every time you run ‘make install’, if the remotes already attached to the driver, it gets the updated compiled driver.

sapphire-al-4.4.tbz

Driver Attach Script

This is a modified version of the original one that searches for any HID device with HOLTEK as it’s name and detaches the hid driver. It then modprobes the hid_sapphire driver which will automatically attach the the remote.

[snip Driver_Attach_Script][/snip]

UDEV

I couldn’t find a way to directly override the driver to use when the USB remote was installed, but I can get it to automatically run a script on insertion, so I get it to run the above driver attach script. It appears to work flawlessly, both on random unplug/replug of the usb, as well as aleep/wake and reboots!

/etc/udev/rules.d/99-holtec-saphire.rules

[snip udev_rule][/snip]

 

Getting nxserver running

So I’m liking the features of nomachine nxserver preview 4 but it takes more effort than expected to get it running.

They currently don’t allow upgrades, so if you’ve installed previous version, uninstall first

sudo apt-get remove nxserver

then install

dpkg -i nxserver_4.0.181-7_i386.deb

First problem, their private key has the wrong permissions:


chmod 600 /usr/NX/share/keys/server.id_dsa.key
chmod 600 /usr/NX/home/nx/.ssh/*

chown nx:root /usr/NX/home/nx/.ssh/*

And I use AllowGroups in sshd_config, to which I added a group nx, then made the group

addgroup nx
addgroup nx nx

Then you have to add your login user

/usr/NX/bin/nxserver --useradd <username> --system
/usr/NX/bin/nxserver --useradd <username> --administrator

And you should replace the http ssl keys

cd /usr/NX/etc
openssl req -newkey rsa:2048 -days 3650 -x509 -nodes -out root.cer

The Common Name is the only important entry, should be the same as domain name

mv httpd.key httpd.key.orig
mv httpd.crt httpd.key.crt
openssl req -newkey rsa:1024 -nodes -out httpd.csr -keyout httpd.key
openssl x509 -req -days 3650 -in httpd.csr -signkey httpd.key -out httpd.crt

Then you should be able to start

/etc/init.d/nxserver restart

Also of note, the default login for Server Manager is nxmanager:nxmanager

Domain controller can’t see me after wakeup

I hibernate my pc’s each night, generally works well.

Woke them up this morning, windows box doesn’t have any synergy mouse/keyboard, can’t log in. after much switching keyboard back and forth between the two, I find windows box can’t resolve linux box by name. The domain controller is happily resolving the windows box, linux can see it, but not vice versa.

Eventually found that if I get linux box to do a smb lookup all of a sudden it reminds the domain controller I’m here and it’ll resolve linux box again no worries. So I’ll now make sure that happens automatically on each wakeup:

# sudo nano -w /etc/pm/sleep.d/wakeup_domain_ping

#!/bin/bash

# I’ve found that sometimes on wakeup the domain controller doens’t realise I’m here…
# so windows box can’t see me by name and therefore doesn’t have any working mouse/keyboard from synergy

case $1 in
suspend|suspend_hybrid|hibernate)
# No need to do anything here
:
;;
resume|thaw)
# “Ping” domain controller
smbclient -L andrewlin
;;
esac

# sudo chmod +x  /etc/pm/sleep.d/wakeup_domain_ping

Happy days.

Streaming system audio from windows to another pc (linux)

So I want to hear what’s going on with my secondary (windows) pc on my headphones (connected to ubuntu).

Get system audio

My onboard realtek 888 does list ‘Stereo Mix’ as a recording option but it was always silent.
So, tried various virtual sound cards, they always either didn’t work/didn’t appear as a recording device, or they just cost more than I want to spend compared to running out and getting a 3.5mm audio cable and looping speaker plug to line-in.

Finally stumbled upon a suggestion: http://www.sevenforums.com/sound-audio/149117-creative-xfi-xtreme-problem.html#post1278957

How to fix Realtek ‘Stereo Mix’ silence? Select Speakers as your default playback device!

Next problem, I can’t select Loudspeaker unless something is plugged into that port because of the port sensing. The Realtek HD Audio Manager on my pc (asus re-branded realtek app) does not have any options to turn off jack sensing, so lets go deeper:
http://thextramoney.wordpress.com/2010/05/16/how-to-disable-jack-detection-on-windows-7/
Regedit -> HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E96C-E325-11CE-BFC1-08002BE10318}\0000\GlobalSettings\EnableDynamicDevices  = 0
And reboot. Still Jack sensing. Kept looking in regedit, the 0000 folder was just one audio driver on the system, the rest of the numbered folders after it (0001, 0002….) were for other drivers that had ever been installed for any sound device. I found another realtek HD one at 0018 and changed the setting there. Reboot. Still wont let me enable Loudspeaker.

Keep hunting…. in:
Regedit -> HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E96C-E325-11CE-BFC1-08002BE10318}\0018\Settings\
there’s a setting ‘JackCtrl’ which I decided to start experimenting with. It was originally set to ” 3f e7 00 00 “. First trial, set to ” ff ff 00 00 ” and reboot. All of a sudden in playback devices I’ve got a “Realtek HD Audio 2nd output”. Looking into it I think it’s the front port, and with the previous setting jack sensing on ti is disabled, so I can select it. That doesn’t help though, as stereo mix still doesn’t get sound.

I eventually give up on finding a software fix, and instead found a short piece of plastic rod, ~ 3mm diameter, and shoved it in the speaker jack. Windows sees it, lets me enable Speakers as playback, job done. Set Speakers as playback default and stereo mix as recording default and we’re ready to stream!

Another option that could be investigated: WASAPI
http://blogs.msdn.com/b/matthew_van_eerde/archive/2008/12/16/sample-wasapi-loopback-capture-record-what-you-hear.aspx
http://naudio.codeplex.com/discussions/203605

Stream the audio

For my initial setup I used Vacard Streamer 2.0 that came with “Vacard 2.b1″. It’ll let me stream from system recording device to a rtp location. Just set the target to rtp://x.x.x.x:4444 where x.x.x is ip or resolvable hostname of linux box. It’ll happily stream as mp3 to reduce bandwidth.

I’ve since switched to using vlc to stream from the pc, started from a batch file.

start /b vlc.exe dshow:// :dshow-vdev=none :dshow-adev=”Line In (High Definition Audio Device)” :dshow-caching=20 :live-caching=50 :sout=#transcode{vcodec=none,acodec=mp3,ab=192,channels=2,samplerate=44100}:rtp{dst=alelec.local,port=5004,mux=ts,sap,name=Radio,ttl=1} :sout-mux-caching=100 :sout-rtp-caching=100 :sout-all :sout-keep

The dshow-adev line is correct for Windows 8, but in Windows 7 I needed it to ba called “Stereo Mix (2- Realtek High Def”

The name can be found initially by running vlc normally and going to file->open capture device

Select the desired audio device in the the drop down box next to Audio Device Name, and then click the “show more options” checkbox down the bottom of the screen. This then gives a printout of the command line name to user.

 

On linux (or mac) box I’ve got vlc running from the console in the background:
# vlc -I telnet –telnet-password vlc rtp://@:5004 –volume 128 &
And it faithfully plays whatever is streamed to it!

Synergy – great software

I’m used to having virtual machines to do multiple os’s, but now at work there’s a spare machine laying around, so on goes linux to use as main pc. This way when windows blue screens as it does far too often, I can keep working on the linux box. Happy Days, higher productivity.

But, I don’t want multiple keyboards/mice.
I also don’t want a physical KVM, expensive, manual process.

Enter Synergy, like having linux in a virtual pc on the second screen, mouse and keyboard moves flawlessly between them.

Went straight to their beta release 1.4.6, works great on Ubuntu 11.4 (as server).
Once installed deb from http://synergy-foss.org/download/
Go ‘One Button’ or ‘Dash Home’ or whatever it’s called, type Startup Applications. Open it. Then ‘Add’, name=synergy, command=synergy, Save. Logout, Login, easy. Should be in the top menu now ans a little grene/blue circle.

On windows-7 x64, I need it as a service or I can’t login (to domain) at boot. Service mode didn’t want to work by default for me, it’d start, conenct, but not mouse/keyboard working.
Use the launcher to put in server settings and hit test button to make sure it works. This did for me. Then use the Autostart button add ‘When Computer Starts’ to install the service.
Read to set all applications in the folder to Administrator (ie right clock, properties -> Compatibility -> Run this program as Administrator) but not sure if that’s really needed.
The main thing that fixed it was changing the service parameters in regedit.

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Synergy Client\Parameters]
“CommandLine”=” –no-daemon –reload –debug DEBUG1 –name MELDSK2006 ANDREWLIN:24800″

Adding the –reload is what fixed it for me. Stop-Start the service in service.msc and it was away!