January 18

Linux: Copy GPT partition table with dd

I recently had to copy the partition table of a 3TB disk in a situation where tools such as sfdisk could not be installed.

Since GPT table length is dependant on the number of partitions, you need to do some investigation.

In this case, it was a ‘QNAP’ server that had fdisk (no GPT support) and parted.

On a working drive, run

parted -ms /dev/sda print

Note the number of partitions.

Formula = (128*N)+1024

Where N is the number of partitions you have. In this case I had 4, so I end up with a value of 1536

dd if=/dev/sda of=GPT_TABLE bs=1 count=1536

You now have a backup of a valid partition table you can apply to another drive

dd if=GPT_TABLE of=/dev/sdb bs=1 count=1536

partprobe /dev/sdb

Once this was done, you can manually re-add the drive.

mdadm –manage /dev/md0 –add /dev/sdb3

If you are wondering how we determined the sd[a-z], we accomplished this through hot-swapping the drive to generate logs indicating the drive.

Now why this supposedly automated RAID product required this…

From: linuxadministration.us

Category: Linux | Comments Off on Linux: Copy GPT partition table with dd
November 5

Linux: ASUS BT400 Bluetooth adapter issue

Ubuntu 13.04 was unable to see the adapter

Found this solution on Launchpad which works:

sudo su –

modprobe -v btusb

echo “0b05 17cb” >> /sys/bus/usb/drivers/btusb/new_id

By: Avrono

Category: Linux | Comments Off on Linux: ASUS BT400 Bluetooth adapter issue
November 2

Linux: Reset a USB device from the command line

Put the following into a file called usbreset.c:

/* usbreset -- send a USB port reset to a USB device */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>

#include <linux/usbdevice_fs.h>


int main(int argc, char **argv)
{
    const char *filename;
    int fd;
    int rc;

    if (argc != 2) {
        fprintf(stderr, "Usage: usbreset device-filenamen");
        return 1;
    }
    filename = argv[1];

    fd = open(filename, O_WRONLY);
    if (fd < 0) {
        perror("Error opening output file");
        return 1;
    }

    printf("Resetting USB device %sn", filename);
    rc = ioctl(fd, USBDEVFS_RESET, 0);
    if (rc < 0) {
        perror("Error in ioctl");
        return 1;
    }
    printf("Reset successfuln");

    close(fd);
    return 0;
}

The run the following commands in terminal:

  1. Compile the program:
    $ cc usbreset.c -o usbreset
    
  2. Get the Bus and Device ID of the USB device you want to reset:
    $ lsusb
    Bus 002 Device 003: ID 0fe9:9010 DVICO
    
  3. Make our compiled program executable:
    $ chmod +x usbreset
    
  4. Execute the program with sudo privilege; make necessary substitution for <Bus> and <Device> ids as found by running the lsusb command:
    $ sudo ./usbreset /dev/bus/usb/002/003
    
By: Li Lo
Category: Linux | Comments Off on Linux: Reset a USB device from the command line
October 25

Linux: auto mount cryptsetup/LUKS/encrypted loop device

apt-get install cryptsetup

Using dm-crypt/LUKS/cryptsetup by doing the following steps:

1. Create a sparse disk image file: dd if=/dev/zero of=IMAGEFILE bs=1 count=1 seek=SIZE
2. Generate a random key in a file: dd if=/dev/random of=KEYFILE bs=1024 count=1
3. Use cryptsetup luksFormat –key-file KEYFILE –cipher aes-xts-plain –size 512 IMAGEFILE (Or use another cipher and key length. Note that 512 here will give you AES with 256 bits because of XTS.) Example 2: cryptsetup luksFormat –key-file KEYFILE –cipher aes-xts-plain64:sha512 -s 512 IMAGEFILE
4. Open crypt container: cryptsetup luksOpen –key-file KEYFILE IMAGEFILE NAME
5. Create file system on /dev/mapper/NAME.
6. Mount file system as usual.

Your script would just have to do steps 4 and 6. For unmounting/closing, umount the file system and call cryptsetup luksClose NAME.

Note that using sparse files will leak the information which sectors of the disk have been used already (the same way as not overwriting a partition with random data before encrypting it). It is up to you to decide whether this is ok for you.

Also note that deleting files will not make the disk image smaller. The sparse file will grow monotonically.

By: P Wendler

Category: Linux | Comments Off on Linux: auto mount cryptsetup/LUKS/encrypted loop device