Edit an Rasbian image file

I find myself doing the same OS tweaks/fixes over and over again after we have just burned a brand new Rasbian SD card.
These fixes are:
1. Enable SSH fix
2. Enable WiFi networking fix
3. Fix the WiFi Power Management bug
4. Fix the rsyslog/xconsole bug
5. Disable IP6 fix

The solution to avoiding repetition: create our own edited copy of a rasbian img file.
How to do this?

Preperation
Copy image, create mountpoint and get our offset values needed for mounting

su
mkdir /mnt/img
cp /home/wouter/Downloads/2017-03-02-raspbian-jessie-lite.img /home/wouter/Downloads/2017-03-02-raspbian-jessie-lite-wspaa.img
fdisk -lu /home/wouter/Downloads/2017-03-02-raspbian-jessie-lite-wspaa.img
 Disk /home/wouter/Downloads/2017-03-02-raspbian-jessie-lite-wspaa.img: 1,3 GiB, 1393557504 bytes, 2721792 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: 0xb2455b06

 Device                                                            Boot  Start     End Sectors  Size Id Type
 /home/wouter/Downloads/2017-03-02-raspbian-jessie-lite-wspaa.img1        8192  137215  129024   63M  c W95 FAT32 (LBA)
 /home/wouter/Downloads/2017-03-02-raspbian-jessie-lite-wspaa.img2      137216 2721791 2584576  1,2G 83 Linux

Lets get our offset values, multiply the start values with 512 and we get 4194304 and 70254592 for offset values

Step 1
Create ssh file on boot partition

mount -t vfat -o loop,offset=4194304 /home/wouter/Downloads/2017-03-02-raspbian-jessie-lite-wspaa.img /mnt/img
touch /mnt/img/ssh
umount /mnt/img

Step 2
Add WiFi stuff to /etc/wpa_supplicant/wpa_supplicant.conf:

mount -t ext4 -o loop,offset=70254592 /home/wouter/Downloads/2017-03-02-raspbian-jessie-lite-wspaa.img /mnt/img
nano /mnt/img/etc/wpa_supplicant/wpa_supplicant.conf
country=NL
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
    ssid="SSID"
    psk="PW"
}

Step 3
Disable WiFi power management, add the line “wireless-power off” to the correct position in /etc/network/interfaces

nano /mnt/img/etc/network/interfaces
...
iface wlan0 inet manual
wireless-power off
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
...

Step 4
Removing [action ‘action 17’ suspended] rsyslog Spam on Raspberry Pi by commenting the last lines of /etc/rsyslog.conf

nano /mnt/img/etc/rsyslog.conf
...
#daemon.*;mail.*;\
#       news.err;\
#       *.=debug;*.=info;\
#       *.=notice;*.=warn       |/dev/xconsole

Step 5
Disable ipv6

sudo nano /mnt/img/etc/modprobe.d/ipv6.conf
# Don't load ipv6 by default
alias net-pf-10 off
alias ipv6 off
options ipv6 disable_ipv6=1

Now let unmount the image and we are done!

umount /mnt/img

For mounting / unmounting the img file we can also use this script I found here:

    #!/bin/sh
    # Author: Dubravko Penezic, dpenezic@gmail.com
    #
    # Credit to valuable answer from few users on Raspberry Pi Forum , http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=28860
    #

    OPTION=$1
    IMG_FILE=$2
    MOUNT_POINT=/datacopy/RPI-raspbian-image/Work/Img

    IMG_DIR=$(basename "$IMG_FILE")
    IMG_DIR="${IMG_DIR%.*}"

    if [[ "$#" -lt 2 || ("$1" != "m" && "$1" != "u") ]]
      then
        echo "Please use script as follow:"
        echo ""
        echo '> $0 m|u <disk_img_file>'
        echo "     m - mount"
        echo "     u - umount"
        exit 1
    fi

    if [[ "$1" == "m" ]]
      then
        SECTOR_OFFSET=$(sudo /sbin/fdisk -lu $IMG_FILE | awk '$6 == "Linux" { print $2 }')
        BYTE_OFFSET=$(expr 512 \* $SECTOR_OFFSET)
        SECTOR_OFFSET_BOOT=$(sudo /sbin/fdisk -lu $IMG_FILE | awk '$6 == "W95" { print $2 }')
        BYTE_OFFSET_BOOT=$(expr 512 \* $SECTOR_OFFSET_BOOT)

        echo Mounting image / at $MOUNT_POINT/$IMG_DIR
        echo Sector offset $SECTOR_OFFSET - Byte offset $BYTE_OFFSET

        sudo mkdir -p $MOUNT_POINT/$IMG_DIR
        sudo mount -t ext4 -o loop,offset=$BYTE_OFFSET $IMG_FILE $MOUNT_POINT/$IMG_DIR

        echo Sector offset $SECTOR_OFFSET_BOOT - Byte offset $BYTE_OFFSET_BOOT
        echo Mounting image /boot at $MOUNT_POINT/${IMG_DIR}_boot

        sudo mkdir -p $MOUNT_POINT/${IMG_DIR}_boot
        sudo mount -t vfat -o loop,offset=$BYTE_OFFSET_BOOT $IMG_FILE $MOUNT_POINT/${IMG_DIR}_boot
    fi

    if [[ "$1" == "u" ]]
      then

        echo Unmounting image / at $MOUNT_POINT/$IMG_DIR
        sudo umount $MOUNT_POINT/$IMG_DIR
        sudo rmdir $MOUNT_POINT/$IMG_DIR

        echo Unmounting image /boot at $MOUNT_POINT/${IMG_DIR}_boot
        sudo umount $MOUNT_POINT/${IMG_DIR}_boot
        sudo rmdir $MOUNT_POINT/${IMG_DIR}_boot

    fi
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s