LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   fdisk /dev/sda << __EOF__: What does this do? (https://www.linuxquestions.org/questions/linux-newbie-8/fdisk-dev-sda-__eof__-what-does-this-do-932648/)

stf92 03-04-2012 06:58 AM

fdisk /dev/sda << __EOF__: What does this do?
 
Code:

fdisk /dev/sda << __EOF__
n
p
1

+64M
t
e
p
w
__EOF__

What does this do? Can somebody tell me? I have tried 'fdisk /dev/sda << __EOF__', where /dev/sda is a micro SD memory card connected to the host through USB. A '>' prompt appeared. Then I typed '__EOF__' and I was back in the shell prompt:
Code:

root@darkstar:~# fdisk /dev/sda << __EOF__
> __EOF__

Command (m for help): Command (m for help): Command (m for help):
got EOF thrice - exiting..
root@darkstar:~#


Kernel 2.6.21.5, Slackware 12.0

druuna 03-04-2012 07:06 AM

Hi,

That is a so called HERE-Document construct.

All between both __EOF__ is given to fdisk in this case.

Have a look here: ABSG - Chapter 19. Here Documents

Hope this helps.

allend 03-04-2012 07:11 AM

From 'man bash'
Quote:

Here Strings
A variant of here documents, the format is:

<<<word

The word is expanded and supplied to the command on its standard input.

The code is running fdisk and giving all the commands to fdisk up to __EOF__

Too slow! druuna was quicker.

stf92 03-04-2012 07:16 AM

OK. But then I must take it that n, p, 1, et cetera are commands to fdisk? I do not think so because fdisk only accepts options.

allend 03-04-2012 07:21 AM

Reread the quote from 'man bash'. fdisk is receiving commands from standard input.
Those are commands you can type when you run fdisk.

druuna 03-04-2012 07:22 AM

Hi,
Quote:

Originally Posted by stf92 (Post 4618122)
OK. But then I must take it that n, p, 1, et cetera are commands to fdisk? I do not think so because fdisk only accepts options.

Yes, those are fdisk commands.

From the top of my head:
n - new partition
p 1 - primary, partition no 1
64 Mb (size)
t e - change partition id to "e" (see list for explanation)
p - show partition layout
w - write changes and exit.

Hope this helps.


Too slow! allend was quicker ;)

stf92 03-04-2012 07:38 AM

According to its manual, fdisk invoked as 'fdisk /dev/sda' is a menu driven program. So I did:
Code:

root@darkstar:~# fdisk /dev/sda

Command (m for help): m
Command action
  a  toggle a bootable flag
  b  edit bsd disklabel
  c  toggle the dos compatibility flag
  d  delete a partition
  l  list known partition types
  m  print this menu
  n  add a new partition
  o  create a new empty DOS partition table
  p  print the partition table
  q  quit without saving changes
  s  create a new empty Sun disklabel
  t  change a partition's system id
  u  change display/entry units
  v  verify the partition table
  w  write table to disk and exit
  x  extra functionality (experts only)

Command (m for help): q

root@darkstar:~#

and I see that is the only way to know the commands, for they're not explained in the manual. E.g., how do I know that the p command takes an argument, '1' in the example?

druuna 03-04-2012 07:51 AM

Hi,

A here-document is mostly used to automate commands you do often by hand. It does assume that you do know the particulars.

If this is the first time you use fdisk I would advise you not to use a here document but get familiar with fdisk first.

BTW the p used in your here-document is _not_ the p print the partition table option. It is part of the n add a new partition option. After using n more questions are asked (the p and 1 being answers to those questions).

Have a look here: Linux Partition HOWTO - 5. Partitioning with fdisk

Hope this helps.

stf92 03-04-2012 08:05 AM

Here is a more complete part of the story:
Code:

First blank the MMC card's partition table with parted: (/dev/sdX as an example)

sudo parted -s /dev/sdX mklabel msdos

With fdisk: (note: GNU Fdisk doesn't work..)

sudo fdisk /dev/sdX << __EOF__
n
p
1

+64M
t
e
p
w
__EOF__

I've read, in my system's documentation, that cfdisk is a more reliable tool than fdisk. Add to this that I've a high degree of familiarity with cfdisk. So, I intend to use cfdisk. And thanks to druuna:

Quote:

Originally Posted by druuna (Post 4618127)
Hi,Yes, those are fdisk commands.

From the top of my head:
n - new partition
p 1 - primary, partition no 1
64 Mb (size)
t e - change partition id to "e" (see list for explanation)
p - show partition layout
w - write changes and exit.

I know exactly what to do, except for the t e command. In the quoted post, it is read:

t e - change partition id to "e"

Could you druuna, or someone else, expand a little? The partition id... isn't it, for instance, 0x83 for ext2/ext3, or 0x82 for a linux swap? But what can "e" possibly be?

druuna 03-04-2012 08:13 AM

Hi,

The e when using fdisk's t option sets the W95 FAT16 (LBA) system ID.

Have a look at the bold parts:
Code:

$ fdisk /dev/sdb
.
.
.
Command (m for help): t
Partition number (1-8): 7
Hex code (type L to list codes): L


 0  Empty          1e  Hidden W95 FAT1 80  Old Minix      be  Solaris boot 
 1  FAT12          24  NEC DOS        81  Minix / old Lin bf  Solaris       
 2  XENIX root      39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 3  XENIX usr      3c  PartitionMagic  83  Linux          c4  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286    84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 5  Extended        41  PPC PReP Boot  85  Linux extended  c7  Syrinx       
 6  FAT16          42  SFS            86  NTFS volume set da  Non-FS data   
 7  HPFS/NTFS      4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 8  AIX            4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility 
 9  AIX bootable    4f  QNX4.x 3rd part 8e  Linux LVM      df  BootIt       
 a  OS/2 Boot Manag 50  OnTrack DM      93  Amoeba          e1  DOS access   
 b  W95 FAT32      51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O       
 c  W95 FAT32 (LBA) 52  CP/M            9f  BSD/OS          e4  SpeedStor     
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs       
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a5  FreeBSD        ee  EFI GPT       
10  OPUS            55  EZ-Drive        a6  OpenBSD        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk    a8  Darwin UFS      f1  SpeedStor     
14  Hidden FAT16 <3 61  SpeedStor      a9  NetBSD          f4  SpeedStor     
16  Hidden FAT16    63  GNU HURD or Sys ab  Darwin boot    f2  DOS secondary 
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs        fd  Linux raid auto
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap      fe  LANstep       
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid ff  BBT           
1c  Hidden W95 FAT3 75  PC/IX

I'm not familiar with cfdisk but I'm assuming that all the fdisk functionality is present and can be used the way it is with fdisk in the here-document code. Do make sure you don't make typo's when automating this!

Hope this helps.

stf92 03-04-2012 09:22 AM

Druuna: you may be wondering what am I aiming at. Briefly stated: I have a very little board called Beagleboard xM, build around an ARM processor, launched on the market by Texas Instruments. A friend of mine asked me to load Linux onto it (Linux embedded), unfortunately of the variety called Ubuntu. OK. To do the job, I decided to follow the instructions contained in this document, 6 Method 3: Manual Install (no automatic scripts).

Following the procedure, I did
Code:

parted -s /dev/sda mklabel msdos
/dev/sda is a micro SD card (just flash memory). The following step in the procedure is the famous fdisk /dev/sda << __EOF__ thing which, having changed my mind, I followed verbatim.

Well, to see what I had done, I used cfdisk to inspect the SD card, and every thing seems alright. The t e command: I did not realize 'e' is just 0x0e!

In the process, I learned what a here-document is and tried my hand with fdisk. No questions, then, in this post except for this: what could the procedure author mean by
Quote:

With fdisk: (note: GNU Fdisk doesn't work..)

sudo fdisk /dev/sdX << __EOF__
And how do I know if the fdisk in my system is GNU? Of course, many thanks for your posts, which I extend to the other people who have written in the thread.

druuna 03-04-2012 09:35 AM

Hi,
Quote:

Originally Posted by stf92 (Post 4618187)
No questions, then, in this post except for this: what could the procedure author mean by

And how do I know if the fdisk in my system is GNU? Of course, many thanks for your posts, which I extend to the other people who have written in the thread.

There are 2 programs called fdisk. One that can be found in the util-package and a different one from GNU, which is an alternative to the first.

Most linux distro's use the util-linux package. On my Debian box:
Code:

$ /sbin/fdisk -v
fdisk (util-linux-ng 2.17.2)

Hope this helps.

stf92 03-04-2012 10:04 AM

On my box I only get
Code:


root@darkstar:~# fdisk -v
fdisk v2.12r

But
Code:

root@darkstar:~# grep -irl fdisk /var/log/packages/
/var/log/packages/util-linux-2.12r-i486-6

So, I think I can be sure its not the GNU fdisk. Thanks again and best regards.

druuna 03-04-2012 10:11 AM

You're welcome :)


All times are GMT -5. The time now is 09:07 PM.