Monday, April 14, 2014

Delete System Files On Windows 7

On Windows 7, system file are protected from being deleted. The following commands can be used to delete system files.

set THE_FILE_TO_DELETE=C:\Windows\IME\IMESC5\DICTS\PINTLGT.IMD
takeown /f %THE_FILE_TO_DELETE%
cacls %THE_FILE_TO_DELETE% /G YourUserName:F
del %THE_FILE_TO_DELETE%

Create Windows PE 5.1 Image

Listed below are the instructsions to create a Windows PE 5.1 x86 disk. To build an amd64 version, replace all ‘x86’ to ‘amd64’ in the commands below.

Prerequisites

Steps

  • Start “Deployment and Imaging Tools Environment” as administrator

  • Copy a basic PE image.

copype x86 C:\WinPE_x86
  • Mount the PE image to local directory
Dism /Mount-Image /ImageFile:"C:\WinPE_x86\media\sources\boot.wim" /index:1 /MountDir:"C:\WinPE_x86\mount"
  • (Optional) Add packages. For example, the following command adds .Net Framework to the PE
Dism /Add-Package /Image:"C:\WinPE_x86\mount" /PackagePath:"C:\Program Files (x86)\Windows Kits\8.1\Assessment and Deployment Kit\Windows Preinstallation Environment\x86\WinPE_OCs\WinPE-NetFx.cab"
  • (Optional) Verify packages using the following command (optional)
Dism /Get-Packages /Image:"C:\WinPE_x86\mount"
  • (Optional) Add thridparty programs. For example, the following command adds 7Zip to the PE
copy "C:\Program Files (x86)\7-Zip\7z*.*" C:\WinPE_x86\mount\Windows\System32
  • (Optional) Change PE boot drive letter. For example the following command change the PE’s boot drive to V:
Dism /image:C\WinPE_x86\mount /Set-TargetPath:V:\
  • Unmount the image and commit changes
Dism /Unmount-Image /MountDir:"C:\WinPE_x86\mount" /commit
  • Deploy the PE image to an ISO
MakeWinPEMedia /ISO C:\WinPE_x86 C:\WinPE_x86\WinPE_x86.iso
  • Deploy the PE image to an USB drive (e.g. U:)
MakeWinPEMedia /UFD C:\WinPE_x86 U:

Saturday, March 29, 2014

Windows Batch Scripting Tips

Find files recursively

for /r %%f in (*.abc) do echo %%f

or

for /f "delims=|" %%f in ('dir /B /S *.abc') do echo %%f

Block of statements

(The parentheses must be used exactly as shown)

if %VAR% == 0 (
    echo "statement 1"
    echo "statement 2"
)

Thursday, March 20, 2014

Setup Schroot Environment On Ubuntu

Here is my note about how to setup Ubuntu Lucid schroot environment on a Ubuntu box. Please refer to Ubuntu Document for details.

Software Prerequisits

sudo apt-get install debootstrap
sudo apt-get install schroot

Create a config file for Ubuntu Lucid (x86)

sudo vi /etc/schroot/chroot.d/lucid_i386.conf

Fill in the following content. Please make sure change the user bob to your actual username.

[lucid_i386]
description=Ubuntu 10.04 for i386
directory=/srv/chroot/lucid_i386
personality=linux32
type=directory
root-users=bob
users=bob

Get environment files

sudo mkdir -p /srv/chroot/lucid_i386
sudo debootstrap --arch=i386 lucid /srv/chroot/lucid_i386/ http://archive.ubuntu.com/ubuntu/

Done. Verify environment works

schroot -l
schroot -c lucid_i386 -u root
lsb_release -a

Here is the steps for Ubuntu Lucid (x64)

sudo vi /etc/schroot/chroot.d/lucid_amd64.conf
[lucid_amd64]
description=Ubuntu 10.04 for amd64
directory=/srv/chroot/lucid_amd64
#personality=linux32
type=directory
root-users=bob
users=bob
sudo mkdir -p /srv/chroot/lucid_amd64
sudo debootstrap --arch=amd64 lucid /srv/chroot/lucid_amd64/ http://archive.ubuntu.com/ubuntu/

For Debian Squeeze (x64)

sudo vi /etc/schroot/chroot.d/squeeze_amd64.conf
[squeeze_amd64]
description=Ubuntu 10.04 for amd64
directory=/srv/chroot/squeeze_amd64
#personality=linux32
type=directory
root-users=bob
users=bob
sudo mkdir -p /srv/chroot/squeeze_amd64
sudo debootstrap --arch=amd64 squeeze /srv/chroot/squeeze_amd64/ http://ftp.debian.org/debian/

Saturday, March 15, 2014

Setup Debian in a Virtualbox

The following tips can make it convenient to use debian in a virtual box, but please keep in mind that they are not good practices for a production server.

Disable Sudo Password Requirement

On ubuntu/debian, you could disable sudo password prompt by following the steps below.

sudo visudo

Change

%sudo ALL=(ALL) ALL

to

%sudo ALL=(ALL) NOPASSWD: ALL

Auto login

The following steps can let you log in automatically to a console when boots.

sudo vi /etc/inittab

Change the line

1:2345:respawn:/sbin/getty 38400 tty1

to

1:2345:respawn:/bin/login -f user </dev/tty1 >/dev/tty1 2>&1

Disable Grub Screen

The following steps can disable grub splash screen when boots

sudo vi /etc/default/grub

Change the value of GRUB_TIMEOUT to 0

And then run the following command to update grub config.

sudo update-grub

Friday, February 21, 2014

Targeting Windows XP with Visual C++ 2013

Build apps using Visual C++ 2013 that target Windows XP, special steps are needed.

Environment Variables to Set

The following codes is quoted from this blog.

set INCLUDE=%ProgramFiles(x86)%\Microsoft SDKs\Windows\7.1A\Include;%INCLUDE%
set PATH=%ProgramFiles(x86)%\Microsoft SDKs\Windows\7.1A\Bin;%PATH%
set LIB=%ProgramFiles(x86)%\Microsoft SDKs\Windows\7.1A\Lib;%LIB%
set CL=/D_USING_V110_SDK71_;%CL%

For building x64 version, change LIB to this one.

set LIB=%ProgramFiles(x86)%\Microsoft SDKs\Windows\7.1A\Lib\x64;%LIB%

For x86 console/windows applications, set /SUBSYSTEM accordingly:

:: for console application
set LINK=/SUBSYSTEM:CONSOLE,5.01 %LINK%
:: for gui application
set LINK=/SUBSYSTEM:WINDOWS,5.01 %LINK%

For x64 console/windows applications:

:: for console application
set LINK=/SUBSYSTEM:CONSOLE,5.02 %LINK%
:: for gui application
set LINK=/SUBSYSTEM:WINDOWS,5.02 %LINK%

Work with CMake

In CMake there is a WIN32 parameter for add_executable command. It works like this:

add_executable(MyExe WIN32 main.cpp) # this exe will link with /SUBSYSTEM:WINDOWS
add_executable(AnotherExe main.cpp)  # this one will link with /SUBSYSTEM:CONSOLE

When we not target Windows XP, it works correctly. But when we target Windows XP, obviously the link flags are not correct. To fix that, we need add a file to override make rules in CMake. CMAKE_USER_MAKE_RULES_OVERRIDE_CXX is for that purpose.

In your project file, add the following code.

cmake_minimum_required(VERSION 2.8)
set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX your_path/overrides.cmake)

# please make sure the variable is set before the `project` command.
# project(MyApp) 

Then in overrides.cmake, add the following codes.

if(WIN32)
    # if target winxp
    if(TARGETING_XP_64)
        SET(CMAKE_CREATE_WIN32_EXE /SUBSYSTEM:WINDOWS,5.02)
        SET(CMAKE_CREATE_CONSOLE_EXE /SUBSYSTEM:CONSOLE,5.02)
    elif(TARGETING_XP)
        SET(CMAKE_CREATE_WIN32_EXE /SUBSYSTEM:WINDOWS,5.01)
        SET(CMAKE_CREATE_CONSOLE_EXE /SUBSYSTEM:CONSOLE,5.01)
    endif()
endif()

Now, to build targetting Windows XP, you could run cmake like the folowing.

cmake -G "YourGenerator" -DTARGETING_XP=On path_to_your_project.cmake

And the following for XP x64.

cmake -G "YourGenerator" -DTARGETING_XP_64=On path_to_your_project.cmake

Tuesday, February 18, 2014

Core Dump on Ubuntu

Listed below are just several notes about core dump on Ubuntu.

The location of core dump files

The following command can print out where will the core dump files be placed.

cat /proc/sys/kernel/core_pattern

If the output start with a | character, the kernel then will write the core dump to the stdin of the command after the | character.

Core dump file size

The following command can remove core dump file size limit.

ulimit -c unlimited

Apport

On ubuntu, core dump files are forwarded to Apport by default. So if you can’t find the core file in your current directory, you may want to try /var/crash. Probably you will see crash report files there.

The command apport-unpack can be used to extract the core files out of a crash report files.