Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

parted script to automate disk partitioning

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
20 posts • Page 1 of 1
Author
Message
Vieri
l33t
l33t
Posts: 935
Joined: Sun Dec 18, 2005 12:26 pm

parted script to automate disk partitioning

  • Quote

Post by Vieri » Tue Jul 12, 2011 3:14 pm

Hi,

I'd like to make a custom BASH script that calls parted or fdisk or whatever, to automatically partition a hard disk accroding to this criteria:

100MB for boot,
256MB for swap,
the rest for root.

If I use "parted" in the script (with -s) and pass the mkpart command with "start" and "end" as numerical values such as:

Code: Select all

DEV=/dev/sda
parted -s $DEV unit MB mkpart primary 0 100
then I get this warning from parted:
"The resulting partition is not properly aligned for best performance."
How can I avoid this? How can I properly align the partitions?

I searched the web and came across a post that suggested using percentage instead.

In fact, if I run:

Code: Select all

parted -s $DEV unit MB mkpart primary 0% 1%
then I don't get that warning.

However, I'd prefer to use fixed values to define boot and swap because 1% of a big disk is too much for a boot partition in my case.

Any ideas?

I'm also using plain BASH from a Gentoo livecd so I don't know if I can handle floats.

I get the total disk size this way:

Code: Select all

DEV=/dev/sda
DSIZE=$( parted -s $DEV unit B print | grep "^Disk $DEV:" | cut -f 3 -d " " | tr -d B )
and get DSIZE=250056015872

If I compute 104857600 / 250056015872 within BASH then I get 0 (as expected). So I don't know how to get decimal percentages in order for parted not to complain about partition alignment and get my boot and swap partition sizes right.

Any suggestions?

Thanks in advance,

Vieri
Top
idella4
Retired Dev
Retired Dev
User avatar
Posts: 1600
Joined: Fri Jun 09, 2006 11:29 am
Location: Australia, Perth

  • Quote

Post by idella4 » Tue Jul 12, 2011 3:29 pm

enter all required options for parted in a file, say parted.txt. The options do the desired setup. Then

# parted < parted.txt

A simple example to show it works.

archtester # cat parted.txt
/dev/sdb
p
q

will execute parted /dev/sdb, then execute p and q options in the parted shell
idella4@aus
Top
Vieri
l33t
l33t
Posts: 935
Joined: Sun Dec 18, 2005 12:26 pm

  • Quote

Post by Vieri » Tue Jul 12, 2011 3:51 pm

idella4 wrote:enter all required options for parted in a file, say parted.txt. The options do the desired setup.
Thanks for the reply but that doesn't really help. Whether I pass the commands via stdin or as command-line parameters doesn't make any difference.

If the txt file contains these lines:

Code: Select all

unit MB
mkpart primary 0 100 
I still get the warning that "The resulting partition is not properly aligned for best performance.".

Any other suggestions as how to create a 100MB boot partition with parted and not have this warning?
Top
jormartr
Apprentice
Apprentice
Posts: 174
Joined: Wed Jan 02, 2008 11:41 am

  • Quote

Post by jormartr » Tue Jul 12, 2011 3:57 pm

Instead of starting at MB 0, start at MB 1.
Top
Vieri
l33t
l33t
Posts: 935
Joined: Sun Dec 18, 2005 12:26 pm

  • Quote

Post by Vieri » Tue Jul 12, 2011 4:16 pm

jormartr wrote:Instead of starting at MB 0, start at MB 1.
Thanks but is there an explanation for this? Why 1MB and not more or less?
Top
jormartr
Apprentice
Apprentice
Posts: 174
Joined: Wed Jan 02, 2008 11:41 am

  • Quote

Post by jormartr » Tue Jul 12, 2011 4:44 pm

I may try, but not being english my first language, I'll be difficult.

Modern drives write in blocks larger than the traditional 512 bytes, so, you must align your partitions to the size of the block that the drive uses.

For example, new hard disks write in chunks of 4kb, even if you only write 1byte to disk. Suppose your ext3 partition starts at the start of the disk, on the first sector, and uses 4kb blocks. Everything is aligned, so when a block is written on the file system, only 4kb will be written to disk.

In the case you do not align your partition, let's see how is the first 4kb disk block:
your first 512 bytes are for the mbr and partition table, and your first ext3 partition, that uses 4kb blocks, starts at the second 512 sector (we call them sectors here, just to have a reference on how all this worked until recently). This way, the first 4kb block on the partition is using the last 7*512 sectors of the first 4kb chunk on the disk, and the last sector of the first 4kb block of the partition, corresponds to the first 512 sector of the second chunk of 4kb of the disk.

Now you are in the situtation, that when your file system wants to write a 4kb block, the hard disk needs to write 2 chunks of 4kb each one. Imagine this problem (a big problem until here on new mechanical drives) on SSD, where units write in chunks of 256kb,512kb ...

For this reason, parted aligns to the megabyte (you could align to less, depending on the drive, but the reason of a megabyte is just to be on the safe side), and because the first 512 bytes of the hard disk are used by the partition table and mbr, you cannot use the remaining 1048064 bytes of the first mb on disk (1Mbyte - 512bytes = 1048064 bytes).

Well, my english is not that good, I hope anybody can understand something here ...
Top
Vieri
l33t
l33t
Posts: 935
Joined: Sun Dec 18, 2005 12:26 pm

  • Quote

Post by Vieri » Tue Jul 12, 2011 4:59 pm

Thank you very much. Great explanation!

I'm running into another small problem, still related to parted.

I'm running this from BASH to create my root partition:

Code: Select all

parted $DEV unit MB mkpart primary 356 -0
but I'm getting this error:

Code: Select all

parted: invalid option -- '0'
Running the same commands from the parted shell is OK.

Running these commands from a text file I pipe to parted (eg. parted < test.txt) makes parted loop forever and I need to kill it from another console.
Top
jormartr
Apprentice
Apprentice
Posts: 174
Joined: Wed Jan 02, 2008 11:41 am

  • Quote

Post by jormartr » Tue Jul 12, 2011 5:12 pm

I think that last Mbyte on disk is selected with "-1", instead of "-0"
Top
Vieri
l33t
l33t
Posts: 935
Joined: Sun Dec 18, 2005 12:26 pm

  • Quote

Post by Vieri » Tue Jul 12, 2011 5:23 pm

Quoting http://www.gnu.org/software/parted/manu ... ted_2.html:
Negative numbers count from the end of the disk, with "-0" being the end of the disk.
In any case, using -1 at the command line gives me the same "invalid option" message.
Top
jormartr
Apprentice
Apprentice
Posts: 174
Joined: Wed Jan 02, 2008 11:41 am

  • Quote

Post by jormartr » Tue Jul 12, 2011 5:36 pm

Parted interprets -0 as an option (like for example gzip -1, where -1 is an option).

Run it as:

Code: Select all

parted -- /dev/xxx unit MB mkpart primary 300 -0
the two dashes indicates parted that you finished passing options, and what comes next are only arguments.
Top
Vieri
l33t
l33t
Posts: 935
Joined: Sun Dec 18, 2005 12:26 pm

  • Quote

Post by Vieri » Tue Jul 12, 2011 6:00 pm

Correct! I was suspecting that but didn't know how to fix it. Thanks!

Just one last doubt.
Each time a mkpart from command line or from within the parted shell, I get the message:
Error: Error informing the kernel about modifications to partition /dev/mapper/isw_djiidbhagi_raid1disks1 -- Invalid
argument. This means Linux won't know about any changes you made to /dev/mapper/isw_djiidbhagi_raid1disks1 until you
reboot -- so you shouldn't mount it or use it in any way before rebooting.
I know that if I press "i" to ignore it then all's OK.

However, in non-interactive mode (-s), this can be a problem.
Can I "force ignore" parted in -s mode or can I fix the parted error "informing the kernel -- Invalid argument".
Could this be a parted/kernel version incompatibility or is this expected behavior?
Top
jormartr
Apprentice
Apprentice
Posts: 174
Joined: Wed Jan 02, 2008 11:41 am

  • Quote

Post by jormartr » Tue Jul 12, 2011 6:15 pm

What is exactly the problem?

Sorry, I did not understood where do you want to go.
Top
Vieri
l33t
l33t
Posts: 935
Joined: Sun Dec 18, 2005 12:26 pm

  • Quote

Post by Vieri » Wed Jul 13, 2011 6:28 am

Here's a section of my custom script:

Code: Select all

#!/bin/sh

echo "Enter device (eg. /dev/sda):"
read DEV
echo "$DEV partition table:"
parted -s $DEV unit MB print
echo "Re-partitioning $DEV. Press ENTER to continue."
read
parted $DEV mklabel msdos
parted $DEV unit MB mkpart primary ext2 1 100
parted $DEV set 1 boot on
parted $DEV unit MB mkpart primary linux-swap 100 356
parted -- $DEV unit MB mkpart primary ext3 356 -0
echo ">>> $DEV Partition table is now:"
parted -s $DEV unit MB print
What I would like to do is that when a user launches this script, no user interaction is needed between the messages
"Re-partitioning $DEV. Press ENTER to continue."
and
">>> $DEV Partition table is now:".
So what I do is add the "-s" option to each parted call.
That works fine (ie. the partition table is correctly created) but for each call to parted I get the error message:

Code: Select all

Error: Error informing the kernel about modifications to partition /dev/mapper/isw_djiidbhagi_raid1disks1 -- Invalid argument.  This means Linux won't know about any changes you made to /dev/mapper/isw_djiidbhagi_raid1disks1 until you reboot -- so you shouldn't mount it or use it in any way before rebooting.
Error: Failed to add partition 1 (Invalid argument)
Surely I could redirect this to /dev/null and ignore it altogether but should I?
Parted's exit status is 1 when this error pops up.
Should my script abort when parted's exit code is != 0 or should I simply ignore it?
Why is there an error "informing the kernel about modifications to the partition"?

Thanks again.
Top
jormartr
Apprentice
Apprentice
Posts: 174
Joined: Wed Jan 02, 2008 11:41 am

  • Quote

Post by jormartr » Thu Jul 14, 2011 1:43 pm

How is the disk that you are modifying and getting these messages, before you modify it? Does it have any partition prior? Do you delete any old partition before you create the new ones? If so, what kind of partitions are these? Are you using mdadm/lvm on these?
Top
Vieri
l33t
l33t
Posts: 935
Joined: Sun Dec 18, 2005 12:26 pm

  • Quote

Post by Vieri » Fri Jul 15, 2011 6:57 am

The disk has an identical partitioning layout. Also has the same file systems: part1 "boot": ext2, part2 "linux swap", part3 "ext3". I don't use lvm, etc.

What I do first is:

Code: Select all

dd bs=512 count=63 if=/dev/zero of=$TARGET_DEVICE
parted -s $TARGET_DEVICE mklabel msdos
then I run the parted commands to create the partitions and here's when I get the error message, one for each parted call:

Code: Select all

parted -s $TARGET_DEVICE unit MB mkpart primary ext2 1 100
parted -s $TARGET_DEVICE set 1 boot on
parted -s $TARGET_DEVICE unit MB mkpart primary linux-swap 100 356
parted -s -- $TARGET_DEVICE unit MB mkpart primary ext3 356 -0
Ignoring the message seems to be OK but I'm wondering what's the exact meaning of it.
Top
jormartr
Apprentice
Apprentice
Posts: 174
Joined: Wed Jan 02, 2008 11:41 am

  • Quote

Post by jormartr » Fri Jul 15, 2011 7:16 am

These messages means that the kernel is not aware of the new partition table you are writing to the disks, and so, if there is some volume on these prior to your parted modifications, system will behave unnotified, and you need to reboot to make use of them.

I find strange that these messages appear, if the disks are empty, specially because there is something related to /dev/mapper. That is why I was asking you if there was some lvm setup on these disks.

If you go this way:
1. Umount any volume on disks
2. Deactivate any lvm/mdadm on disks
3. Partition (parted scripts)

You should not have any problem.

Also, after the parted scripts, if you see that the kernel does not recognize new partition table, run the command partprobe, this way you do not need to reboot to continue working with them.
Top
Vieri
l33t
l33t
Posts: 935
Joined: Sun Dec 18, 2005 12:26 pm

  • Quote

Post by Vieri » Fri Jul 15, 2011 7:58 am

I'm not using LVM and when I boot the gentoo kernel from the live CD, I don't pass the "dolvm" option.
But I do pass the dodmraid parameter and run dmraid -a y.

Anyway, all's well despite the error messages.
After parted has finished working, I can run fdisk and list the partitions just fine. So I'm supposing partprobe isn't necessary.

By the way, I'm also getting a warning when making swap:

Code: Select all

mkswap: /dev/mapper/isw_djiidbhagi_raid1disks2: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
I don't know what it means but I'm guessing that it's not important and should not use "-f".
Top
Vieri
l33t
l33t
Posts: 935
Joined: Sun Dec 18, 2005 12:26 pm

  • Quote

Post by Vieri » Fri Jul 15, 2011 8:14 am

By the way, partprobe /dev/mapper/mydevice gives me the same error message as above.
Anyway, I think I can safely ignore it.
Top
jormartr
Apprentice
Apprentice
Posts: 174
Joined: Wed Jan 02, 2008 11:41 am

  • Quote

Post by jormartr » Fri Jul 15, 2011 9:43 am

By recognizing partition table, I mean that you can access the new /dev/sdxx. If you don't, then execute partprobe, without arguments.

Now that you explain that about the dmraid, it explains why parted warns you.

If you modify any of the disks that contain dmraid volumes, deactivate these dmraid volumes on these disks if you do anything else than just modifying partition tables (like mkfs, mounts ...). If the disks does not contain any dmraid volume, then you can safely ignore the warns.
Top
Vieri
l33t
l33t
Posts: 935
Joined: Sun Dec 18, 2005 12:26 pm

  • Quote

Post by Vieri » Fri Jul 15, 2011 1:59 pm

So basically, it's not "safe" to partition & mke2fs, etc., on a dmraid activated disk.
So if I have a dmraid disk such as /dev/mapper/whatever, I should first deactivate dmraid and then partition and mk filesystems directly on the hard disk devices which in my case are /dev/sda and /dev/sdb.
Is that right?
Top
Post Reply

20 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic