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
October 25

Linux: How to disable/enable journaling on an ext4 filesystem

At times, to meet performance requirements, you would want to disable file system journaling. Given below are steps to do so for an ext4 file system (e.g. /dev/sda1). These steps have been tested on RHEL 5.7). All commands are to be executed with root privileges:

STEP 1: Unmount the file system partition whose journaling you wish to disable

Use the following command to unmount the partition on /dev/sda1 (let’s say it’s /opt):

umount /opt

NOTE: The command used above is umount and not unmount.

STEP 2: Disable journaling for the file system

Use the following command to disable journaling for an ext4 file system:

tune4fs -O ^has_journal /dev/sda1

 

STEP 3: Perform a file system check

Use the following command to perform a file system check. This is not strictly required, but is recommended for checking file system integrity after disabling journaling:

e4fsck –f /dev/sda1

STEP 4: Reboot

You may use the following command to reboot the Linux OS:

shutdown –r now

STEP 5: Verify that the file system has journaling disabled and the partition is mounted

After the host has rebooted, you may use the following commands to check if journaling is disabled for the filesystem and the partition is mounted:

dmesg | grep EXT4

Expected output similar to: EXT4-fs (dm-3): mounted filesystem without journal

df -h
By: G Sature
Category: Linux | Comments Off on Linux: How to disable/enable journaling on an ext4 filesystem