cheatsheet.zwischenspeicher.info

Some tech documentation and snippets, finally organized.
Posts tagged as shell

Mount an USB connected Android based smartphone into Linux

​User-mountable and world-readable, using fuse and jmtpfs:

Install required software:

$ apt-get install fuse jmtpfs usbutils

Connect the smartphone with an USB data cable and read out required information:

$ lsusb

The command returns a list of all connected USB devices; the line for my Moto G4 play looks like this:

Bus 001 Device 003: ID 22b8:2e82 Motorola PCS

Now use the printed Vendor and Product ID (before resp. after the colon) to create the according udev rule:

$ echo 'SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", ATTR{idProduct}=="2e82", SYMLINK+="libmtp-%k", MODE="660", GROUP="audio", ENV{ID_MTP_DEVICE}="1", ENV{ID_MEDIA_PLAYER}="1"' >> /etc/udev/rules.d/55-android.rules

Create mountpoint, make sure that you are in the audio group and test setup:

$ mkdir /media/motomobil
$ chmod a+rwx /media/motomobil/
$ jmtpfs -o allow_other /media/motomobil/
$ fusermount -u /media/motomobil/

jmtpfs will grab the first available device. Allternatively, the device can be specified with the -device=<busLocation>,<devNum> switch, to dump the parameters, run jmtpfs --listDevices.

Add fstab entry:

$ echo "jmtpfs       /media/motomobil       fuse       noauto,nodev,allow_others,rw,user,noatime       0       0" >> /etc/fstab

Burning a mixed-mode CD-R

It is possible to "enhance" an audio CD with arbitrary files as a bonus track. The following post shows how to create such a mixed-mode CD or enhanced CD on the command line.

Create suitable wav files with libav-tools

$ cd $AUDIOSOURCEDIR
$ for i in $(ls); do avconv -i $i /path/to/outdir/$i.wav; done

Note: ffmpeg will work as well, with the same syntax and options: just /avconv/ffmpeg/.

Burn the audio-tracks

$ cd $WAVDIR
$ wodim -v dev=/dev/sr0 speed=4 -multi -audio *.wav

The -multi switch (for multi session) is necessary to prevent wodim from closing the disk after burning. Additionally, I use the slow speed=4 option to burn sharper delimited dots and thus enhance the disk's readability in older (or dusty) audio players.

Analyze the audio-session and prepare the "bonus track"

$ TRACKINFO=`cdrecord -msinfo dev=1,0,0`
$ echo $TRACKINFO
  0,105248

In this case, the first session started at sector 0 and the following will start at sector 105248. This information can now be passed to mkisofs or the newer xorrisofs:

$ xorrisofs -J -r -C $TRACKINFO -V $16_CHAR_DISKLABEL -o /path/to/data.iso /path/to/data

Append the iso to the disk

$ wodim -v dev=/dev/sr0 -data /path/to/data.iso

The resulting CD will serve the first session to all common audio CD players, while the second (data) session can be accessed with a CD-ROM drive.

Note: Not all CD-ROM drives support mixed-mode media - some may fail to access the audio tracks from the first session.

CLI Wifi setup with iw

This is a quick overview of the necessary commands and configuration to connect to wireless networks from the command line with iw and wpa_supplicant.

Preparation

Identify available Wifi adapters:

$ iw dev

Check the device status (from now on using device wlan0):

$ ip link show wlan0

Bring up the interface and scan for available networks:

$ ip link set wlan0 up
$ iw wlan0 scan

Now there are several possibilities to actually connect to the targeted wireless network:

Solution 1 - manually with wpa_supplicant and dhclient

Generate the wpa_supplicant configuration file (replace $SSID with the network's SSID, hit return and enter the passphrase):

$ wpa_passphrase $SSID >> /etc/wpa_supplicant.conf

Note: You might want to remove the (commented out) clear text passphrase from the resulting wpa_supplicant.conf file.

Connect to the wireless network and obtain an IP address:

$ wpa_supplicant -B -D wext -i wlan0 -c /etc/wpa_supplicant.conf
$ dhclient wlan0

Solution 2 - using wpa_supplicant with ifup/down

Add the Wifi configuration to your /etc/network/interfaces (of course $SSID and $PASSPHRASE have to be replaced by the actual values):

### File: /etc/network/interfaces

auto wlan0
iface wlan0 inet dhcp 
    wpa-ssid $SSID
    wpa-psk  $PASSPHRASE

To avoid the clear text passphrase in your interfaces file, you can run

$ wpa_passphrase $SSID

As before, you'll have to replace $SSID with the network's real SSID, hit return and enter the passphrase. When done, replace the $PASSPHRASE in the above /etc/network/interfaces file with the resulting 256bit value of psk.

Now bring up the interface, connect to the wireless network and obtain an IP address as usual with

$ ifup wlan0

Note: This solution will (try to) automatically connect to the configured wireless network when the system boots.

Checking the link

Finally, use ip or ifconfig to check the link status and ping a known host to verify connectivity:

$ ip addr show wlan0
$ ifconfig wlan0
$ ping -c 2 devuan.org

Create a bootable FreeDOS medium from within Linux to flash your BIOS

The quick and dirty way is to simply dd a DOS floppy image to an empty USB drive. I use the FreeDOS FDOEM.288 image from fdos.org, because it contains barely anything but kernel and COMMAND.COM, so there is enough space left for the payload.

Note: All data on the drive will be lost.

$ dd if=/path/to/FDOEM.288 of=/dev/sdX

Now mount the USB drive and add the updater and flashrom binaries. That's it.


If you want to flash a computer that neither has a floppy drive nor can't boot from USB, you can create a CD-ROM from the floppy disk image. Mount the FDOEM.288 image and add the updater and flashrom binaries to the image. Working directory is the directory containing the floppy disk image:

$ mount -o loop -t msdos FDOEM.288 /mnt

$ cp /path/to/updater /path/to/firmware /mnt/

$ umount /mnt

genisoimage creates a bootable ISO9660 image, which will then be burned to CD-ROM with wodim:

$ genisoimage -o fdoem288.iso -b FDOEM.288 FDOEM.288

$ wodim -v dev=/dev/cdrw fdoem288.iso

If you need more space than the approximately 2.5MB available in the FDOEM.288 image, or a MS Windows environment, you might want to boot a copy of BartPE in a virtual machine. From within that, you can easily create a custom BartPE boot CD or even a multi purpose / emergency hard disk.