6

I want to write protect and unprotect it to keep files from being formatted whenever people use it in a windows PC. I don't want people to delete my school work and I bring this to school and use it with a live CD of Lubuntu 12.04 in my moms laptop. People have got into my backpack while I went to the bathroom and plugged it into a PC (the flash drive had ex4 file format) and when it asked to format it they clicked yes assuming nothing will be deleted.

1
  • 1
    Just keep it in your pants :)
    – Symin
    Aug 27, 2013 at 7:16

3 Answers 3

1

I like your question!. But I dont think there is a direct way for it. I suggest a workaround.

You could create 2 partitions- the first one fat32 (for windows) and the second one ext2 (for you). As windows only recognises one partition per flash drive, it wont recognise your ext2 partition and hence wont format it.

So if anybody plugs in your Usb in windows your data wont be deleted.

Also I think ext2 or ext3 is better for flash drives rather than ext4.

You could create these two partitions using Gparted.

However making more than 1 partition is not very well supported so use it with care and only when you need to.

And as suggested, always make backups!

3
  • If you want help with partitioning just ask :)
    – abchk1234
    Aug 27, 2013 at 5:27
  • 1
    Unless you're using it for an extremely write-heavy application, don't use ext2 for flashdrives; it's non-journalled filesystem, so you're quite likely to lose data on unclean mount. The meager performance or extended life benefit of not journalling is usually not worth the risk of data losses, considering most SD card now employs good wear leveling algorithm.
    – Lie Ryan
    Aug 27, 2013 at 6:12
  • Windows is not as good as Linux when it comes to partitions. I made a 16GB flashdrive with half NTFS and half ext4. Windows did not recognise it and wanted to format it before I could use it. Jan 4, 2014 at 7:34
0

There is no way to make an ordinary USB flash disk read only. There are some 'tricks' but they are system and machine specific.

If you are worried about your data you could get USB with hardware encryption, but people will still be able to format it.

Just keep it with you at all times if you can.

0

My recommendation is to buy a flash drive designed for this purpose, no way to retrofit an existing flash drive. Either a USB flash drive have a write protector switch or they don't.

There is also a product called the USB Write blocker. 1 end plugs into the computer and you plug your usb hard drive or USB memory stick into the other end.

Now I found a script by xorangekiller, he claims that the script has been tested to work on Debian 6, Debian 7, Ubuntu 10.04, Ubuntu 12.04, and Fedora 17. And presumably it should work on any GUN/Linux distribution, and probably BSD and OS X too. Below is the script:

#!/bin/bash

# Write Protect Drive
# Description: This script will write protect a flash drive by using all the remaining free space.
# Last Synced with Awesomestik Installer 1.0
# Author: xorangekiller
# Released: 21 Feb 2012

writeprotect() {
    loop=0
    free=$(df $USBDEV | tail -n 1 | awk {'print $4;'})
    echo "Free space on ${USBDEV}: ${free}K"
    while [ $free -gt 0 ]; do
        # Note that 1048576 is 1 gigabyte in kilobytes.
        if [ $free -gt 1048576 ]; then
            free=1048576
        fi
        # Check that the name of the file we want to write is not already taken.
        while [ -e "${USBMNT}/IamDummy${loop}" ]; do
            let loop=loop+1
        done
        echo "Writing file ${USBMNT}/IamDummy${loop} of size ${free}K . . ."
        dd if=/dev/zero of=${USBMNT}/IamDummy${loop} bs=${free}K count=1
        sleep 5 # Give everything time to settle.
        free=$(df $USBDEV | tail -n 1 | awk {'print $4;'})
    done
    echo "Free space on ${USBDEV}: ${free}K"
}

# Check that we are root before doing anything particularly useful.
#if [ $(id -u) != 0 ]; then 
#   echo "You need to be root to run this script"
#   exit 1
#fi

if [ -n "$1" ]; then
    drivetoprotect="$1"
else
    echo "Write protect the specified drive by using all available free space."
    echo "To protect the current drive just type current at the prompt."
    echo "Which drive would you like to protect?"
    read drivetoprotect
fi

if [ "$drivetoprotect" == "current" ]; then
    drivetoprotect=`pwd`
fi

if [ ! -d "$drivetoprotect" ]; then
    echo "ERROR: $drivetoprotect is not a valid directory."
    exit 1
fi

# This check was implemented in the original write protect script because of Cooper and carried over here... DO NOT TRY TO WRITE PROTECT YOUR SYSTEM DRIVE!
if [ "$(df "$drivetoprotect" | awk '{print $6}' | tail -n 1)" == "/" ]; then
    echo "ERROR: You are attempting to write protect your system drive."
    exit 1
fi

# Use the USBMNT variable to maintain compatability with the writeprotect function from the Awesomestik Installer script.
USBMNT=$drivetoprotect

USBDEV=$(df "$USBMNT" | awk '{print $1}' | tail -n 1)
writeprotect

echo "The drive is now write protected!"
exit 0

Source:USB Write Blocker : Makes any USB drive Write protected

Not the answer you're looking for? Browse other questions tagged .