LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2007, 09:04 AM   #1
activeq
Member
 
Registered: Jul 2006
Location: Balen, Belgium
Distribution: Suse 10, Centos, Open Solaris
Posts: 76

Rep: Reputation: 15
howto format usb sticks in script


Hi,

I'm searching for a solution to format multiple usb sticks and create 2 new partitions.
1 partition have to be read only.

I was searching for fdisk, but this program have an own menu so I can't put this in a bash script to run automatically.

Does anybody know howto to do this?

Many thanks in advance.

Kind regards,
 
Old 07-12-2007, 09:23 AM   #2
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
You can edit the partition table using fdisk's command line arguments, check the man page of fdisk for more information.

As for making one partition read-only, I don't believe that is possible in the partition table. Or at least, I have never heard of that.
 
Old 07-12-2007, 09:41 AM   #3
activeq
Member
 
Registered: Jul 2006
Location: Balen, Belgium
Distribution: Suse 10, Centos, Open Solaris
Posts: 76

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by MS3FGX
You can edit the partition table using fdisk's command line arguments, check the man page of fdisk for more information.

As for making one partition read-only, I don't believe that is possible in the partition table. Or at least, I have never heard of that.
Thanks for your answer.

I tried fdisk, but this didn't worked. It keep showing fdísk's own menu.

Kind regards,
 
Old 07-12-2007, 09:47 AM   #4
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
@activeq: what exactly did you do? which commands?
 
Old 07-12-2007, 09:56 AM   #5
activeq
Member
 
Registered: Jul 2006
Location: Balen, Belgium
Distribution: Suse 10, Centos, Open Solaris
Posts: 76

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by muha
@activeq: what exactly did you do? which commands?

fdisk -d -o -w /dev/hda2

But I'll guess this will be wrong
 
Old 07-12-2007, 11:12 AM   #6
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
You may find sfdisk easier to use for running from a script.
 
Old 07-12-2007, 11:18 AM   #7
ramram29
Member
 
Registered: Jul 2003
Location: Miami, Florida, USA
Distribution: Debian
Posts: 848
Blog Entries: 1

Rep: Reputation: 47
The 'parted --script' command allows you to partition disks with single line commands. Here's an example bash script:


#!/bin/sh

echo "WANRNING! This CF Utility will format /dev/sda"
echo ""
echo "Detecting /dev/sda1"
echo ""
fdisk -l
echo ""
echo "Press any key if detected else Ctrl-C..."
read junk
echo "Formatting /dev/sda1"
echo ""
sleep 1
dd if=/dev/zero of=/dev/sda bs=8k count=1
sleep 1
parted --script /dev/sda mklabel msdos
parted --script /dev/sda mkpart primary ext2 0 122
parted --script /dev/sda mkpart primary ext2 122 128
parted --script /dev/sda mkfs 1 ext2
parted --script /dev/sda mkfs 2 ext2
parted --script /dev/sda set 1 boot on
parted --script /dev/sda print
sleep 5
mkfs.ext2 -b 1024 /dev/sda1
mkfs.ext2 -b 1024 /dev/sda2
 
Old 07-13-2007, 02:23 AM   #8
activeq
Member
 
Registered: Jul 2006
Location: Balen, Belgium
Distribution: Suse 10, Centos, Open Solaris
Posts: 76

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ramram29
The 'parted --script' command allows you to partition disks with single line commands. Here's an example bash script:


#!/bin/sh

echo "WANRNING! This CF Utility will format /dev/sda"
echo ""
echo "Detecting /dev/sda1"
echo ""
fdisk -l
echo ""
echo "Press any key if detected else Ctrl-C..."
read junk
echo "Formatting /dev/sda1"
echo ""
sleep 1
dd if=/dev/zero of=/dev/sda bs=8k count=1
sleep 1
parted --script /dev/sda mklabel msdos
parted --script /dev/sda mkpart primary ext2 0 122
parted --script /dev/sda mkpart primary ext2 122 128
parted --script /dev/sda mkfs 1 ext2
parted --script /dev/sda mkfs 2 ext2
parted --script /dev/sda set 1 boot on
parted --script /dev/sda print
sleep 5
mkfs.ext2 -b 1024 /dev/sda1
mkfs.ext2 -b 1024 /dev/sda2
Sorry...but this script isn't working on my Suse system nor Ubuntu.

I've checked the parted command and this is the command I have to go for, but the strange thing is, is that my 2 systems keep crying that /dev/sda is in use and can't be formated, set partitions or whatever.

Any idea how to overcome this?

Thanks in advance.
 
Old 07-13-2007, 03:04 AM   #9
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Quote:
my 2 systems keep crying that /dev/sda is in use and can't be formated, set partitions or whatever.
Are you sure it isn't mounted?
Code:
mount | grep sda
^should not give lines talking about sda; if it does,
Code:
umount /dev/sda
and then re-try.

EDIT: out of curiosity, because I haven't used parted, what's the difference between
Code:
parted --script /dev/sda mkfs 1 ext2
parted --script /dev/sda mkfs 2 ext2
and
Code:
mkfs.ext2 -b 1024 /dev/sda1
mkfs.ext2 -b 1024 /dev/sda2
or rather why do you run those both? Isn't parted making the filesystems, or what's that mkfs command in it - why do you have to run mkfs.ext2 after that again?

Last edited by b0uncer; 07-13-2007 at 03:06 AM.
 
Old 07-13-2007, 03:47 AM   #10
activeq
Member
 
Registered: Jul 2006
Location: Balen, Belgium
Distribution: Suse 10, Centos, Open Solaris
Posts: 76

Original Poster
Rep: Reputation: 15
With some more testing, i get it partically done.

this is what i do:

dd if=/dev/zero of=/dev/sda bs=8k count=1
parted -s /dev/sda mklabel msdos
parted -s /dev/sda mkpart primary fat32 0 100
parted -s /dev/sda mkpart primary fat32 100 500
parted -s /dev/sda mkfs 1 fat32
parted -s /dev/sda mkfs 2 fat32
parted -s /dev/sda set 1 boot on
parted -s /dev/sda print

This will work for Linux. I'll see 2 partitions.But if i put the same disk to a Windows machine, I only see one single drive which is the one i set 'boot on'.

I figured out that both partitions use id 'c'. and mkfs 2 isn't accessible in Windows.

How can I set id to 'd' so Windows will see both of my partitions?

Please see output below fdisk -l and note ID sda1 and sda2:

nl1email:~ # fdisk -l

Disk /dev/hda: 30.7 GB, 30750031872 bytes
255 heads, 63 sectors/track, 3738 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 1 131 1052226 82 Linux swap / Solaris
/dev/hda2 * 132 3738 28973227+ 83 Linux

Disk /dev/sda: 519 MB, 519831552 bytes
16 heads, 62 sectors/track, 1023 cylinders
Units = cylinders of 992 * 512 = 507904 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 197 97656 b W95 FAT32
Partition 1 has different physical/logical endings:
phys=(12, 40, 13) logical=(196, 14, 13)
Partition 1 does not end on cylinder boundary.
/dev/sda2 197 886 341797 b W95 FAT32
Partition 2 has different physical/logical beginnings (non-Linux?):
phys=(12, 40, 14) logical=(196, 14, 14)
Partition 2 has different physical/logical endings:
phys=(54, 180, 57) logical=(885, 15, 57)
Partition 2 does not end on cylinder boundary.
nl1email:~ #

Last edited by activeq; 07-13-2007 at 03:57 AM.
 
Old 07-13-2007, 07:33 AM   #11
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
As far as I know, Windows only looks at the first partition on removable USB devices. When I was still using Windows on my desktop, I could never find a way for it to recognize more than one partition on a removable device. Perhaps there is some trick to it.
 
Old 07-13-2007, 07:35 AM   #12
ramram29
Member
 
Registered: Jul 2003
Location: Miami, Florida, USA
Distribution: Debian
Posts: 848
Blog Entries: 1

Rep: Reputation: 47
That's another reason why I don't use Windows - it does not recognize the second partition on an USB pendrive. Let me repeat that. Windows will not recognize a second partition on a USB pendrive. Which makes sense because if you have many pendrives with many partitions then you will run out of alphabet letters for the 'drive letter'. I always found that so ridiculous.
 
Old 07-13-2007, 07:49 AM   #13
activeq
Member
 
Registered: Jul 2006
Location: Balen, Belgium
Distribution: Suse 10, Centos, Open Solaris
Posts: 76

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ramram29
That's another reason why I don't use Windows - it does not recognize the second partition on an USB pendrive. Let me repeat that. Windows will not recognize a second partition on a USB pendrive. Which makes sense because if you have many pendrives with many partitions then you will run out of alphabet letters for the 'drive letter'. I always found that so ridiculous.

Yes...this will be the thing because the second partition was recognized in device manager, but not in Explorer.

Unfortunatly 99% of our costumers use Windows, so automatically formatting and partitioning in Linux for USB pen drives is not possible :-(

Thanks for all your help. Again learned something.
 
Old 07-13-2007, 07:55 AM   #14
activeq
Member
 
Registered: Jul 2006
Location: Balen, Belgium
Distribution: Suse 10, Centos, Open Solaris
Posts: 76

Original Poster
Rep: Reputation: 15
One recall on your post ramram29.

We use software which format the pendrives into two partitions. Windows is recognize them as 2 different drives!!

If I can manage that with fdisk or parted, this would be great.

We copy alott of data to thousands of disks everyday. duplicating one pendrive to 32 others at ones. We use Windows for this at the moment, but Windows keeps losing its USB driver. have to reboot everytime to be recognized. Bcause I'm a big Linux fan, I would like to run the procedure with a script.

But....the last thing I need to do, I can't get it done. That is partitioning a pen drive in two partitions so Windows will recognize the two partitions a two drives and not 1.

pppffff
 
Old 07-13-2007, 08:02 AM   #15
hacker supreme
Member
 
Registered: Oct 2006
Location: As far away from my username as possible
Distribution: Gentoo
Posts: 259
Blog Entries: 1

Rep: Reputation: 31
I don't think that Windows detects it as two disks. It'll only recognise the first partition.


reason for edit: tryping erorts.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
howto auto-launch a script when usb key inserted? adamrosspayne Linux - Newbie 3 09-22-2006 04:21 AM
Best supported USB WLAN sticks MS3FGX Linux - Wireless Networking 4 04-17-2006 01:40 PM
mp3 usb players/sticks unihiekka Linux - Hardware 1 12-01-2005 12:32 PM
USB memory sticks island_dude Linux - Hardware 1 01-20-2004 11:16 AM
Mounting different usb-sticks robertmarkbram Linux - Hardware 3 10-22-2003 07:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration