cheatsheet.zwischenspeicher.info

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

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

Mount a partition of a qcow2 (or raw) disk image

There are situations when you need to access a VM's disk / partition directly, e.g to reset a forgotten root password. For QEMU's qcow2 partitions this can be achieved with the "network block device" (nbd) driver and qemu-nbd from the qemu-utils package. (For "raw" disk images, see below.)

First, load the nbd kernel module:

$ modprobe nbd max_part=12

Now use the command qemu-nbd to make the disk image available as network block device:

$ qemu-nbd --connect=/dev/nbd0 /path/to/image.qcow2

fdisk can be used to list the available partitions inside the disk image:

$ fdisk -l /dev/nbd0

Finally mount the partition (in this case partition #2) to an arbitrary mount point:

$ mount /dev/nbd0p2 /path/to/mountpoint/

chroot into the directory as usual. When you're done, exit the chroot, then unmount the partition and disconnect the network block device:

$ umount /dev/nbd0p2
$ qemu-nbd --disconnect /dev/nbd0


Similarly, a partition inside a raw disk image can be mounted as loop device:

$ losetup /dev/loop0 /path/to/diskimage.img

Make the partition mappings available:

$ kpartx -av /dev/loop0

And mount the partition (here partition #1):

$ mount /dev/mapper/loop0p1 /mnt/image

Alternatively, the partition can be mounted directly: In this case its offset (in bytes) from the beginning of the disk image needs to be passed to the mount command, fdisk will provide the necessary values to calculate it.

Here an example disk image with only one partition:

$ fdisk -l netinst.img

Disk netinst.img: 32 MiB, 33554432 bytes, 65536 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xb6bdaf05

Device         Boot   Start     End   Sectors    Size   Id   Type
netinst.img1   *       2048   65535     63488     31M    b   W95 FAT32

The "Start" sector of the target partition (here 2048) needs to be multiplied with the disk's sector size, here 512 bytes. In this case the partition's offset is 512*2048=1048576 bytes:

$ mount -o loop,offset=1048576 netinst.img /path/to/mountpoint/