Sunday, January 06, 2013

scripting disk partitionning in Linux - take 2

It is possible to use parted to script/automate disk partitioning in Linux, as described in "Command-line partitioning and formatting".

Another way is to use sgdisk from the GPT fdisk programs.

In Debian and derivatives, it can be installed with sudo apt-get install gdisk.

The current version 0.8.1 from the Ubuntu 12.04 repository would partition only the first 2TB of a 4 TB. disk. So you may need to get a more recent version from the downloads page. I got version 0.8.5 for x64, and that worked very well.

The following will create and format a single NTFS partition on an entire drive:

disk=/dev/sdb            # Make sure you got this right !!
label="My_Disk_Name"
echo "disk $disk will be completely erased."

sudo sgdisk -Z $disk
sudo sgdisk --new=0:0:-8M -t 1:0700 $disk
sudo sgdisk -p $disk
sudo mkntfs --verbose --fast --label "$label" --no-indexing --with-uuid ${disk}1

-Z removes any left-over partitions

--new=0:0:-8M creates a single partition from the start of the disk to 8MB before the end (just in case it's useful to not end on the very last sector)

-t 1:0700 sets the first partition we just created to type "Microsoft Basic Partition", which is the type we want for a simple NTFS partition. Linux would be -t 1:8300. Use sgdisk -L to get a list of partition types.

Note that for comfortable (and safer) manual partitioning, there is also cgdisk. It is like the old cfdisk, but works with new disks over 2TB.

Labels: , , , , , , , ,

Tuesday, June 21, 2011

Command-line partitioning and formatting

Automatic non-interactive formatting in Linux is possible with parted.
The following creates 1 single ext3 partition on an entire disk. Of course, if you assign the wrong disk to the $disk variable, it will be a bad day...
# Select the disk device and chose a label for the partition
# ptype=msdos for disks up to 2 TB. ptype=gpt for disks over 2 TB
disk=/dev/sdx; label=my_part_label; ptype=msdos
I have added sleep commands, so I can just copy/paste the whole thing and still have a chance to Ctrl-C if I change my mind at the last second.

# print the current partition(s) state
parted $disk print ; sleep 10

# create a gpt ot msdos partition table (depending on $ptype variable defined above)
parted -a optimal $disk mklabel $ptype ; sleep 5

# create the partition, starting at 1MB which may be better
# with newer disks
parted -a optimal -- $disk unit compact mkpart primary ext3 "1" "-1" ; sleep 5

# format it
mke2fs -j -v -L "$label" ${disk}1 && echo "OK. That's it"

Update: see also scripting disk partitionning in Linux - take 2 for another way, using sgdisk instead of parted.

Labels: , , , , , , ,

Tuesday, June 07, 2011

Windows installers options for silent installs

Different installers use different command-line options for silent or unattended installs. Since I had started these notes, I have found a good overview on unattended.sourceforge.net.

Inno Setup

can be identified with the "Inno Setup" string appearing in various places in the installer's .exe. The options are described here. The most useful ones are:
  • /SAVEINF="filename"
    Save installation settings to the specified file.
  • /LOADINF="filename"
    Load the settings from the specified file after having checked the command line.
  • /SP-
    Disables the This will install... Do you wish to continue? prompt at the beginning of Setup.
  • /SILENT, /VERYSILENT
    When Setup is silent the wizard and the background window are not displayed but the installation progress window is. When a setup is very silent this installation progress window is not displayed. Everything else is normal so for example error messages during installation are displayed.
  • /DIR="x:\dirname"
  • /LANG=language
    Specifies the language to use. language specifies the internal name of the language as specified in a [Languages] section entry.

Nullsoft's NSIS

can be identified with the "NSIS" string appearing in various places in the installer's .exe. The options are described here, but there seem to be only 2 useful ones:
  • /S
    Silent installation
  • /D=C:\Bla
    Set output folder

Labels: , , , , , ,

Friday, May 16, 2008

Windows ip config with batch scripts

It is possible to configure TCP/IP with simple batch scripts in Windows XP, even though the information is well hidden. There are a few typical Windows annoyances attached, but for a notebook user who needs to change settings often, double-clicking a batch file is definitely much better than the network configuration GUI.

The almost secret - even though it is included in the default XP install - tool is called netsh.

Below are a few examples. The main idiosyncrasy is that you need the name of the interface to configure, and that name is somewhat unpredictable. In particular, it is localized. For the normal Ethernet interface, in an English system, it starts with "Local Area Connection". But it is different in other language versions of Windows. There is no simple standard name like "eth0" or "en0".

Look up your interface names in "Network Connections", or by typing ipconfig or
netsh interface show interface
at the command prompt.

A simple batch file which will set your LAN interface to DHCP:
SET LAN=Local Area Connection

netsh interface ip set address name="%LAN%" source=dhcp
netsh interface ip set dns name="%LAN%" source=dhcp register=NONE
netsh interface ip set wins name="%LAN%" source=dhcp

This one will set it to a fixed IP address:
SET LAN=Local Area Connection

SET IP=192.168.1.56
SET MASK=255.255.255.0

SET GW=192.168.1.1
SET DNS=%GW%

netsh interface ip set address name="%LAN%" source=static addr=%IP% mask=%MASK%
netsh interface ip set address name="%LAN%" gateway=%GW% gwmetric=0
netsh interface ip set dns name="%LAN%" source=static addr=%DNS% register=NONE

Finally, a more complete example, which sets both the LAN and WIFI interfaces can take an optional second DNS server, and set the WINS server:
SET LAN=Local Area Connection 5
SET WIFI=Wireless

SET IPLAN=192.168.1.56
SET MASKLAN=255.255.255.0

SET IPWIFI=192.168.1.57
SET MASKWIFI=255.255.255.0

SET GW=192.168.1.1
SET DNS=192.168.1.1
SET DNS2=
SET WINS=none

netsh interface ip set address name="%LAN%" source=static addr=%IPLAN% mask=%MASKLAN%
netsh interface ip set address name="%LAN%" gateway=%GW% gwmetric=0
netsh interface ip set dns name="%LAN%" source=static addr=%DNS% register=NONE
IF NOT "%DNS2%" == "" netsh interface ip add dns name="%LAN%" addr=%DNS2% index=2
netsh interface ip set wins name="%LAN%" source=static addr=%WINS%

netsh interface ip set address name="%WIFI%" source=static addr=%IPWIFI% mask=%MASKWIFI%
netsh interface ip set address name="%WIFI%" gateway=%GW% gwmetric=0
netsh interface ip set dns name="%WIFI%" source=static addr=%DNS% register=NONE
IF NOT "%DNS2%" == "" netsh interface ip add dns name="%WIFI%" addr=%DNS2% index=2
netsh interface ip set wins name="%WIFI%" source=static addr=none
netsh interface ip set wins name="%WIFI%" source=static addr=%WINS%


You can also use netsh to configure the Windows firewall, check network connectivity, etc.

As a ping replacement, try this:
netsh diag ping adapter 1
(replace "1" with your network adapter's index number, which you can find with netsh diag show adapter). It will automatically ping your gateway, your DNS server(s) and your own IP address.

For more information, try these links:
How can I configure TCP/IP settings from the Command Prompt?
or
10 things you should know about the NETSH tool
or just do a web search on "netsh".

Labels: , , , , ,

Wednesday, January 16, 2008

Simple password management

To easily manage all your passwords, you don't need any freeware/shareware/crapware/malware/whateverware. If you are running Windows, all you need is 2 batch files, each containing a single line.

As a bonus, you can get some very simple security-through-obscurity by using a little known feature of the NTFS file system called "Alternate Data Streams". The security is not great, but the obscurity feels like a cool hack. And it's still better than having passwords.txt on your desktop, or Post-its on your monitor. (Of course, you can also skip the coolness and combine these handy batch files with the excellent TrueCrypt for really strong encryption at the expense of a minimum of additional hassle).

  1. Create a file containing anything (or nothing). Let's call it x, and put it in our profile folder (C:\Documents adn Settings\username\)
  2. Create a batch file (let's call it password-add.bat) with one line:
    @ECHO %* >> "%USERPROFILE%\x:passwords"
  3. Create a second batch file (for example password.bat) also with one line :
    @FIND /I "%1" < "%USERPROFILE%\x:passwords"
  4. Copy these two files to some directory in your path (like C:\Windows or C:\Windows\System32)
To add your new Google user name and password, open a Command Prompt window, and type:

password-add "Google: mystupidname@gmail.com pass: ul7ra-secr37"

To retrieve that password once you have forgotten it, type anything like

password Google
or
password stupid
or
password @gmail.com
etc.

To add some obscurity, call the batch files something else (and shorter so you don't have to type so much): like newp.bat and p.bat.

To add even more obscurity, copy some small .dll file in c:\Windows\System32 to a new name like msp32.dll, and in the batch files replace "%USERPROFILE%\x:passwords" with "c:\Windows\System32\msp32.dll".

To add real security, get TrueCrypt, and put the file on a TrueCrypt volume. (Don't forget to correct the 2 batch files).

Important: This only works on NTFS partitions. If you move your file to a FAT32 partition or send it by email or FTP, all your passwords are lost forever. If your backups are done to an external FAT32 disk, you won't have a backup either. You can move the file around as much as want, providing that it always stays on NTFS partitions. If you copy over a network, the server also needs to be Windows (not Samba).

Labels: , , , , , , ,